JAVA语言程序设计(基础篇)第十版——第三章 选择 编程练习题(参考答案)

(3.2节+(3.8-3.16小节)+综合题)

3.2节

*3.1(代数:解一元二次方程)

import java.util.Scanner;

public class T1 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a, b, c: ");
		double a=input.nextDouble();
		double b=input.nextDouble();
		double c=input.nextDouble();
		input.close();
		
		double derta= Math.pow(b,2)-4*a*c ;//根的判别式
		
		double r1= ( -b + Math.pow( Math.pow(b,2) - 4*a*c , 0.5 ) ) / 2*a ;
		double r2=( -b - Math.pow( Math.pow(b,2) - 4*a*c , 0.5 ) ) / 2*a ;
		
		if( derta>0 )
			System.out.print("The equation has two roots:" + (float)r1 + " and " + (float)r2 );
		else if ( derta==0 )
		    
			System.out.print("The equation has one root " + (float)r1);
		else
			System.out.print("The equation has no real roots");
		
	}

}

 3.2(游戏:三个数的加法)

import java.util.Scanner;

public class T2 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		int random1=(int) (Math.random()*10);
		int random2=(int)(System.currentTimeMillis()%10);
		int random3=(int)(System.currentTimeMillis()/7%10);
		
		System.out.print(" 请输入这三个整数的和: ");
		int number=input.nextInt();
		input.close();
		
		int sum=random1 + random2 + random3;
		
		System.out.println(" 这三个随机数是 :"+ random1 + ", " + random2 + ", " + random3);
		if(sum==number)
			System.out.println("you are right!");
		else
			System.out.println("you are wrong!");
			
		System.out.println("答案是:"+ random1 + "+" + random2 + "+" + random3 + "=" + sum);
		
		
	}

}

*3.3(代数:求解2x2线性方程)

import java.util.Scanner;

public class T3 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a, b, c, d, e, f: ");
		double a=input.nextDouble();
		double b=input.nextDouble();
		double c=input.nextDouble();
		double d=input.nextDouble();
		double e=input.nextDouble();
		double f=input.nextDouble();
		input.close();
		
		double x=(e*d-b*f)/(a*d-b*c);
		double y=(a*f-e*c)/(a*d-b*c);
		
		if( (a*d-b*c)==0 )
			System.out.println(" The equation has no solution ");
		else
			System.out.println(" x is " + x + " and y is " + y ); 
		
		
	}

}

**3.4(随机月份)

public class T4 {
	public static void main(String[] args) {
		
		int random=(int) (Math.random()*12+1);
		
		if (random==1)
			System.out.println("random is " + random + " : January ");
		else if (random==2)
			System.out.println("random is " + random + " : February ");
		else if (random==3)
			System.out.println("random is " + random + " : March ");
		else if (random==4)
			System.out.println("random is " + random + " : April ");
		else if (random==5)
			System.out.println("random is " + random + " : May ");
		else if (random==6)
			System.out.println("random is " + random + " : June ");
		else if (random==7)
			System.out.println("random is " + random + " : July ");
		else if (random==8)
			System.out.println("random is " + random + " : August ");
		else if (random==9)
			System.out.println("random is " + random + " : September ");
		else if (random==10)
			System.out.println("random is " + random + " : October ");
		else if (random==11)
			System.out.println("random is " + random + " : November ");
		else if (random==12)
			System.out.println("random is " + random + " : December ");
		
	}
}

 

*3.5(找到将来的日期)

import java.util.Scanner;

public class T5 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter today's day (0-6):");
		int number=input.nextInt();
		
		System.out.print("Enter the number of days elapsed since today:");  
		int days=input.nextInt();
		input.close();
		
		String day = null;
		switch(number) {
		
		case 0:	day="Sunday";
				break;	
		case 1:	day="Monday";
				break;
		case 2:	day="Tuesday";
				break;
		case 3:	day="Wednesday";
				break;	
		case 4:	day="Thursday";
				break;	
		case 5:	day="Friday";
				break;	
		case 6:	day="Saturday";
				break;	
			
		}
		
		
		
		int afterDay=( number + (days%7) ) % 7 ;
		
		String futureDay = null;
		switch(afterDay) {
		case 0:	futureDay="Sunday";
				break;
		case 1:	futureDay="Monday";
				break;
		case 2:	futureDay="Tuesday";
				break;
		case 3:	futureDay="Wednesday";
				break;
		case 4:	futureDay="Thursday";
				break;
		case 5:	futureDay="Friday";
				break;
		case 6:	futureDay="Saturday";
				break;
			
		}
		
		
		System.out.print("Today is " + day + " and the future day is " +  futureDay );   
		
		
	}

}

*3.6(医疗应用程序:BMI)

import java.util.Scanner;

public class T6 {

	public static void main(String[] args) {
		//1英尺=0.3047999995367米
		//1英寸=0.0254米
		Scanner input=new Scanner(System.in);
		System.out.print("Enter weight in pounds: ");
		double weight=input.nextDouble();
		
		System.out.print("Enter feet: ");
		double feet=input.nextDouble();
		
		System.out.print("Enter inches: ");
		double inches=input.nextDouble();
		input.close();
		
		double weightInKilograms= weight*0.45359237 ;
		double heightInMeters= inches*0.0254 + feet*0.3047999995367 ;
		double bmi=weightInKilograms/(heightInMeters*heightInMeters);
		
		System.out.println("BMI is " + bmi);
		
		if(bmi<18.5)
			System.out.println("Underweight");
		else if(bmi<25)
			System.out.println("Normal");
		else if(bmi<30)
			System.out.println("Overweight");
		else 
			System.out.println("Obese");
		
	}

}

3.7(财务应用程序:整钱兑零)

略,暂时不会,看见钱头就大o(╥﹏╥)o,不想去想,。。。。。。(用IF选择语句去做)

*3.8(对三个整数排序)


                
  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值