Java综合编程练习
实验内容
- 使用商运算(/)和求余运算(%),编程将一个整数部分的每一位数分别显示出来。例如:整数1234,其每一位分别是:1、2、3、4。
package EXception;
public class Exception1 {
public static void main(String[] args) { // TODO Auto-generated method stub Exception1_1 a=new Exception1_1(); a.chu(123456); } } ------------------------------------------------------------------- package EXception;
public class Exception1_1 { static void chu(int num){ int num1=num; int i=0; do{ i++; num/=10; }while(num>0); int[] a= new int[i]; for(int j=0;j<i;j++) { int x=num1%10; a[j]=x; num1/=10; } for(int k=i-1;k>=0;k--) { System.out.print(a[k]+" "); } } }
|
- 公鸡5元1只,母鸡3元1只,小鸡1元3只,用100元钱买100只鸡。问买公鸡、母鸡、小鸡各多少(要求每种鸡都有)?
package EXception;
public class Exception2 {
public static void main(String[] args) { // TODO Auto-generated method stub for(int x=0;x<=100;x++) { for(int y=0;y<=100;y++) { for(int z=0;z<=100/3;z++) { if((x+y+3*z==100) && (5*x+3*y+z==100)) { System.out.println("公鸡:"+x+" 母鸡:"+y+" 小鸡:"+3*z); } } } } } }
|
- 以下方法返回数组元素中最大值和最小值之间的差值,编程实现和测试该方法:
static double range(double[] x);
- 创建两个类,类Compute中定义加减乘除4个方法,并且该类存在于mypackage内。在另一个测试类Test中引入这个包,创建一个Compute类的实例调用类Compute中的方法,实现以下运算:(1+2)*3。
package EXception; import EXception.Computer; public class EXception4 {
public static void main(String[] args) { // TODO Auto-generated method stub Computer com=new Computer(); System.out.println(com.chen(com.add(1, 2), 3)); } } -------------------------------------------------------------------------- package EXception;
public class Computer { public double add(double a,double b) { return a+b; } public double jian(double a,double b) { return a-b; } public double chen(double a,double b) { return a*b; } public double chu(double a,double b) { return a/b; } }
|
- 编写一个用户注册程序。程序中,提示用户从键盘输入用户名(userName)和密码(password),要求用户名只能包含26个英文字母,大小写均可,而密码只能是0~9的数字组成,且用户名不能重复。判断用户输入的用户名(userName)和密码(password)是否符合要求,如果符合要求,则使用I/O流存入文件;否则,则提示用户重新输入。
package EXception; import java.io.*;
public class Exception5 {
public static void main(String[] args) throws IOException { // TODO Auto-generated method stub BufferedReader inn = new BufferedReader (new InputStreamReader(System.in));
while(true) { System.out.println("用户名:"); String name = inn.readLine(); System.out.println("密码:"); String key = inn.readLine(); boolean isname = name.matches("[a-zA-Z]+"); boolean iskey = key.matches("[0-9]*");
if(isname && iskey) { boolean iszc=true;
FileReader outOne=new FileReader("D:\\eclipse-workspace\\user.txt"); BufferedReader outTwo=new BufferedReader(outOne); String user; while((user= outTwo.readLine()) !=null ) { String oldname[] = user.split(" "); if(name.equals(oldname[0])) { iszc=false; break; } } outOne.close(); outTwo.close();
if(iszc==true) { BufferedWriter inTwo =new BufferedWriter(new FileWriter("D:\\eclipse-workspace\\user.txt")); inTwo.newLine(); inTwo.write(name+" "+key); System.out.println("注册成功!"); inTwo.close(); break; } else System.out.println("已存在该用户名!请重新设置用户名");
} else System.out.println("输入的用户名或密码格式错误,请重新输入!");
}
inn.close(); } } |
- 选择两个城市(桂林、昆明)作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。
Runnable 接口
package EXception;
public class Exception6 {
public static void main(String[] args) { // TODO Auto-generated method stub cityone cityOne,cityTwo; cityOne = new cityone("桂林"); cityTwo = new cityone("昆明"); cityOne.start(); cityTwo.start(); }
} ------------------------------------------------------------------------------------------------------- package EXception;
public class cityone extends Thread{ cityone(String s){ setName(s); } public void run(){ for(int i=1;i<=10;i++) { System.out.println("第"+i+"次"+getName()); try { Thread.sleep((int)(Math.random()*1000)); }catch(InterruptedException e) { } } System.out.println("我想去"+getName()+"旅游"); System.exit(0); } }
|
Thread类 package EXception;
public class Exctption6_1 {
public static void main(String[] args) { // TODO Auto-generated method stub CityTwo city=new CityTwo(); Thread cityone,citytwo; cityone = new Thread(city); cityone.setName("桂林"); citytwo = new Thread(city); citytwo.setName("昆明"); cityone.start(); citytwo.start();
}
}
-------------------------------------------------------------------------------------------------------
package EXception;
public class CityTwo implements Runnable{ public void run() { String city = Thread.currentThread().getName(); for(int i=1;i<=10;i++) { System.out.println("第"+i+"次"+city); try { Thread.sleep((int)(Math.random()*1000)); }catch(InterruptedException e) { } } System.out.println("我想去"+city+"旅游"); System.exit(0); } }
|
- 基于以上用户注册程序,编写用户登录程序。程序中,提示用户从键盘输入用户名(userName)和密码(password)进行登录。考虑以下3种情况:
- 如果用户名(userName)和密码(password)与文件中与保存的用户名和密码匹配,则登录成功;
- 如果用户名(userName)错误,则提示用户“输入的用户名不存在,为未注册用户。”;
- 定义密码错误的异常类:WrongPasswordException.java。如果用户名与文件中与保存的用户名匹配,而密码错误,就用throw抛出WrongPasswordException异常,并提示用户重新输入密码。用户有3次重新输入密码的机会,如果3次不成功,则程序终止。使用for循环和try-catch来控制密码重新输入的流程。
package EXception; import java.io.*; public class Exception7 {
public static void main(String[] args) throws IOException { // TODO Auto-generated method stub
BufferedReader inn = new BufferedReader(new InputStreamReader(System.in)); String user;
for(int i=3;i>0;i--) { boolean iscz=false; try { System.out.println("用户名:"); String usename = inn.readLine(); System.out.println("密码:"); String password = inn.readLine();
FileReader one = new FileReader("D:\\eclipse-workspace\\user.txt"); BufferedReader Two = new BufferedReader(one);
while((user = Two.readLine())!=null) {
String name[]=user.split(" "); if(usename.equals(name[0]) && password.equals(name[1])) { System.out.println("登陆成功"); iscz=true; break; } else if(usename.equals(name[0]) && !password.equals(name[1])) { iscz=true; System.out.println("密码错误"); throw new WrongPasswordException(i);
} } one.close(); Two.close(); if(iscz==false) { System.out.println("用户名不存在"); }
}catch(WrongPasswordException e) { System.out.println(e.toString()); } } inn.close();
}
} -------------------------------------------------------------------------------------------------------
package EXception;
public class WrongPasswordException extends Exception{ String message; public WrongPasswordException(int m) { message="还有"+m+"次机会"; } public String toString() { return message; } }
|
判断和选择
- 每个章节后面的习题必须掌握。(第1、2、3、4、5、6、9、12、15章)
判断题
( T ) Java中所有的I/O都是通过流来实现的。
( T ) package语句必须放到java程序的最开始 。
选择题
整型数据类型中,需要内存空间最多的是( B )。
A. short
B. long
C. int
D. byte
给出下面代码,关于该程序以下哪个说法是正确的?( C )
public class Person{
static int arr[] = new int[5];
public static void main(String a[]) { System.out.println(arr[0]);} }
}
A. 编译时将产生错误
B. 编译时正确,运行时将产生错误
C. 输出零
D. 输出空