- <div>public class Systemin {</div><div>
- </div><div><span class="Apple-tab-span" style="white-space:pre"> </span>public static void main(String[] args) {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Random r = new Random();</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>int num = r.nextInt(101);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>int count = 0;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>String regex = "^[0-9]*$";</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Pattern p = Pattern.compile(regex);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println(num);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>boolean flag = false;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("请输猜想的数字(数字为0-100之间,最多10次机会):");</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>while(!flag){</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Scanner sc = new Scanner(System.in);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>String inNum = sc.nextLine();</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>if(p.matcher(inNum).matches()){</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>count++;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>if(count>10 || count ==10){</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>flag = true;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("猜想次数到达上线,程序结束。。。");</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>if(num==Integer.parseInt(inNum)){</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>flag = true;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("恭喜你输入正确,程序结束。。。");</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}else if(num>Integer.parseInt(inNum)){</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("输入的数字小于随机数,请重新输入:");</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}else if(num<Integer.parseInt(inNum)){</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("输入的数字大于随机数,请重新输入:");</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}else{</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("输入的不是数字,不计入猜数成绩,请重新输入:");</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div>}</div>
复制代码
|
|