System.out.print(array[i]+"\t");
}
System.out.println();
}
}
5、实现会员注册,要求用户名长度不小于3,密码长度不小于6,注册时两次输入密码必须相同(字符串)
importjava.util.Scanner;
publicclassRegister{
Stringname;
Stringpassword;
StringnewPassword;
///////////
publicvoidnameExe(){
Scannerinput=newScanner(System.in);
System.out.println("请输入用户名,密码和验证密码");
System.out.print("用户名:
");
name=input.next();
System.out.print("密码:
");
password=input.next();
System.out.print("验证密码:
");
newPassword=input.next();
while(name.length()<3||(password.equals(newPassword)==false)
||(password.length()<6)){
if(name.length()<3){
System.out.println("用户名不能小于3");
}
if((password.equals(newPassword)==false)||password.length()<6){
System.out.println("两次输入密码不一样或密码不能小于6位");
}
System.out.println("\n"+"请重新输入");
System.out.print("用户名:
");
name=input.next();
System.out.print("密码:
");
password=input.next();
System.out.print("验证密码:
");
newPassword=input.next();
}
System.out.println("注册成功!
");
}
}
publicclassVerify{
publicstaticvoidmain(String[]args){
Registerm1=newRegister();
m1.nameExe();
}
}
6、一个景区根据游人的年龄收取不同价格的门票。
请编写游人类,根据年龄段决定能够购买的门票价格并输出,然后写出测试类测试该类(类的基本实现)
publicclassTourist{
intage;
intticketPrice;
publicvoidsetAge(intage){
this.age=age;
}
publicvoidticket(){
if(age>0&&age<12)
ticketPrice=20;
elseif(age<20)
ticketPrice=40;
elseif(age<50)
ticketPrice=80;
else
ticketPrice=35;
System.out.println("门票价格:
"+ticketPrice);
}
}/////
importjava.util.Scanner;
publicclassTest{
publicstaticvoidmain(String[]args){
Scannerinput=newScanner(System.in);
Touristt1=newTourist();
System.out.print("请输入年龄:
");
t1.setAge(input.nextInt());
t1.ticket();
}
}
7、
(1)编写一个圆类Circle,该类拥有:
①一个成员变量
Radius(私有,浮点型);//存放圆的半径;
②两个构造方法
Circle()//将半径设为0
Circle(doubler)//创建Circle对象时将半径初始化为r
③三个成员方法
doublegetArea()//获取圆的面积
doublegetPerimeter()//获取圆的周长
voidshow()//将圆的半径、周长、面积输出到屏幕
(2)编写一个圆柱体类Cylinder,它继承于上面的Circle类。
还拥有:
①一个成员变量
doublehight(私有,浮点型);//圆柱体的高;
②构造方法
Cylinder(doubler,doubleh)//创建Circle对象时将半径初始化为r
③成员方法
doublegetVolume()//获取圆柱体的体积
voidshowVolume()//将圆柱体的体积输出到屏幕
编写应用程序,创建类的对象,分别设置圆的半径、圆柱体的高,计算并分别显示圆半径、圆面积、圆周长,圆柱体的体积。
//ProgrammeNameTestCylinder.java
classCircle{//定义父类--园类
privatedoubleradius;//成员变量--园半径
Circle(){//构造方法
radius=0.0;
}
Circle(doubler){//构造方法
radius=r;
}
doublegetPerimeter(){//成员方法--求园周长
return2*Math.PI*radius;
}
doublegetArea(){//成员方法--求园面积
returnMath.PI*radius*radius;
}
voiddisp(){//成员方法--显示园半径、周长、面积
System.out.println("圆半径="+radius);
System.out.println("圆周长="+getPerimeter());
System.out.println("圆面积="+getArea());
}
}
classCylinderextendsCircle{//定义子类--圆柱类
privatedoublehight;//成员变量--园柱高
Cylinder(doubler,doubleh){//构造方法
super(r);
hight=h;
}
publicdoublegetVol(){//成员方法--求园柱体积
returngetArea()*hight;
}
publicvoiddispVol(){//成员方法--显示园柱体积
System.out.println("圆柱体积="+getVol());
}
}
publicclassTestCylinder{//定义主类
publicstaticvoidmain(String[]args){//主程入口
CircleCi=newCircle(10.0);//生成园类实例
Ci.disp();//调用园类的方法
CylinderCyl=newCylinder(5.0,10.0);//生成圆柱类实例
Cyl.disp();//调用父类方法
Cyl.dispVol();//调用子类方法
}
}
8、编写一个Java应用程序,从键盘读取用户输入两个字符串,并重载3个函数分别实现这两个字符串的拼接、整数相加和浮点数相加。
要进行异常处理,对输入的不符合要求的字符串提示给用户,不能使程序崩溃。
(异常处理)
//programmenameStrinput.java
importjava.io.*;
publicclassStrinput
{
publicstaticvoidmain(Stringargs[]){
Strings1=null,s2=null,ss,si,sf;