用异常处理实现猜数游戏程序虽然觉得有点大材小用
但是该说不说真的觉得用更高级的方法思路一下就打开了
原代码放在如图下
package Gold13k; import java.util.Random; import java.util.Scanner; public class catch1 { public static void main(String[] args) { Random rand =new Random(); int sums = rand.nextInt(100)+1; Scanner sc = new Scanner(System.in); int temp = 0; while (true){ System.out.println("请输入数字:"); try { temp = sc.nextInt(); }catch (Exception e){ System.out.println("请输入正确的数字"); sc.next(); continue; } if (temp>sums){ System.out.println("太大了"); }else if (temp <sums){ System.out.println("太小了"); }else { System.out.println("恭喜你!答对了!"); break; } } } }