Java程序设计总复习题
1、编写一个Java程序,用if-else语句判断某年份是否为闰年。<;分支>
// Programme Name LeapYear.java
public class LeapYear{
public static void main<String args[]>{
int year=2010;
if<args.length!=0>
year=Integer.parseInt<args[0]>;
if<<year%4==0 && year%100!=0>||<year%400==0>>
System.out.println<year+" 年是闰年。">;
else
System.out.println<year+" 年不是闰年。">;
}
}//if-else语句
2、编写一个Java程序在屏幕上输出1!+2!+3!+……+10!的和。〔循环
// programme name ForTest.java
public class ForTest {
public static void main< String args[] > {
int i,j,mul,sum=0;
java经典上机编程题for<i=1;i<=10;i++> {
mul=1;
for<j=1,j<=i;j++> {
mul=mul*j;
}
sum=sum+mul;
}
System.out.println<"1!+2!+3!+……+10!= "+sum>;
}
}
3、依次输入10个学生成绩,判断学生〔优秀、良好、中等、及格、不及格并计算人数〔switch
4、使用冒泡排序〔数组
publicclass BubbleSort {
publicstaticvoid main<String[] args> {
int[] array={63,4,24,1,3,5};
BubbleSort sorter=new BubbleSort<>;
sorter.sort<array>;
}
//冒泡排序
publicvoid sort<int[] array>{
for<int i=1;i<array.length;i++>
for<int j=0;j<array.length-1;j++>{
if<array[j]>array[j+1]>{
int temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
showArray<array>;
}
//遍历数组,并输出数组的元素。
publicvoid showArray<int[] array>{false god
for<int i=0;i<array.length;i++>{
System.out.print<array[i]+"\t">;
}
System.out.println<>;
}
}
5、实现会员注册,要求用户名长度不小于3,密码长度不小于6,注册时两次输入密码必须相
同〔字符串
import java.util.Scanner;
publicclass Register {
String name;
String password;
String newPassword;
///////////
publicvoid nameExe<>{
Scanner input=new Scanner<System.in>;
System.out.println<"请输入用户名,密码和验证密码">;
System.out.print<"用户名:">;
<>;
System.out.print<"密码:">;
<>;
System.out.print<"验证密码:">;
4的二进制怎么算plc中的浮点数是什么意思<>;
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<"用户名:">;this is going to hurt
<>;
System.out.print<"密码:">;
<>;
System.out.print<"验证密码:">;
<>;
}
System.out.println<"注册成功!">;
}
}
publicclass Verify {
publicstaticvoid main<String[] args> {
Register m1=new Register<>;
m1.nameExe<>;
}
}
6、一个景区根据游人的年龄收取不同价格的门票。请编写游人类,根据年龄段决定能够购
买的门票价格并输出,然后写出测试类测试该类〔类的基本实现
publicclass Tourist {
int age;
int ticketPrice;
publicvoid setAge<int age>{
this.age=age;
}
publicvoid ticket<>{
if<age>0&&age<12>
ticketPrice=20;
elseif<age<20>
ticketPrice=40;
elseif<age<50>
ticketPrice=80;
else
ticketPrice=35;
System.out.println<"门票价格:"+ticketPrice>;
}
}/////
import java.util.Scanner;
publicclass Test {
publicstaticvoid main<String[] args> {
Scanner input=new Scanner<System.in>;
Tourist t1=new Tourist<>;
System.out.print<"请输入年龄:">;
t1.setAge&Int<>>;
t1.ticket<>;
}
}
7、〔1编写一个圆类Circle,该类拥有:
①一个成员变量
Radius〔私有,浮点型;// 存放圆的半径;
②两个构造方法
Circle< > // 将半径设为0
Circle<double r > //创建Circle对象时将半径初始化为r
③三个成员方法
double getArea< > //获取圆的面积
double getPerimeter< > //获取圆的周长
void show< > //将圆的半径、周长、面积输出到屏幕
〔2编写一个圆柱体类Cylinder,它继承于上面的Circle类。还拥有:
①一个成员变量
double hight〔私有,浮点型;// 圆柱体的高;
②构造方法
Cylinder <double r, double h > //创建Circle对象时将半径初始化为r
③成员方法
double getVolume< > //获取圆柱体的体积
void showVolume< > //将圆柱体的体积输出到屏幕编写应用程序,创建类的对象,分别设置圆的半径、圆柱体的高,计算并分别显示圆半径、
圆面积、圆周长,圆柱体的体积。
//Programme Name TestCylinder.java
class Circle { //定义父类--园类
private double radius; //成员变量--园半径
Circle<> { //构造方法
radius=0.0;
}
Circle<double r> { //构造方法
radius=r;
}
double getPerimeter<> { //成员方法--求园周长
return 2*Math.PI*radius;
}
double getArea<> { //成员方法--求园面积
return Math.PI*radius*radius;
}
void disp<> { //成员方法--显示园半径、周长、面积System.out.println<"圆半径="+radius>;
System.out.println<"圆周长="+getPerimeter<>>;
System.out.println<"圆面积="+getArea<>>;
}
}
class Cylinder extends Circle { //定义子类--圆柱类
private double hight; //成员变量--园柱高
滑块有限元Cylinder<double r,double h> { //构造方法
super<r>;
hight=h;
}
public double getV ol<> { //成员方法--求园柱体积
return getArea<>*hight;
}
public void dispV ol<> { //成员方法--显示园柱体积
System.out.println<"圆柱体积="+getV ol<>>;
}
}
public class TestCylinder { //定义主类
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论