《Java语言程序设计与数据结构》编程练习答案(第三章)(二)

《Java语言程序设计与数据结构》编程练习答案(第三章)(二)

英文名:Introduction to Java Programming and Data Structures, Comprehensive Versions, 11th Edition

3.12

import java.util.Scanner;
public class book {
    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 book {
    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 book {
    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 book {
    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 book {
    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 book {
    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 book {
    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 book {
    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);
    }
}

3.20

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the temperature: ");
        double t = input.nextDouble();
        System.out.print("Enter the velocity: ");
        double v = input.nextDouble();
        double twc = 35.74+0.6215*t-35.75*Math.pow(v,0.16)+0.4275*t*Math.pow(v,0.16);
        if(t>=-58&&t<=41&v>=2)
            System.out.println("The twc is "+twc);
        else
            System.out.println("Invalid input.");
    }
}

3.21

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        int[] mdays = {31,28,31,30,31,30,31,31,30,31,30,31};
        Scanner input = new Scanner(System.in);
        System.out.print("Enter year: (e,g.,2012):");
        int year = input.nextInt();
        System.out.print("Enter month: 1-12: ");
        int month = input.nextInt();
        if((year%4==0&&year%100!=0)||(year%400==0))
            mdays[1]=29;
        System.out.print("Enter the day of the month: 1-"+mdays[month-1]+": ");
        int q = input.nextInt();
        if(month==1||month==2){
            month+=12;
            year--;
        }
        int h=(q+26*(month+1)/10+year%100+year%100/4+year/100/4+5*(year/100))%7;
        String[] days = {"Sat","Sun","Mon","Tue","Wed","Thu","Fri"};
        System.out.println("Day of the week is "+days[h]);
    }
}

3.22

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        System.out.print("Enter a point with two coordinates: ");
        Scanner input = new Scanner(System.in);
        double x = input.nextDouble();
        double y = input.nextDouble();
        if(x*x+y*y>100)
            System.out.println("Point ("+x+", "+y+") is not in the circle");
        else
            System.out.println("Point ("+x+", "+y+") is in the circle");
    }
}

3.23

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        System.out.print("Enter a point with two coordinates: ");
        Scanner input = new Scanner(System.in);
        double x = input.nextDouble();
        double y = input.nextDouble();
        if(x<=10/2&&x>=-10/2&&y<=5.0/2&&y>=-5.0/2)
            System.out.println("Point ("+x+", "+y+") is in the rectangle");
        else
            System.out.println("Point ("+x+", "+y+") is not in the rectangle");
    }
}
发布了75 篇原创文章 · 获赞 61 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/swy_swy_swy/article/details/104446799
今日推荐