import java.util.Scanner; public class caishuziyx { public static void main(String[] args) { System.out.println("---------猜数字游戏----------"); caishuziyx n=new caishuziyx(); n.show(); } int jiag; int n; int u; Scanner input = new Scanner(System.in); public void show() { int shuij1 = (int) (Math.random() * (100)) ; for (int i = 0; i < 4; i++) { System.out.print("请输入要猜的数字:"); jiag = input.nextInt(); if (jiag != shuij1) { n += 1; u += 1; } else { System.out.println("猜对了"); break; } if (jiag > shuij1 && u < 4) { System.out.println("再小点\n\n再猜一次吧"); } else if (jiag < shuij1 && u < 4) { System.out.println("再大点\n\n再猜一次吧"); } if (n == 4) { System.out.println("\n4次内没有猜对,下次努力吧!"); } } } }
执行结果: