明解JAVA 第四章答案

练习4-1

// 判断读入的整数值的符号(正/负/0)并显示判断结果

import java.util.Scanner;

class New {

    public static void main(String[] args) {
        Scanner stdIn = new Scanner(System.in);
        int retry;
do{
        System.out.print("整数值:");
        int n = stdIn.nextInt();

        if (n > 0)
            System.out.println("该值为正。");
        else if (n < 0)
            System.out.println("该值为负。");
        else
        System.out.println("该值为0。");
    
        System.out.println("要重复一次吗?1 yes  0  no");
        retry=stdIn.nextInt();
        }while(retry==1);
    }
}

练习4-2

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

class New {

    public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
    Random  rand=new  Random();
    int x=rand.nextInt(90);
    int y=x+10;
    int m;
    do{
        System.out.print("输入你要猜的数字:");
        m=input.nextInt();
        if(m<y)
            System.out.println("小了");
        else if(m>y)
            System.out.println("大了");
        else
            System.out.println("猜中了");
        
        
    }while(m!=y);
        
        }
    }

练习4-3

import java.util.Scanner;

class New {

    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.print("请输入较小值:");
        int x=input.nextInt();
        System.out.print("请输入较大值:");
        int y=input.nextInt();
        do{
            System.out.print(x+" ");
            x++;
        }while(x<=y);
        
        
        
    }    

    }

练习4-4

// 从某一正整数值倒数到0(其1)

import java.util.Scanner;

class New{

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

        System.out.println("倒数。");
        int x;
        do {
            System.out.print("正整数值:");
            x = stdIn.nextInt();
        } while (x <= 0);

        while (x >= 0) {
            System.out.println(x);    // 显示x的值
            x--;                    // x的值递减(值减少1)
        }
        System.out.print("x的值是"+x);
    }
}

练习4-5

会倒数到-1

// 从某一正整数值倒数到0(其2)

import java.util.Scanner;

class New{

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

        System.out.println("倒数。");
        int x;
        do {
            System.out.print("正整数值:");
            x = stdIn.nextInt();
        } while (x <= 0);

        while (x >= 0)
            System.out.println(--x);    // 显示x的值并递减
    }
}

练习4-6

// 显示所读入的数值个*(其1)

import java.util.Scanner;

class New {

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

        System.out.print("要显示多少个*呢:");
        int n = stdIn.nextInt();

        int i = 0;
        while (i < n) {
            System.out.print('*');
            i++;
        }
        if(n>0)
        System.out.println();
    }    
}

练习4-7

// 显示所读入的数值个*(其1)

import java.util.Scanner;

class New {

    public static void main(String[] args) {
    Scanner input =new Scanner(System.in);
    System.out.print("要显示多少个呢:");
    int n=input.nextInt();
    int i=0;
    while(i<n){
        if(i%2==0)
            System.out.print("*");
        else    
            System.out.print("+");
        i++;
    }
    }
}

练习4-8

import java.util.Scanner;

class New {

    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.print("请输入正整数值:");
        int x=input.nextInt();
        int i=0;
        while(x!=0){
            x/=10;
            i++;
        }
        System.out.print("位数是:"+i);
        
    
    
    
     }
}

练习4-9

import java.util.Scanner;

class New {

    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.print("请输入正整数值:");
        int x=input.nextInt();
        int i=1;
        int sum=1;
        while(i<=x){
            sum*=i;
            i++;
        }
        System.out.print("1到"+x+"的乘积是"+sum);
        
     }
}

练习4-11

// 从某一正整数值倒数到0(其1)

import java.util.Scanner;

class New {

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

        System.out.println("倒数。");
        int x;
        do {
            System.out.print("正整数值:");
            x = stdIn.nextInt();
        } while (x <= 0);
        int i;
    for(i=x;i>=0;i--){
            System.out.println(i);    // 显示x的值
        }
    }
}

练习4-12

import java.util.Scanner;

class New {

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

        System.out.println("正数。");
        int x;
        do {
            System.out.print("正整数值:");
            x = stdIn.nextInt();
        } while (x <= 0);
        int i;
    for(i=0;i<=x;i++){
            System.out.println(i);    // 显示x的值
        }
    }
}

练习4-13

 

上一篇:初识Java Random类的使用


下一篇:基本算法思维——拿最多物品