java语言程序设计-基础篇(第八版)第三章课后题答案

第三章


复习题
3.1 <; <=; ==; !=; >; >=


3.2 不能转换。布尔型不能转换到其它类型。


3.3-3.11
3.3
number = 30:
a: 30 is even.
30 is odd.
b: 30 is even.
number = 35:
a: 35 is odd.
b: 35 is odd.


3.4
1.无输出
2.z is 7
3.x is 2


3.5
缩进正确的:b,c,d
等价的:bd


3.6
if(x>2){
if(y>2){
int z = x + y;
System.out.println("z is" + z);
}
}else{
System.out.println("x is " + x);
}

3.7 yes
3.8 0.5; 0.0; 0.234
3.9 (int)(Math.random())*20
(int)(Math.random()*10)+10
(int)(Math.random()*41)+10
3.10 
if(y>0){
x=1;
}
3.11
(1)
if(score>90){
pay+=pay*3%;
}
(2)
if(score>90){
pay+=pay*3%;
}else{
pay+=pay*1%;
}


3.13
newLine=(count%10==0)


3.14
false
false
true
true
true
true


3.15
(num>1)&&(num<100)
3.16
(num>1)&&(num<100)||(num<0)
3.17
x/=y
3.18
2,1
3.19
If ch is 'A', the expression is true;


If ch is 'p', the expression is false;


If ch is 'E', the expression is true;


If ch is '5', the expression is false;
3.20
true
true
false
true
true
3.21
(age>13)&&(age<18)
3.22
(weight>50)||(heigt>160)
3.23
(weight>50)&&(heigt>160)
3.24
(weight>50)^(heigt>160)
3.25
char,int,short,byte 
还可以接受String和enum类型的参数。
现在java 7是可以接受String了


如果在执行完case语句后没有使用关键字break, 那么下一个 case statement 被执行
switch可读性高,而且效率快


3.26
y=2


3.27
switch(a){
case 1:
x+=5;
break;
case 2:
x+=10;
break;
case 3:
x+=16;
break;
case 4:
x+=34;
}


3.28
public class DemoTest {
    public static void main(String[] args) {
        int day=3;
        String dayName="";


        switch(day){
            case 0:
                dayName="Sunday";
                break;
            case 1:
                dayName="Monday";
                break;
            case 2:
                dayName="Tuesday";
                break;
            case 3:
                dayName="Wednesday";
                break;
            case 4:
                dayName="Thursday";
                break;
            case 5:
                dayName="Friday";
                break;
            case 6:
                dayName="Saturday";
                break;
        }
        System.out.println(dayName);
    }
}


3.29
public class DemoTest {
    public static void main(String[] args){
        int count=10;
        System.out.print(count%10==0? (count +"\n"):(count + ""));
            }
}


3.30
pay=((temperature>90)?pay*1.5:pay*1.1);


3.31
输出布尔值,字符,十进制整数,浮点数和字符串的格式描述符分别是 %b, %c, %d, %f, and %s.
3.32
(a) the last item 3 does not have any specifier.


(b) There is not enough items


(c) The data for %f must a floating-point value
3.33
(a)amount is 32.320000 3.233000e+01
(b)amount is 32.3200 3.2330e+01
(c) *false
(d) **Java
(e)false*Java 
(f)*falseJava****
3.34
%s
Use the String.format method to create a formatted string.
3.35
运算符顺序是:^, &&, and || 


3.36
true


3.37
false
false


3.38

----------------------------------------------------------------------------------
编程练习
3.1
import java.util.Scanner;


public class DemoTest {
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.print("pls enter a,b,c:");
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();
        double r1,r2;


        r1=(-b+Math.pow((b*b-4*a*c),0.5))/(2*a);
        r2=(-b-Math.pow((b*b-4*a*c),0.5))/(2*a);
        if(b*b-4*a*c>0) {
            System.out.println(r1 + "and" + r2);
        }else if(b*b-4*a*c==0){
            System.out.println(r1);
        }else{
            System.out.println("no real roots");
        }
            }
}


3.2
import java.util.Scanner;


public class DemoTest {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("pls enter a number:");
        int a = input.nextInt();


        if (a % 2 == 0) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }
    }
}


3.3
import java.util.Scanner;


public class DemoTest {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("pls 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 x,y;
        x=(e*d-b*f)/(a*d-b*c);
        y=(a*f-e*c)/(a*d-b*c);


        if (a*d-b*c == 0) {
            System.out.println("the equation has no solution");
        } else {
            System.out.println("x="+x+"y="+y);
        }
    }
}
3.4
import java.util.Scanner;


public class DemoTest {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int a=(int)(Math.random()*100);
        int b=(int)(Math.random()*100);
        System.out.print("pls enter sum of:"+a+"+"+b+"is");
        int c = input.nextInt();
        System.out.println(a+b==c ? true:false);
    }
}
3.5
import java.util.Scanner;


public class DemoTest {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int a=(int)(Math.random()*10);
        int b=(int)(Math.random()*10);
        int c=(int)(Math.random()*10);
        System.out.print("pls enter sum of:"+a+"+"+b+"+"+c+"is");
        int d = input.nextInt();
        System.out.println(a+b+c==d ? true:false);
    }
}
3.6
3.7
import java.util.Scanner;


public class Main {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter an amount in double,for eg. 11.56");
        double amount=input.nextDouble();
        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<=0){
            System.out.print("");
        }else if(numberOfOneDollars==1){
            System.out.println("\t"+numberOfOneDollars+" dollar");
        }else{
            System.out.println("\t"+numberOfOneDollars+" dollars");
        }


        if(numberOfQuarters<=0){
            System.out.print("");
        }else if(numberOfQuarters==1){
            System.out.println("\t"+numberOfQuarters+" quarter");
        }else{
            System.out.println("\t"+numberOfQuarters+" quarters");
        }


        if(numberOfDimes<=0){
            System.out.print("");
        }else if(numberOfDimes==1){
            System.out.println("\t"+numberOfDimes+" dimes");
        }else{
            System.out.println("\t"+numberOfDimes+" dimes");
        }


        if(numberOfNickels<=0){
            System.out.print("");
        }else if(numberOfNickels==1){
            System.out.println("\t"+numberOfNickels+" nickel");
        }else{
            System.out.println("\t"+numberOfNickels+" nickels");
        }


        if(numberOfPennies<=0){
        }else if(numberOfPennies==1){
            System.out.println("\t"+numberOfPennies+" pennie");
        }else{
            System.out.println("\t"+numberOfPennies+" pennies");
        }
    }
}


3.8
import java.util.Scanner;


public class DemoTest {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter three integer:");
        int num1=input.nextInt();
        int num2=input.nextInt();
        int num3=input.nextInt();
        int temp;
        if(num2>num3) {
                temp = num2;
                num2 = num3;
                num3 = temp;
            }
        if(num1>num3){
            temp=num3;
            num1=num3;
            num3=num1;
            }
        if(num1>num2){
            temp=num1;
            num1=num2;
            num2=temp;
        }
        System.out.println(num1+"<="+num2+"<="+num3);
    }
}


用数组排序
import java.util.Scanner;
import java.util.Arrays;


public class DemoTest {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter three integer:");
        int num1=input.nextInt();
        int num2=input.nextInt();
        int num3=input.nextInt();


        int[] array={num1,num2,num3};
        Arrays.sort(array);
        for(int i=0;i<array.length;i++){
            System.out.println(array[i]);
        }
    }
}


3.9
import java.util.Scanner;


public class Main {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter nine integer: ");
        int d1=input.nextInt();
        int d2=input.nextInt();
        int d3=input.nextInt();
        int d4=input.nextInt();
        int d5=input.nextInt();
        int d6=input.nextInt();
        int d7=input.nextInt();
        int d8=input.nextInt();
        int d9=input.nextInt();


        int d10=(d1*1+d2*2+d3*3+d4*4+d5*5+d6*6+d7*7+d8*8+d9*9)%11;
        System.out.println(d1+""+d2+""+d3+""+d4+""+d5+""+d6+""+d7+""+d8+""+d9+""+d10);
    }
}


3.10
import java.util.Scanner;


public class Main {


    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int num1 = (int) (Math.random() * 100);
        int num2 = (int) (Math.random() * 100);
        System.out.println("pls enter the sum of "+num1+"+"+num2+"=");
        int sum=input.nextInt();
        if(sum==(num1+num2)){
            System.out.println("Correct!");


        }else{
            System.out.println("pls rechecking the result!");
        }
    }
}


3.11
import java.util.Scanner;


public class Main {
    public static int method(int year, int month) {
        int days;
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
             days = 31;
        } else if (month == 2) {
            if (year % 4 == 0) {
                 days = 29;
            } else {
                 days = 28;
            }
        }else{
            days=30;
        }
        return days;
        }


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入年:");
        int year = scanner.nextInt();
        System.out.print("请输入月:");
        int month = scanner.nextInt();


        System.out.println(year+" year"+month+" month have "+method(year,month)+" days");
    }
}


3.12
import java.util.Scanner;


public class Main {
    private static void method(int num) {
        if(num%5!=0&&num%6!=0){
            System.out.print(num+"is not divisible by either 5 or 6");
        }else if(num%5==0&&num%6==0){
            System.out.println(num+"is divisible by both 5 and 6");
        }else{
            System.out.println(num+"is divisible by 5 or 6,but not both");
        }
        }


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("pls enter a integer");
        int num=input.nextInt();
        method(num);
    }
}
或者用异或
import java.util.Scanner;


public class Main {
    private static void method(int num) {
        if(num%5==0&&num%6==0){
            System.out.println(num+"is divisible by both 5 and 6");
        }else if(num%5==0^num%6==0){
            System.out.println(num+"is divisible by 5 or 6,but not both");
        }else{
            System.out.println(num+"is not divisible by either 5 or 6");
        }
        }


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("pls enter a integer");
        int num=input.nextInt();
        method(num);
    }
}


3.14
import java.util.Scanner;


public class Main{
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.println("Enter your guess: ");
        int guess=input.nextInt();


        int result=(int)(Math.random()*2);
        System.out.println("the result is "+result);


            if(guess==result){
                System.out.println("correct!");
            }else if(result==0){
                System.out.println("head");
            }else{
                System.out.println("tail");
            }
    }
}


3.15
import java.util.Scanner;


public class Main{
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.println("pls enter your numbers: ");
        int guessDigits=input.nextInt();
        int lottery=(int)(Math.random()*1000);
        System.out.println("the winning numbers is "+lottery);


        int lotteryDigit1=lottery/100;
        lottery=lottery%100;
        int lotteryDigit2=lottery/10;
        int lotteryDigit3=lottery%10;


        int guessDigit1=guessDigits/100;
        guessDigits=guessDigits%100;
        int guessDigit2=guessDigits/10;
        int guessDigit3=guessDigits%10;


        boolean secondPrize=(guessDigit1==lotteryDigit2&&guessDigit2==lotteryDigit3&&guessDigit3==lotteryDigit1)||
                (guessDigit1==lotteryDigit2&&guessDigit2==lotteryDigit1&&guessDigit3==lotteryDigit2)||
                (guessDigit1==lotteryDigit3&&guessDigit2==lotteryDigit1&&guessDigit3==lotteryDigit2)||
                (guessDigit1==lotteryDigit3&&guessDigit2==lotteryDigit2&&guessDigit3==lotteryDigit1)||
                (guessDigit1==lotteryDigit1&&guessDigit2==lotteryDigit3&&guessDigit3==lotteryDigit2);


        boolean thirdPrize=guessDigit1==lotteryDigit1||guessDigit1==lotteryDigit2||guessDigit1==lotteryDigit3||
                guessDigit2==lotteryDigit2||guessDigit2==lotteryDigit3||3==lotteryDigit3;


        if(guessDigits==lottery){
            System.out.println("Exact Match,you win 10,000 dollars");
        }else if(secondPrize){
            System.out.println("Match all digits,you win 3,000 dollars ");
        }else if(thirdPrize){
            System.out.println("Match one digits,you win 1,000 dollars");
        }else{
            System.out.println("Sorry,no match");
        }
    }
}


3.16
提示一共26个英文字母
public class Main{
    public static void main(String[] args) {
        System.out.println((char)('A'+Math.random() * 27));
    }
}


3.17
import java.util.Random;
import java.util.Scanner;


public class Main{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("pls enter your number(scissor(0),rock(1),paper(2):");
        int num1=input.nextInt();
        Random random=new Random();
        int num=random.nextInt(3);
        boolean draw=(num1==0&&num==0)||(num1==1&&num==1)||(num1==2&&num==2);
        if(draw){
            System.out.println("it is draw");
        }else if(num1==0){
            if(num==1) {
                System.out.println("you failure,you are scissor,the computer is rock");
            }else if(num==2){
                System.out.println("you win,you are scissor,the computer is paper");
            }
        }else if(num1==1){
            if(num==2) {
                System.out.println("you failure,you are rock,the computer is paper");
            }else if(num==0){
                System.out.println("you win,you are rock,the computer is scissor");
            }
        }else if(num1==2){
            if(num==1) {
                System.out.println("you win,you are paper,the computer is rock");
            }else if(num==0){
                System.out.println("you failure,you are rock,the computer is paper");
            }
        }


    }
}


3.19
import java.util.Scanner;


public class Main{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("enter three edges:");
        double a=input.nextDouble();
        double b=input.nextDouble();
        double c=input.nextDouble();


        System.out.println("Can edges"+a+","+b+","+c+"form a triangle?");
        boolean result=(a+b)>c&&(a+c)>b&&(b+c)>a;
        System.out.println(result);
    }
}


3.21
import java.util.Scanner;


public class Main{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("enter year:(e.g.,2008)");
        int year=input.nextInt();


        System.out.println("enter month: 1-12");
        int month=input.nextInt();


        System.out.println("enter the day of month: 1-31");
        int day=input.nextInt();


        if(month==1||month==2){
            month=month+12;
            year=year-1;
        }


        int k=year%100;
        int j=(int)(year/100);
        int h=(day+(int)(26*(month+1)/10)+k+(int)(k/4)+(int)(j/4)+5*j)%7;
        String result;
        if(h==0){
            result="Saturday";
        }else if(h==1){
            result="Sunday";


        }else if(h==2){
            result="Monday";
        }else if(h==3){
            result="Tuesday";
        }else if(h==4){
            result="Wednesday";
        }else if(h==5){
            result="Thursday";
        }else{
            result="Friday";
        }
        System.out.println("Day of the week is "+result);
    }
}


3.22
import java.util.Scanner;


public class Main{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("enter a point with two coordinates");
        double x=input.nextDouble();
        double y=input.nextDouble();
        double z=Math.pow(((x-0)*(x-0)+(y-0)*(y-0)),0.5);
        double k=10;
        System.out.println(z<=k?"in the circle":"not in the circle");


    }
}


3.23
import java.util.Scanner;


public class Main{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("enter a point with two coordinates");
        double x=input.nextDouble();
        double y=input.nextDouble();
        System.out.println(x<=(5/2)&&y<=(10/2)?"in the rectangle":"not in the rectangle");


    }
}


3.24
public class Main{
    public static void main(String[] args) {
        final int NUM_OF_CARDS=52;
        int k=(int)(Math.random()*NUM_OF_CARDS);
        String result=null;
        String color=null;
        if(k%13==0){
            result="Ace";
        }else if (k%13==10){
            result="Jack";
        }else if(k%13==11){
            result="Queen";
        }else if(k%13==12){
            result="King";
        }else {
            result=k%13+"";
        }


        if(k/13==0){
            color="Clubs";
        }else if(k/13==1){
            color="Diamond";
        }else if(k/13==2){
            color="Heart";
        }else if(k/13==3){
            color="Spades";
        }


        System.out.println(result+" of " +color);
    }
}


3.25
import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.println("enter the a,b,c:");
        double a=input.nextDouble();
        double b=input.nextDouble();
        double c=input.nextDouble();
        boolean legitimate=(a+b)>c&&(a+c)>b&&(b+c)>a;
        if(legitimate){
            System.out.println(a+b+c);
        }else{
            System.out.println("invalid");
        }
    }
}


3.26
import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.println("enter the number:");
       int a=input.nextInt();


        boolean result=a%5==0&&a%6==0;
        System.out.println("Is " + a + " divisible by 5 and 6?"+result);


         result=a%5==0 || a%6==0;
        System.out.println("Is " + a + " divisible by 5 or 6?"+result);


        result=a%5==0^a%6==0;
        System.out.println("Is " + a + " divisible by 5 or 6, but not " +
                "both?"+ result);
    }
}


P110







©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页