猜数字游戏
/*猜数字游戏 * * * */ public class Demo { public static void main(String[] args) { //随机产生0-100的整数 int a = (int) (Math.random()*100); int b = (int) (Math.random()*100); int sum = a+b; //提示用户输入2个整数的和 Scanner sc = new Scanner(System.in); System.out.println("请输入2个整数的和"); int c = sc.nextInt(); //猜数字 for(;;){ if(c==sum){ System.out.println("恭喜你猜对了!!"+a+"+"+b+"="+sum); break; }else if(c>=sum){ System.out.println("你猜大了"); System.out.println("请输入2个整数的和"); c = sc.nextInt(); }else{ System.out.println("你猜小了"); System.out.println("请输入2个整数的和"); c = sc.nextInt(); } } } }