明解JAVA 第二章答案

Posted nightswatch-candle

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了明解JAVA 第二章答案相关的知识,希望对你有一定的参考价值。

练习2-1

编译错误,无法运行。

练习2-2

package candle1220;



    class Nightwatch{

        public static void main(String[] args) {
            int x,y,z;
             x=22;
             y=23;
             z=35;
            System.out .println("合计值是"+(x+y+z)+"平均值是"+(x+y+z)/3);
    }
    }

 练习2-3

package candle1220;

import java.util.Scanner;

    class Nightwatch{

        public static void main(String[] args) {
            Scanner stdIn=new Scanner(System .in);
            int x;
            System .out .print("整数值:");
            x=stdIn.nextInt();
            System.out .println("输入的值是"+x);
                
    }
    }

练习2-4

package candle1220;

import java.util.Scanner;

    class Nightwatch{

        public static void main(String[] args) {
            Scanner stdIn=new Scanner(System .in);
            int x;
            System.out.print("整数值:");
            x=stdIn.nextInt();
            System.out.println("加上10的值是"+(x+10));
            System.out.println("减去10的值是"+(x-10));                
    }
    }
                        

练习2-5

package candle1220;

import java.util.Scanner;

    class Nightwatch{

        public static void main(String[] args) {
            Scanner stdIn=new Scanner(System .in);
            double x,y;
            System.out.print("x的值:");
            x=stdIn.nextDouble();
            System.out.print("y的值:");
            y=stdIn.nextDouble();
            System.out.println("合计值是"+(x+y));
            System.out.println("平均值是"+(x-y)/2);                
            
    }
    }
                        

练习2-6

package candle1220;

import java.util.Scanner;

    class Nightwatch{

        public static void main(String[] args) {
            Scanner stdIn=new Scanner(System .in);
            double x,y;
            System.out.println("求三角形的面积。");
            System.out.print("底:");
            x=stdIn.nextDouble();
            System.out.print("高:");
            y=stdIn.nextDouble();
            System.out.println("面积是"+(x*y)/2);
            
    }
    }
                        

练习2-7

package candle1220;

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

    class Nightwatch{

        public static void main(String[] args) {
            Scanner stdIn=new Scanner(System .in);
            Random rand=new Random();
        int lucky=rand.nextInt(8);
            System.out.print("随机数:"+(lucky+1));
            
    }
    }
                        
package candle1220;

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

    class Nightwatch{

        public static void main(String[] args) {
            Scanner stdIn=new Scanner(System .in);
            Random rand=new Random();
        int lucky=rand.nextInt(9);
            System.out.print("随机数:"+(lucky-9));
            
    }
    }
                        

练习2-8

package candle1220;

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

    class Nightwatch{

        public static void main(String[] args) {
            Scanner stdIn=new Scanner(System .in);
            Random rand=new Random();
            int lucky=(rand.nextInt(11)-5);
            System.out.print("整数值:");
            int x=stdIn.nextInt();
            System.out.println("生成了该值±5范围内的随机数,是"+(x+lucky));
            
        
    }
    }
                        

练习2-9

package candle1220;

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

    class Nightwatch{

        public static void main(String[] args) {
            Scanner stdIn=new Scanner(System.in);
            Random rand=new Random();
            double lucky=rand.nextDouble();
            System.out.print("随机数:"+lucky);
                
        
    }
    }
                        
package candle1220;

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

    class Nightwatch{

        public static void main(String[] args) {
            Scanner stdIn=new Scanner(System.in);
            Random rand=new Random();
            double lucky=10*rand.nextDouble();
            System.out.print("随机数:"+lucky);
                
        
    }
    }
                        
package candle1220;

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

    class Nightwatch{

        public static void main(String[] args) {
            Scanner stdIn=new Scanner(System.in);
            Random rand=new Random();
            double lucky=(2*rand.nextDouble()-1);
            System.out.print("随机数:"+lucky);
                
        
    }
    }
                        

练习2-10

package candle1220;

import java.util.Scanner;

    class Nightwatch{

        public static void main(String[] args) {
            Scanner stdIn=new Scanner(System.in);
            String s1="柴田";
            String s2="望洋";
            System.out.println("姓:"+s1);
            System.out.println("名:"+s2);
            System.out.println("你好"+s2+s1+"先生。");
            
            
                
    }
    }
                        

 

以上是关于明解JAVA 第二章答案的主要内容,如果未能解决你的问题,请参考以下文章

明解C语言第三版入门篇,do语言练习4-2答案,求大神

明解c语言第三版 入门篇 练习题答案 第六章 第6-11题解法。函数相关的问题。

明解C语言的代码清单5-13看不明白

明解C语言

明解c语言 13-4

片段(Java) | 机试题+算法思路+考点+代码解析 2023