键盘输入实现猜数字游戏
import java.util.Scanner; import java.util.Random; public class GuessNum3{ public static void main(String[] args){ System.out.println("猜数字游戏"); System.out.println("请输入一个1~100之间的整数"); Random r=new Random(); int ranNum=r.nextInt(100)+1; Scanner sc=new Scanner(System.in); for(int i=1;i<=10;i++){ int guessNum=sc.nextInt(); if(ranNum==guessNum){ System.out.println("恭喜你答对了"); break; }else if(guessNum>ranNum){ System.out.println("大了"); }else if(guessNum<ranNum){ System.out.println("小了"); } System.out.println("您的次数已经用完了"); if(i==10){ break; } System.out.println("你还剩"+(10-i)+"次机会"); } } }