猜数字游戏代码优化
作者:互联网
package com.cfqp;
import java.util.Random;
import java.util.Scanner;
//猜数字游戏代码优化版
public class demo51 {
public static void main(String[] args) {
Random r = new Random();
int [] arr = new int[5];
for (int i = 0; i < arr.length; i++) {
int i1 = r.nextInt(20) + 1;
arr[i]=i1;
}
Scanner sc = new Scanner(System.in);
OUT:
while(true){
System.out.println("请输入你猜的数字:");
int i = sc.nextInt();
for (int i1 = 0; i1 < arr.length; i1++) {
if(i==arr[i1]){
System.out.println("数据的第一个位序为:"+(i1+1));
System.out.println("运气不错,猜中了");
break OUT; //结束整个While循环
}
}
System.out.println("猜错了,请继续!!!");
}
System.out.println("随机生成的5个数为:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+"\t");
}
}
}
标签:arr,游戏,int,System,i1,代码优化,println,out,数字 来源: https://blog.csdn.net/cfqp123456/article/details/123608132