游戏1:猜数字小游戏:
import java.util.Scanner;
class GuessNumber{public static void main(String args[]){int number = (int)(Math.random()*100)+1;System.out.println("请输入一个数据(1-100)用于猜数字。");while(true){Scanner sc = new Scanner(System.in);int guessnumber = sc.nextInt();if(guessnumber > number){System.out.println("您输入的数据过大,请重新猜一次。");}else if(guessnumber < number){System.out.println("您输入的数据过小,请重新猜一次");}else{System.out.println("恭喜你猜对了");break;}}}
}
结果展示: