java期末考试
第三章
60_1
一
此方法为先定好最大值和最小值 且int类型下无整数再大于或小于他们 也就是默认初始最大最小值为int类型的临界值
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int max =-999999999;
int min=999999999;
int w =1;
while (w!=0){
System.out.print("请输入一个整数(输入0结束):");
w =s.nextInt();
if(w==0){break;}
if(w<min){min=w;}
if(w>max){max=w;}
}
System.out.print("最大值是:"+max+" ");
System.out.print("最小值是:"+min);
}
二
此方法把值暂设为第一次录入的值
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请输入一个整数(输入0结束):");
int max;
int min;
int num;
min=max=num = s.nextInt();
while (num != 0) {
if (num < min) {
min = num;
}
if (num > max) {
max = num;
}
System.out.println("请输入一个整数(输入0结束):");
num = s.nextInt();
}
System.out.println("最大值max=" + max + " 最小值min=" + min);
}
60_2
public static void main(String[] args) {
Scanner s =new Scanner(System.in);
int w;
String a = null;
do {
System.out.print("请输入数字1~7(输入0时结束):");
w =s.nextInt();
if(w==1|w==2|w==3|w==4|w==5|w==6|w==7){
switch( w){
case 1:
a="MON";
break;
case 2:
a="TUE";
break;
case 3:
a="WED";
break;
case 4:
a="THU";
break;
case 5:
a="FRI";
break;
case 6:
a="SAT";
break;
case 7:
a="SUN";
break;
}
System.out.println(a);
}else if(w==0){
System.out.print("程序结束!");
}else{
System.out.println();
}
} while (w!=0);
}
60_3
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s =new Scanner(System.in);
int i ;
int c ;
int jipiao1=5000;
double jipiao2 = 0;
double toudengcang = 0;
double jingjicang = 0;
System.out.println("请输入您出行的年份:1~12");
i=s.nextInt();
System.out.println("请问您选择头等舱还是经济舱?头等舱输入1,经济舱输入2");
c=s.nextInt();
if(i==4|i==5|i==6|i==7|i==8|i==9|i==10){
toudengcang=0.9;
jingjicang=0.6;
}else if(i==3|i==2|i==1|i==11|i==12){
toudengcang=0.5;
jingjicang=0.4;
}
if (c==1) {
jipiao2=jipiao1*toudengcang;
}else if (c==2){
jipiao2=jipiao1*jingjicang;
}
System.out.print("您的机票价格为:"+jipiao2);
}
第四章
82_1
public static void main(String[] args) {
// TODO Auto-generated method stub
String [] s =new String [5];
Scanner sc =new Scanner(System.in);
System.out.println("输入5句话:");
for(int i=0;i<s.length;i++){
System.out.print("第"+(i+1)+"句话:");
s[i]=sc.next();
}
System.out.println("逆序输出5句话:");
for(int d=4;d>=0;d--){
System.out.println(s[d]);
}
}
82_2
public static void main(String[] args) {
int [] array =new int[]{1,3,-1,5,2};
System.out.println("原数组:");
System.out.println(Arrays.toString(array));
int [] newArray =new int[5];
for(int x =0;x<array.length-1;x++){
for(int y =0;y<array.length-x-1;y++){
int ms ;
if(array[y]>array[y+1]){
ms=array[y+1];
array[y+1]=array[y];
array[y]=ms;
}
}
}
for(int z=0;z<newArray.length;z++){
newArray[z]=array[z];
}
System.out.println("排序后数组:");
System.out.println(Arrays.toString(newArray));
}
82_3
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int z[]=new int[10];
int z1=0;
int z2=0;
int z3=0;
int z4=0;
System.out.println("请输入10个数");
for(int x=0;x<z.length;x++){
z[x]=sc.nextInt();
if(z[x]==1){z1+=1; }else
if(z[x]==2){z2+=1; }else
if(z[x]==3){z3+=1; }else {
z4+=1;
}
}
System.out.print("输入的10个数为:");
for(int y:z){ System.out.println(y);}
System.out.println("数字1的个数"+z1);
System.out.println("数字2的个数"+z2);
System.out.println("数字3的个数"+z3);
System.out.println("非法数字的个数"+z4);
}
第六章
125_2
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double loan;
int yearchoice;
System.out.print("请输入贷款金额:");
loan = sc.nextInt();
System.out.print("请选择贷款年限: 1.3年(36个月) 2.5年(60个月) 3.20年(240个月)");
yearchoice =sc.nextInt();
System.out.println();
System.out.println();
System.out.println();
System.out.print("***月供为:"+loan(loan,yearchoice));
}
public static double loan(double loan,int yearchoice){
double lilv=0;
int year=0;
if(yearchoice==1){ lilv =0.0603;
year=3;}
if(yearchoice==2){ lilv =0.0612;
year=5;}
if(yearchoice==3){ lilv =0.0639;
year=20;}
double myhk= (loan +(loan*lilv))/(year*12);
return myhk;
}
125_3
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String d = "y";
do {
System.out.print("请输入第一条边:");
int a = sc.nextInt();
System.out.print("请输入第二条边:");
int b = sc.nextInt();
System.out.print("请输入第三条边:");
int c = sc.nextInt();
if (isTriangle(a, b, c)==true) {
System.out.println("这是一个" + shape(a, b, c));
System.out.print("继续吗?(y/n):");
d = sc.next();
} else {
System.out.println("这不能构成三角形");
System.out.print("继续吗?(y/n):");
d = sc.next();
}
if (d.equals("n")) {
break;
}
} while (d != "n");
System.out.print("谢谢使用!");
}
public static boolean isTriangle(int a, int b, int c) {
if (a + b > c & a + c > b & b + c > a) {
boolean flag = true;
return flag;
} else {
boolean flag = false;
return flag;
}
}
public static String shape(int a, int b, int c) {
String shape = " ";
if (a * a == b * b + c * c | b * b == a * a + c * c | c * c == b * b + a * a) {
shape = "直角三角形";
return shape;
} else if (a == b & b == c) {
shape = "等边三角形";
return shape;
} else if (a * a > b * b + c * c | b * b > a * a + c * c | c * c > b * b + a * a) {
shape = "钝角三角形";
return shape;
} else {
shape = "锐角三角形";
return shape;
}
}
第七章
145_2
public static void main(String [] args){
Bird bird =new Bird("一只红色的", 4, "鸟");
Fish fish =new Fish("一只5斤重的", 2, "鱼");
}
}
class Animal {
public void printInfo(String xiushifu, int age, String wuzhong) {
System.out.println("我是" + xiushifu + wuzhong + "!");
System.out.println("我今年" + age + "岁了!");
}
}
class Bird extends Animal {
private String xiushifu;
private int age;
private String wuzhong;
public Bird(String xiushifu, int age, String wuzhong) {
super.printInfo(xiushifu, age, wuzhong);
this.age=age;
this.wuzhong=wuzhong;
this.xiushifu=xiushifu;
}
}
class Fish extends Animal {
private String xiushifu;
private int age;
private String wuzhong;
public Fish(String xiushifu, int age, String wuzhong) {
super.printInfo(xiushifu, age, wuzhong);
this.age=age;
this.wuzhong=wuzhong;
this.xiushifu=xiushifu;
}
}
145_3
public static void main(String[] args) {
// TODO Auto-generated method stub
Phones i = new IPhone();
Phones a = new APhone();
Phones w = new WPhone();
i.call();
a.call();
w.call();
}
}
class Phones{
public void call(){};
}
class IPhone extends Phones{
public void call(){
System.out.println("IPhone通话中");
};
}
class APhone extends Phones{
public void call(){
System.out.println("APhone通话中");
};
}
class WPhone extends Phones{
public void call(){
System.out.println("WPhone通话中");
};