游戏后台随机生成1-20之间的5个数,未猜中提示“未命中”,继续猜测,猜中提示“运气不错,猜中了”,并输出数据出现的第一次位置
源代码:
import java.util.Random;
import java.util.Scanner;//游戏后台随机生成1-20之间的5个数
//未猜中提示“未命中”,继续猜测
//猜中提示“运气不错,猜中了”,并输出数据出现的第一次位置
public class yxsjs {public static void main(String[] args) {System.out.println("请您输入数字");int []a=new int[5];for(int i=0;i<5;i++){Random x=new Random();a[i]= x.nextInt(20)+1;}Scanner sc=new Scanner(System.in);OUT:while(true){int b= sc.nextInt();for(int i=0;i<5;i++){if(b==a[i]){System.out.println("运气不错,猜中了");System.out.println(i+1);break OUT;}}System.out.println("未命中");}for(int i=0;i<5;i++){System.out.print(a[i]+" ");}}
}
运行截图:
