Java语言程序设计与数据结构(基础篇) 课后题练习 第三章

第三章

3.1

import java.util.Scanner;
public class Test3{
	public static void main(String[] args){
		System.out.println("Enter a, b, c:");
		Scanner input = new Scanner(System.in);
		Double a = input.nextDouble();
		Double b = input.nextDouble();
		Double c = input.nextDouble();
		Double s = b * b - 4 * (a * c);
		if(s > 0){
			double r1 = (-b + Math.pow(s,0.5))/(2*a);
			double r2 = (-b - Math.pow(s,0.5))/(2*a);
			System.out.println("The equation has two roots" + " " + r1 + " " + "and" + " " + r2);
		}
		else if(s == 0){
			double r = -b/(2*a);
			System.out.println("The equation has one roots" + " " + r);
		}
		else{
			System.out.println("The equation has no real roots");
		}
	} 
}

3.2

import java.util.Scanner;
public class Test3{
	public static void main(String[] args){
		int number1 = (int)(Math.random()*10);
		int number2 = (int)(Math.random()*10);
		int number3 = (int)(Math.random()*10);
		Scanner input = new Scanner(System.in);
		System.out.print("What is" + " " + number1 + "+" + number2 + "+" +number3 + "?");
		int sum = input.nextInt();
		boolean answer = (number1+number2+number3)==sum;
		System.out.println(number1 + "+" + number2 + "+" + number3 + " is" + " " +answer);
	} 
}

3.3

3.4

import java.util.Scanner;
public class Test3{
	public static void main(String[] args){
		int month = (int)(Math.random()*12 + 1);
		switch(month){
		case 1 : System.out.println("January");break;
		case 2 : System.out.println("Februay");break;
		case 3 : System.out.println("March");break;
		case 4 : System.out.println("Apirl");break;
		case 5 : System.out.println("May");break;
		case 6 : System.out.println("Jun");break;
		case 7 : System.out.println("July");break;
		case 8 : System.out.println("August");break;
		case 9 : System.out.println("September");break;
		case 10 : System.out.println("October");break;
		case 11 : System.out.println("Novwember");break;
		case 12 : System.out.println("December");}
	} 
}

3.5

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

3.6

import java.util.Scanner;
public class Test3{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("Enter weight in pounds:");
		double weight = input.nextDouble();
		System.out.println("Enter the feet:");
		double feet = input.nextDouble();
		System.out.println("Enter inches:");
		double inches = input.nextDouble();
		inches  = inches + 12*feet;
		double BMI = ((weight * 0.45359237)/((inches * 0.0254)*(inches * 0.0254)));
		
		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");
	} 
}

3.8

import  java.util.Scanner;
public class Test3{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("Enter 3 numbers:");
		int num1 = input.nextInt();
		int num2 = input.nextInt();
		int num3 = input.nextInt();
		if(num1>num2){
			int temp = num1;
			num1 = num2;
			num2 = temp;
		}
		if(num2>num3){
			int temp = num2;
			num2 = num3;
			num3 = temp;
		}
		if(num1>num3){
			int temp = num1;
			num1 = num3;
			num3 = temp;
		}
		System.out.println(num1 + " " + num2 + " " + num3);
	}
}

3.9

在这里插入代码片
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值