Java第三章上机实践-实验2-猜数字游戏

来源:互联网 发布:黑蜘蛛软件 编辑:程序博客网 时间:2024/04/29 20:33


Guess.java

import java.util.Random;import java.util.Scanner;/* * To change this template, choose Tools | Templates * and open the template in the editor. *//** * * @author Administrator */public class Guess {    public void guessnum(){        Scanner reader=new Scanner(System.in);        Random random=new Random();        System.out.println("please input a number between 1 to 100:");        int realNumber = random.nextInt(100)+1;  //随机数字初始化        int yourGuess = 0;        System.out.println("please input the number which you guess:");        yourGuess=reader.nextInt();        //当yourGuess  的数字与随机数不同时,进入while循环,直到两数字大小一样        while(yourGuess!=realNumber){            if(yourGuess>realNumber){                System.out.println("your Guess number is bigger than real number,please input again:");                yourGuess=reader.nextInt();            }            else if(yourGuess<realNumber){                System.out.println("your Guess number is smaller than real number,please input again:");                yourGuess=reader.nextInt();            }        }        //输出正确数字        System.out.println("you are right!");    }}



Test.java

/* * To change this template, choose Tools | Templates * and open the template in the editor. *//** * * @author Administrator */public class Test {    public static void main(String[] aargs){        Guess gu=new Guess();        gu.guessnum();    }    }


0 0
原创粉丝点击