明解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 第二章答案)