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

3.1

import java.util.Scanner;
public class disanzhang {

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

}

3.2

import java.util.Scanner;
public class disanzhang {

public static void main(String[] args){
        int number1 = (int)(System.currentTimeMillis()%10);
        int number2 = (int)(System.currentTimeMillis()/10%10);
        int number3 = (int)(System.currentTimeMillis()/100%10);
        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

import java.util.Scanner;
public class disanzhang {

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

}

3.4

public class disanzhang {

public static void main(String[] args){
        int month = (int)(Math.random()*12+1);
        String[] names={"Feb","Jan","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
        System.out.println("The number is "+month+", and the month is "+names[month-1]);
}

}

3.5

import java.util.Scanner;
public class disanzhang {

public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("Enter today's day:");
        int day = input.nextInt();
        System.out.print("Enter the number of days elapsed since today:");
        int plus = input.nextInt();
        int result = (day+plus)%7;
        String[] days = {"Sun","Mon","Tues","Wed","Thu","Fri","Sat"};
        System.out.println("Today is "+days[day]+" and the future day is "+days[result]);
}

}

3.6

import java.util.Scanner;
public class disanzhang {

public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("Enter weight in pounds:");
        double pounds = input.nextDouble();
        System.out.print("Enter feet:");
        double feet = input.nextDouble();
        System.out.print("Enter inches:");
        double inches = input.nextDouble();
        final double KILOS_PER_POUND = 0.45359237;
        final double METERS_PER_INCH = 0.0254;
        final double METERS_PER_FOOT = 0.3048;
        double kilos = pounds*KILOS_PER_POUND;
        double meters = feet*METERS_PER_FOOT+inches*METERS_PER_INCH;
        double bmi = kilos/(meters*meters);
        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 disanzhang {

public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an amount in double, for example 114.514:");
        double amount = input.nextDouble();
        int remainAmount = (int)(amount*100);
        int numberOfDollars = remainAmount/100;
        remainAmount%=100;
        int numberOfQuarters = remainAmount/25;
        remainAmount%=25;
        int numberOfDimes = remainAmount/10;
        remainAmount%=10;
        int numberOfNickels = remainAmount/5;
        remainAmount%=5;
        int numberOfPennies = remainAmount;
        System.out.println("Your amount "+amount+" consists of");
        if(numberOfDollars>0){
            System.out.print(numberOfDollars);
            if(numberOfDollars>1)
                System.out.println(" dollars");
            else
                System.out.println(" dollar");
        }
        if(numberOfQuarters>0){
            System.out.print(numberOfQuarters);
            if(numberOfQuarters>1)
                System.out.println(" quarters");
            else
                System.out.println(" quarter");
        }
        if(numberOfDimes>0){
            System.out.print(numberOfDimes);
            if(numberOfDimes>1)
                System.out.println(" dimes");
            else
                System.out.println(" dime");
        }
        if(numberOfNickels>0){
            System.out.print(numberOfNickels);
            if(numberOfNickels>1)
                System.out.println(" nickels");
            else
                System.out.println(" nickel");
        }
        if(numberOfPennies>0){
            System.out.print(numberOfPennies);
            if(numberOfPennies>1)
                System.out.println(" pennies");
            else
                System.out.println(" penny");
        }
}

}

3.8

import java.util.Scanner;
public class disanzhang {

public static void main(String[] args){
        System.out.print("Input 3 integers:");
        Scanner input = new Scanner(System.in);
        int num1 = input.nextInt();
        int num2 = input.nextInt();
        int num3 = input.nextInt();
        if(num1>num2){
            int tmp = num1;
            num1 = num2;
            num2 = tmp;
        }
        if(num2>num3){
            int tmp = num2;
            num2 = num3;
            num3 = tmp;
        }
        if(num1>num2){
            int tmp = num1;
            num1 = num2;
            num2 = tmp;
        }
        System.out.println(num1+" "+num2+" "+num3);
}

}

3.9

import java.util.Scanner;
public class disanzhang {

public static void main(String[] args){
        int sum = 0;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the first 9 digits of an ISBN as integer: ");
        int isbnNum = input.nextInt();
        int old = isbnNum;
        for(int i=9;i>=1;i--){
            sum+=isbnNum%10*i;
            isbnNum/=10;
        }
        int tail = sum%11;
        System.out.printf("The ISBN-10 number is %09d",old);
        if(tail<10)
            System.out.println(tail);
        else
            System.out.println("X");
}

}

3.10

import java.util.Scanner;
public class disanzhang {

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();
        if(number1+number2==answer)
            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

import java.util.Scanner;
public class disanzhang {

public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the month number: ");
        int month = input.nextInt();
        System.out.print("Enter the year number: ");
        int year = input.nextInt();
        int[] months={31,28,31,30,31,30,31,31,30,31,30,31};
        String[] monthNames={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
        boolean isLeapYear = (year%4==0&&year%100!=0)||(year%400==0);
        if(isLeapYear)
            months[1]=29;
        System.out.println(monthNames[month-1]+" "+year+" has "+months[month-1]+" days.");
}

}

3.12

import java.util.Scanner;
public class disanzhang {

public static void main(String[] args){
        System.out.print("Enter a three-digit integer:");
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        int old = num;
        if(num<0)
            num*=(-1);
        int gewei = num%10;
        int baiwei = num/100;
        if(gewei==baiwei)
            System.out.println(old+" is a palindrome.");
        else
            System.out.println(old+" is not a palindrome.");
}

}

3.13

import java.util.Scanner;
public class disanzhang {

public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the filing status:");
        int status = input.nextInt();
        System.out.print("Enter the taxable income:");
        double income = input.nextDouble();
        double tax = 0;
        if(status==0){
            if(income<=8350)
                tax = income*0.1;
            else if(income<=33950)
                tax = 8350*0.1+(income-8350)*0.15;
            else if(income<=82250)
                tax = 8350*0.1+(33950-8350)*0.15+(income-33950)*0.25;
            else if(income<=171550)
                tax = 8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(income-82250)*0.28;
            else if(income<=372950)
                tax = 8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(171550-82250)*0.28+(income-171550)*0.33;
            else
                tax = 8350*0.1+(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.1;
            else if (income <= 67900)
                tax = 16700 * 0.1 + (income - 16700) * 0.15;
            else if (income <= 137050)
                tax = 16700 * 0.1 + (67900 - 16700) * 0.15 + (income - 67900) * 0.25;
            else if (income <= 208850)
                tax = 16700 * 0.1 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (income - 137050) * 0.28;
            else if (income <= 372950)
                tax = 16700 * 0.1 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (income - 208850) * 0.33;
            else
                tax = 16700 * 0.1 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (372950 - 208850) * 0.33 + (income - 372950) * 0.35;
        }
        else if(status==2) {
            if (income <= 8350)
                tax = income * 0.1;
            else if (income <= 33950)
                tax = 8350 * 0.1 + (income - 8350 )* 0.15;
            else if (income <= 68525)
                tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
            else if (income <= 104425)
                tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (income - 68525) * 0.28;
            else if (income <= 186475)
                tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (income - 104425) * 0.33;
            else
                tax = 8350 * 0.1 + (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.1;
            else if (income <= 45500)
                tax = 11950 * 0.1 + (income - 11950 )* 0.15;
            else if (income <= 117450)
                tax = 11950 * 0.1 + (45500 - 11950) * 0.15 + (income - 45500) * 0.25;
            else if (income <= 190200)
                tax = 11950 * 0.1 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (income - 117450) * 0.28;
            else if (income <= 372950)
                tax = 11950 * 0.1 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (income - 190200) * 0.33;
            else
                tax = 11950 * 0.1 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (372950 - 190200) * 0.33 + (income - 372950) * 0.35;
        }
        System.out.println("Tax is "+(int)(tax*100)/100.0);
}

}

3.14

import java.util.Scanner;
public class disanzhang {

public static void main(String[] args){
        int coin = (int)(Math.random()*2);
        System.out.print("Guess: ");
        Scanner input = new Scanner(System.in);
        int g = input.nextInt();
        if(g==coin)
            System.out.println("You are right, coin is "+coin);
        else
            System.out.println("You are wrong, coin is "+coin);
}

}

3.15

import java.util.Scanner;
public class disanzhang {

public static void main(String[] args){
        int lottery = (int)(Math.random()*1000);
        int ld1 = lottery%10;
        int ld2 = lottery/10%10;
        int ld3 = lottery/100%10;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter your pick: ");
        int guess = input.nextInt();
        int g1 = guess%10;
        int g2 = guess/10%10;
        int g3 = guess/100%10;
        System.out.println("The lottery number is "+lottery);
        if(guess==lottery)
            System.out.println("you win $10,000");
        else if((g1==ld1&&g2==ld3&&g3==ld2)||(g1==ld2&&g2==ld1&&g3==ld3)||(g1==ld2&&g2==ld3&&g3==ld1)||(g1==ld3&&g2==ld1&&g3==ld2)||(g1==ld3&&g2==ld2&&g3==ld1))
            System.out.println("you win $3000");
        else if(g1==ld1||g1==ld2||g1==ld3||g2==ld1||g2==ld2||g2==ld3||g3==ld1||g3==ld2||g3==ld3)
            System.out.println("you win $1000");
        else
            System.out.println("bad luck!");
}

}

3.16

public class disanzhang {

public static void main(String[] args){
        int x = (int)(Math.random()*100)-50;
        int y = (int)(Math.random()*200)-100;
        System.out.println("The random point is ("+x+","+y+")");
}

}

3.17

import java.util.Scanner;
public class disanzhang {

public static void main(String[] args){
        System.out.print("scissor(0), rock(1), paper(2):");
        Scanner input = new Scanner(System.in);
        int player = input.nextInt();
        int com  = (int)(Math.random()*3);
        String[] names = {"scissor","rock","paper"};
        if(com==player)
            System.out.println("The computer is "+names[com]+". You are "+names[player]+" too. It is a draw.");
        else{
            System.out.print("The computer is "+names[com]+". You are "+names[player]+". You ");
            String result = "";
            if((com==0&&player==1)||(com==1&&player==2)||(com==2&&player==0))
                result="win";
            else
                result="lose";
            System.out.println(result);
        }
}

}

3.18

import java.util.Scanner;
public class disanzhang {

public static void main(String[] args){
        System.out.print("Enter the weight:");
        Scanner input = new Scanner(System.in);
        double w = input.nextDouble();
        double c = 0;
        if(w>20)
            System.out.println("the package cannot be shipped");
        else if(w<=0)
            System.out.println("Invalid input");
        else if(w<=1)
            c=3.5;
        else if(w<=3)
            c=5.5;
        else if(w<=10)
            c=8.5;
        else if(w<=20)
            c=10.5;
        if(c!=0)
            System.out.println("The cost is "+c);
}

}

3.19

import java.util.Scanner;
public class disanzhang {

public static void main(String[] args){
        System.out.print("Enter the three lengths:");
        Scanner input = new Scanner(System.in);
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();
        double cir = a+b+c;
        if((2*a>=cir)||(2*b>=cir)||(2*c>=cir))
            System.out.println("Invalid input");
        else
            System.out.println("The circumference is "+cir);
}

}

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xupengboo

你的鼓励将是我创作最大的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值