《Java语言程序设计与数据结构(基础篇)》第11版第三章复习题答案

3.2.1答案:<  <=  >  >=  ==  !=

3.2.2答案:true

          false

          true

          true

          false

3.2.3答案:不合法

Java:不兼容的类型:boolean无法转化为int

Java:不兼容的类型:int无法转化为boolean

3.3.1答案:

import java.util.Scanner;
public class Review3_3_1 {
    public static void main(String[] args){
         Scanner input = new Scanner(System.in);
         System.out.println("Enter the y : ");
         int y = input.nextInt();
         int x =0 ;
         if(y>0)
             x=1;
         System.out.println("x is "+ x);
    }
}

3.3.2答案:

import java.util.Scanner;
public class Review3_3_2 {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter your score: ");
        double score = input.nextDouble();
        if(score > 90)
            score = score + score * 0.03;
        System.out.println("the cost is: "+score);
    }
}

3.3.3答案:if中的语句应该用括号括起来,第一行应改为if(radius >= 0)

3.4.1答案:

import java.util.Scanner;
public class Review3_4_1 {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter your score: ");
        double score = input.nextDouble();
        if(score > 90)
            score = score + score * 0.03;
        else
            score = score + score * 0.01;
        System.out.println("the cost is: "+score);
    }
}

3.4.2答案:

number 是30 的时候

(a)30 is even.

30 is odd.

(b)30 is even.

number 是35 的时候

(a)30 is odd.

(b)30 is odd.

3.5.1答案:第一个:代码无输出

           第二个:z is 7

           第三个:x is 2

3.5.2答案:第一个:代码无输出

          第二个:x is 3

第三个:z is 6

3.5.3答案:score在60分以上的结果输出都是D

3.6.1答案:a,c,d是等价的

          b,c是合理缩进的

3.6.2答案:boolean newLine = count % 10 == 0;

3.6.3答案:正确,b更好

3.6.4答案:

number为14时:(a)14 is even

                (b)14 is even

number为15时:(a)15 is multiple of 5

                (b) 15 is multiple of 5

number为30时:(a) 30 is even

30is multiple of 5

                (b) 30 is even

3.7.1答案:0.5  0.0  0.234

3.7.2答案:int i = (int)(Math.random()*20)

          int i = (int) (10+Math.random()*10)

          int i = (int)(Math.random()*51)

          int i = (int)(Math.random()+0.5)

3.9.1答案:等价

3.10.1答案:

false

false

true

true

true

true

3.10.2答案:

(a)if((num>=1)&&(num<=100)){

System.out.println(“true”);}

(b)if((num>=1)&&(num<=100)||(num<0)){

System.out.println(“true”);}

3.10.3答案:

(a) x>0.5&&x<9.5

(b)x<0.5||x>9.5

3.10.4答案:x/=y

3.10.5答案:等同,6是2和3的最小公倍数

3.10.6答案:

x=45时,表达式的值为false

x=67时,表达式的值为true

x=101时,表达式的值为false

3.10.7答案:

(x<y&&y<z) is true

(x<y||y<z) is true

!(x<y) is false

(x+y<z) is true

(x+y>z) is false

3.10.8答案:

if(age>13&&age<18)

   System.out.println(“true”);

3.10.9答案:

if((weight>50)||(height>60))

   System.out.println(“true”);

3.10.10答案:

if((weight>50)&&(height>60))

   System.out.println(“true”);

3.10.11答案:

if((weight>50)^(height>60))

   System.out.println(“true”);

3.11.1答案:闰年的二月有29天,2000和2016是闰年

3.12.1答案:输入05时,如果与随机产生的彩票数数字且顺序完全一致,

则输出“Exact math : you win $10,000”

输入05时,如果与随机产生的彩票数数字匹配但顺序不一致,

则输出“Exact math : you win $3,000”

输入05时,如果与随机产生的彩票数数字只有一个匹配,

则输出“Exact math : you win $1,000”

输入05时,如果与随机产生的彩票数数字且顺序完全一致,

则输出“Sorry,no match”

3.13.1答案:char,byte,short,int,string

           一旦匹配其中一个case,就从匹配的case处开始执行,直到遇见break语句活着到达switch语句的结束

          可以把switch语句转换成等价的if语句

          但不一定能将if语句转换为等价的switch语句

          使用switch语句的优点:程序逻辑结构清晰,语句简单

3.13.2答案:执行后,y=2,(因为没有使用break跳出,所以当case 6满足条件时,y=1,没有跳出,y继续加1,y=2)

            将其改成if-else后为

public class XiTi3_13 {
    public static void main(String[] args){
        int x = 3,y = 3;
        if((x+3) == 6){
            y=1;
        }
        else
            y=y+1;
        System.out.println(y);
    }
}

3.13.3答案:x是17

           使用switch后,代码为

           int x = 1,a = 3;

           switch(a){

           case 1: x+=5;break;

case 2: x+=10;break;

case 3: x+=16;break;

case 4: x+=34;break;

}

3.13.4答案:

switch(day){

  case 0:System.out.println(“Sunday”);break;

case 1:System.out.println(“Monday”);break;

case 2:System.out.println(“Tuesday”);break;

case 3:System.out.println(“Wednesday”);break;

case 4:System.out.println(“Thursday”);break;

case 5:System.out.println(“Friday”);break;

 case 6:System.out.println(“Saturday”);break;

3.13.5答案:

import java.util.Scanner;
public class ChineseZodiac {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the years : ");
        int year = input.nextInt();
        if(year % 12 == 0)
            System.out.println("monkey");
        else if(year % 12 == 1)
            System.out.println("rooster");
        else if(year % 12 == 2)
            System.out.println("dog");
        else if(year % 12 == 3)
            System.out.println("pig");
        else if(year % 12 == 4)
            System.out.println("rat");
        else if(year % 12 == 5)
            System.out.println("ox");
        else if(year % 12 == 6)
            System.out.println("tiger");
        else if(year % 12 == 7)
            System.out.println("rabbit");
        else if(year % 12 == 8)
            System.out.println("dragon");
        else if(year % 12 == 9)
            System.out.println("snake");
        else if(year % 12 == 10)
            System.out.println("horse");
        else if(year % 12 == 11)
            System.out.println("sheep");
    }
}

3.14.1答案:sorted

3.14.2答案:

ticketPrice = (ages >= 16) ? 20 :10;

3.14.3答案:

(a)

If(x > 10)

  score = 3 * scale;

else

 score = 4 * scale;

(b)

if(income > 10000)
    tax = income * 0.2;
else
    tax = income * 0.17 + 1000;

(c)
if(number % 3 == 0)
    System.out.println(i);
else

    System.out.println(j);

3.14.4答案:

import java.util.Scanner;
public class XiTi3_14_4 {
    public static void main(String[] args){
        System.out.println("Enter a digit: ");
        Scanner input= new Scanner(System.in);
        double a = input.nextDouble();
        int b = (a>5.0) ? 1: -1;
        System.out.println(b);
    }
}

3.15.1答案:布尔操作符由高级向低级的顺序为!  &&  ||

计算结果为: true  true

3.15.2答案:这个说法是假的

3.15.3答案:false

            false

3.15.4答案:一样  一样  一样

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值