Java语言程序设计基础篇编程练习题-第三章

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

import java.util.Scanner;

public class Test3_01 {
	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 criterion = b * b - 4 * a * c;
		
		if(criterion > 0) {
			double root1 = (-b + Math.pow(criterion, 0.5)) / (2 * a);
			double root2 = (-b - Math.pow(criterion, 0.5)) / (2 * a);
			System.out.println("The equation has two roots " + root1 + " and " + root2);
		}else if(criterion == 0) {
			double root = (-b + Math.pow(criterion, 0.5)) / (2 * a);
			System.out.println("The equation has one root " + root);
		}else {
			System.out.println("The equation has no real roots");
		}
		
	}
}

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

import java.util.Scanner;

public class Test3_02 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int result = 0;
		double num = Math.random();
		if(num < 0.5) {
			//产生两个数
			int num1 = (int)(Math.random() * 10);
			int num2 = (int)(Math.random() * 10);
			System.out.print("Enter the sum of (" + num1 + " + " + num2 + ") :");
			result = num1 + num2;
		}else {
			//产生三个数
			int num1 = (int)(Math.random() * 10);
			int num2 = (int)(Math.random() * 10);
			int num3 = (int)(Math.random() * 10);
			System.out.print("Enter the sum of (" + num1 + " + " + num2 + " + " + num3 + ") :");
			result = num1 + num2 + num3;
		}
		int sum = input.nextInt();
		input.close();
		if(sum == result) {
			System.out.println("Yes");
		}else {
			System.out.println("No");
		}
	}
}

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

import java.util.Scanner;

public class Test3_03 {
	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 criterion = a * d - b * c;
		
		if(criterion == 0) {
			System.out.println("The equation has no solution");
		}else {
			double x = (e * d - b * f) / (a * d - b * c);
			double y = (a * f - e * c) / (a * d - b * c);
			System.out.println("x is " + x + " and y is " + y);
		}
	}
}

3.4(随机月份)

import java.util.Scanner;

public class Test3_04 {
	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("February");
			break;
		case 3:
			System.out.println("March");
			break;
		case 4:
			System.out.println("April");
			break;
		case 5:
			System.out.println("May");
			break;
		case 6:
			System.out.println("June");
			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("November");
			break;
		case 12:
			System.out.println("December");
			break;
		default:
			System.out.println(month);
		}
	}
}

3.5(找到将来的日期)

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

3.6(医疗应用程序:BMI)

import java.util.Scanner;
public class Test3_06 {
	public static void main(String[] args) {
		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: ");//英寸    1英尺=12英寸
		double inches = input.nextDouble();
		input.close();
		
		inches += feet * 12;
		
		final double KILOGRAMS_PER_POUND = 0.45359237;
		final double METERS_PER_INCH = 0.0254;
		
		double weightInKilograms = weight * KILOGRAMS_PER_POUND;
		double heightInMeters = inches * METERS_PER_INCH;
		
		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(财务应用程序,整钱兑零)

import java.util.Scanner;
public class Test3_07 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter an amount in double, for example 11.56: ");
		double amount = input.nextDouble();
		input.close();
		
		int remainingAmount = (int)(amount * 100);
		
		int numberOfOneDollars = remainingAmount / 100;
		remainingAmount = remainingAmount % 100;
		
		int numberOfQuarters = remainingAmount / 25;
		remainingAmount = remainingAmount % 25;
		
		int numberOfDimes = remainingAmount / 10;
		remainingAmount = remainingAmount % 10;
		
		int numberOfNickels = remainingAmount / 5;
		remainingAmount = remainingAmount % 5;
		
		int numberOfPennies = remainingAmount;
		
		System.out.println("Your amount " + amount + " consists of");
		if(numberOfOneDollars <= 1) {
			System.out.println("    " + numberOfOneDollars + " dollar");
		}else {
			System.out.println("    " + numberOfOneDollars + " dollars");
		}
		if(numberOfQuarters <= 1) {
			System.out.println("    " + numberOfQuarters + " quarter ");
		}else {
			System.out.println("    " + numberOfQuarters + " quarters ");
		}
		if(numberOfDimes <= 1) {
			System.out.println("    " + numberOfDimes + " dime");
		}else {
			System.out.println("    " + numberOfDimes + " dimes");
		}
		if(numberOfNickels <= 1) {
			System.out.println("    " + numberOfNickels + " nickel");
		}else {
			System.out.println("    " + numberOfNickels + " nickels");
		}
		if(numberOfPennies <= 1) {
			System.out.println("    " + numberOfPennies + " pennie");
		}else {
			System.out.println("    " + numberOfPennies + " pennies");
		}
		
	}
}

3.8(对三个整数排序)

import java.util.Scanner;
 
public class Test3_08 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter a, b, c: ");
		int a = input.nextInt();
		int b = input.nextInt();
		int c = input.nextInt();
		input.close();
		
		if(a > b) {
			int t = a;
			a = b;
			b = t;
		}
		if(a > c) {
			int t = a;
			a = c;
			c = t;
		}
		if(b > c) {
			int t = b;
			b = c;
			c = t;
		}
		
		System.out.println(a + ", " + b + ", " + c);
	}
}

3.9(商业,检查ISBN-10)

import java.util.Scanner;
 
public class Test3_09 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the first 9 digits of an ISBN as integer: ");
		String str = input.nextLine();
		input.close();
		
		int sum = 0;
		for(int i = 0; i < str.length(); i++) {
			sum += (int)(str.charAt(i) - '0') * (i + 1);
		}
		int endNumber = sum % 11;
		String s = "";
		switch(endNumber) {
		case 0:
			s = "0";
			break;
		case 1:
			s = "1";
			break;
		case 2:
			s = "2";
			break;
		case 3:
			s = "3";
			break;
		case 4:
			s = "4";
			break;
		case 5:
			s = "5";
			break;
		case 6:
			s = "6";
			break;
		case 7:
			s = "7";
			break;
		case 8:
			s = "8";
			break;
		case 9:
			s = "9";
			break;
		case 10:
			s = "X";
			break;
		default:
			break;
		}
		str += s;
		System.out.println("The ISBN-10 number is " + str);
	}
}

3.10(游戏:加法检测)

import java.util.Scanner;
 
public class Test3_10 {
	public static void main(String[] args) {
 
		int number1 = (int)(Math.random() * 100);
		int number2 = (int)(Math.random() * 100);
 
		System.out.print("What is " + number1 + " + " + number2 + " ? ");
		
		Scanner input = new Scanner(System.in);
		int answer = input.nextInt();
		input.close();
		
		if(answer == number1 + number2) {
			System.out.println("You are correct!");
		}else {
			System.out.println("Your answer is wrong.");
			System.out.println(number1 + " + " + number2 + " should be " + (number1 + number2));
		}
	
	}
}

3.11()

如果放在第三章,单纯使用选择语句,可以使用示例代码1。但是这种题目是有使用数组解决的方法,如示例代码2所示。

// 示例代码1
import java.util.Scanner;
 
public class Test3_11 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter number of years: ");
        int years = input.nextInt();
        System.out.print("Enter number of months: ");
        int months = input.nextInt();
        input.close();
        
        boolean falg = (years % 4 == 0 && years % 100 != 0) || years % 400 == 0;
        int days = 0;
        String str = "";
        
        switch(months) {
        case 1:
            str = "January";
            days = 31;
            break;
        case 2:
            str = ("February");
            if(falg) {
                days = 29;
            }else {
                days = 28;
            }
            break;
        case 3:
            str = ("March");
            days = 31;
            break;
        case 4:
            str = ("April");
            days = 30;
            break;
        case 5:
            str = ("May");
            days = 31;
            break;
        case 6:
            str = ("June");
            days = 30;
            break;
        case 7:
            str = ("July");
            days = 31;
            break;
        case 8:
            str = ("August");
            days = 31;
            break;
        case 9:
            str = ("September");
            days = 30;
            break;
        case 10:
            str = ("October");
            days = 31;
            break;
        case 11:
            str = ("November");
            days = 30;
            break;
        case 12:
            str = ("December");
            days = 31;
            break;
        default:
            System.out.println(months);
        }
        
        System.out.println(str + " " + years + " has " + days + " days");
    }
}
// 示例代码2
import java.util.Scanner;
 
public class TestMonth {
	public static void main(String[] args) {
		String[] month = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
		int[] days = {29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		Scanner input = new Scanner(System.in);
		System.out.print("Enter number of years: ");
		int years = input.nextInt();
		System.out.print("Enter number of months: ");
		int months = input.nextInt();
		input.close();
		
		int theDays = 0;
		boolean falg = (years % 4 == 0 && years % 100 != 0) || years % 400 == 0;
		if (months == 2) {
			if (falg) {
				theDays = days[0];
			} else {
				theDays = days[2];
			}
		} else {
			theDays = days[months];
		}
		System.out.println(month[months] + " " + years  + " has " + theDays + " days");
	}
}

3.12(回文数字)

import java.util.Scanner;
 
public class Test3_12 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter a three-digit integer: ");
		int number = input.nextInt();
		input.close();
		
		//判断是否是回文数字
		int num1 = number % 10;
		int num2 = number / 100;
		if(num1 == num2) {
			System.out.println(number + " is a palindrome");
		}else {
			System.out.println(number + " is not a palindrome");
		}
	}
}

3.13(财务应用程序,计算税款)

import java.util.Scanner;
 
public class Test3_13 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("(0-single filer, 1-married jointly or qualifying widow(er),2-married separateiy, 3-head of household)");
		System.out.print("Enter the filing status: ");
		int status = input.nextInt();
		System.out.print("Enter the taxable income: ");
		double income = input.nextDouble();
		input.close();
		
		double tax = 0;
		
		if(status == 0) {
			if(income <= 8350) {
				tax = income * 0.10;
			}else if(income <= 33950) {
				tax = 8350 * 0.10 + (income - 8350) * 0.15;
			}else if(income <= 82250) {
				tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
			}else if(income <= 171550) {
				tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (income - 82250) * 0.28;
			}else if(income <= 372590) {
				tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28
						+ (income - 171550) * 0.33;
			}else {
				tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28
						+ (372950 - 171550) * 0.33 + (income - 372950) * 0.35;
			}
		}else if(status == 1) {
			if(income <= 16700) {
				tax = income * 0.10;
			}else if(income <= 67900) {
				tax = 16700 * 0.10 + (income - 16700) * 0.15;
			}else if(income <= 137051) {
				tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (income - 67900) * 0.25;
			}else if(income <= 208850) {
				tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (income - 137050) * 0.28;
			}else if(income <= 372590) {
				tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28
						+ (income - 208850) * 0.33;
			}else {
				tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (171550 - 137050) * 0.28
						+ (372950 - 171550) * 0.33 + (income - 372950) * 0.35;
			}
		}else if(status ==2) {
			if(income <= 8350) {
				tax = income * 0.10;
			}else if(income <= 33950) {
				tax = 8350 * 0.10 + (income - 8350) * 0.15;
			}else if(income <= 68525) {
				tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
			}else if(income <= 104425) {
				tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (income - 68525) * 0.28;
			}else if(income <= 372590) {
				tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28
						+ (income - 104425) * 0.33;
			}else {
				tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28
						+ (186475 - 104425) * 0.33 + (income - 186475) * 0.35;
			}
		}else if(status ==3) {
			if(income <= 11950) {
				tax = income * 0.10;
			}else if(income <= 45500) {
				tax = 11950 * 0.10 + (income - 11950) * 0.15;
			}else if(income <= 82250) {
				tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (income - 45500) * 0.25;
			}else if(income <= 171550) {
				tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (income - 117450) * 0.28;
			}else if(income <= 372590) {
				tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (171550 - 117450) * 0.28
						+ (income - 171550) * 0.33;
			}else {
				tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (171550 - 117450) * 0.28
						+ (372950 - 171550) * 0.33 + (income - 372950) * 0.35;
			}
		}else {
			System.out.println("Error: invalid status");
			System.exit(1);
		}
		System.out.println("Tax is " + (int)(tax * 100) / 100.0);
	}
}

3.14(游戏,猜硬币正反面)

import java.util.Scanner;
 
public class Test3_14 {
	public static void main(String[] args) {
		int num = (int)(Math.random() * 2);
		Scanner input = new Scanner(System.in);
		System.out.print("Enter a number: ");
		int guess = input.nextInt();
		input.close();
		
		if(guess == num) {
			System.out.println("Yes");
		}else {
			System.out.println("No");
		}
	}
}

以上代码均为个人编写,不当之处,悉请指正。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值