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



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



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();
    }
    
}


你可能感兴趣的:(Java)