Java语言程序设计(基础篇)(原书第10版) 第三章编程练习题

Java语言程序设计(基础篇)(原书第10版) 第三章编程练习题

心血来潮补一下之前没做完的课后题,争取全部做完,欢迎大家评论指正。

在这里插入图片描述

需要书籍或者相关资料可以私聊!!!

3-1 代数:解一元二次方程

import java.util.Scanner;

public class Program3_1 {
   

	public static void main(String[] args) {
   
		// TODO Auto-generated method stub
		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();
		
		double m = b*b - 4*a*c;
		double n = -b / (2*a);
		if(m < 0)
			System.out.println("The equation has no real roots");
		else if(m == 0)
			System.out.printf("The equation has one root %.6f\n",n);
		else
			System.out.printf("The equation has two roots %.6f and %.6f\n",
					(n + Math.pow(m, 0.5) / (2*a)),(n - Math.pow(m, 0.5) / (2*a)));
	}

}

3-2 游戏:三个数的加法

import java.util.Scanner;

public class Program3_2 {
   
   public static void main(String[] args) {
   
       int number1 = (int)(System.currentTimeMillis() % 10);
       int number2 = (int)(System.currentTimeMillis() * 7 % 10);
       int number3 = (int)(System.currentTimeMillis() * 3 % 10);

       // Create a Scanner
       Scanner input = new Scanner(System.in);

       System.out.print(
               "What is " + number1 + " + " + number2 + " + " + number3 + "? ");

       int answer = input.nextInt();

       System.out.println(
               number1 + " + " + number2 + " + " + number3 + " = " + answer + " is " +
                       (number1 + number2 + number3 == answer));
   }
}

3-3 代数:求解2*2线性方程

import java.util.Scanner;

public class Program3_3 {
   

    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();

        double x1 = e*d - b*f, y1 = a*f - e*c, m = a * d - b * c;

        if(Math.abs(m) < 1e-6)
            System.out.println("The equation has no solution");
        else
            System.out.printf("x is %.1f and y is %.1f", x1/m, y1/m);
    }

}

3-4 随机月份

public class Program3_4 {
   

	public static void main(String[] args) {
   
		// TODO Auto-generated method stub
		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;
		
		}
	}
}

3-5 找到将来的日期

import java.util.Scanner;

public class Program3_5 {
   

    public static void main(String[] args) {
   
        String[] week = {
   "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

        Scanner input = new Scanner(System.in);

        System.out.print("Enter today's day: ");
        int today = input.nextInt();

        System.out.print("Enter he number of days elapsed since today: ");
        int number = input.nextInt();

        System.out.printf("Today is %s and the future day is %s", week[today], week[(today+number) % 7]);
    }

}

3-6 医疗应用程序:BMI

import java.util.Scanner;

public class Program3_6 {
   

    public static void main(String[] args) {
   
        Scanner input = new Scanner(System.in);

        // Prompt the user to enter weight in pounds
        System.out.print("Enter weight in pounds: ");
        double weight = input.nextDouble();

        System.out.print("Enter feet: ");
        double feet = input.nextDouble();

        // Prompt the user to enter height in inches
        System.out.print("Enter inches: ");
        double inches = input.nextDouble();

        double height = feet * 12 + inches;

        final double KILOGRAMS_PER_POUND = 0.45359237; // Constant
        final double METERS_PER_INCH = 0.0254; // Constant

        // Compute BMI
        double weightInKilograms = weight * KILOGRAMS_PER_POUND;
        double heightInMeters = height * METERS_PER_INCH;
        double bmi = weightInKilograms /
                (heightInMeters * heightInMeters);

        // Display result
        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 Program3_7 {
   
  public static void main(String[] args) {
      
    // Create a Scanner
    Scanner input = new Scanner(System.in);

    // Receive the amount 
    System.out.print(
      "Enter an amount in double, for example 11.56: ");
    double amount = input.nextDouble();

    int remainingAmount = (int)(amount * 100);

    // Find the number of one dollars
    int numberOfOneDollars = remainingAmount / 100;
    remainingAmount = remainingAmount % 100;

    // Find the number of quarters in the remaining amount
    int numberOfQuarters = remainingAmount / 25;
    remainingAmount = remainingAmount % 25;

    // Find the number of dimes in the remaining amount
    int numberOfDimes = remainingAmount / 10;
    remainingAmount = remainingAmount % 10;

    // Find the number of nickels in the remaining amount
    int numberOfNickels = remainingAmount / 5;
    remainingAmount = remainingAmount % 5;

    // Find the number of pennies in the remaining amount
    int numberOfPennies = remainingAmount;

    // Display results
    System.out.println("Your amount " + amount + " consists of");
    if(numberOfOneDollars > 1)
    	System.out.println("    " + numberOfOneDollars + " dollars");
    else if(numberOfOneDollars == 1)
    	System.out.println("    " + numberOfOneDollars + " dollar");
    if(numberOfQuarters > 1)
    	System.out.println("    " + numberOfQuarters + " quarters ");
    else if(numberOfQuarters == 1)
    	System.out.println("    " + numberOfQuarters + " quarter ");
    if(numberOfDimes > 1)
    	System.out.println("    " + numberOfDimes + " dimes"); 
    else if(numberOfDimes == 1)
    	System.out.println("    " + numberOfDimes + " dime"); 
    if(numberOfNickels > 1)
    	System.out.println("    " + numberOfNickels + " nickels");
    else if(numberOfNickels == 1)
    	System.out.println("    " + numberOfNickels + " nickel");
    if(numberOfPennies > 1)
    	System.out.println("    " + numberOfPennies + " pennies");
    else if(numberOfPennies == 1)
    	System.out.println("    " + numberOfPennies + " pennie");
  }
}

3-8 对三个整数排序

import java.util.Scanner;

public class Program3_8 {
   

    public static void main(String[] args) {
   
        Scanner input = new Scanner(System.in);

        System.out.print("Enter three numbers: ");
        int a = input.nextInt();
        int b = input.nextInt();
        int c = input.nextInt
  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值