JAVA编程题目 求大神解答
键盘输入一个数字之后开始执行循环输出随机数到控制台要求:有输入提示语、随机数、控制台输出提示:hasNext()方法是判断是否有键盘输入值...
键盘输入一个数字之后开始执行循环输出随机数到控制台
要求:有输入提示语、随机数、控制台输出
提示:hasNext()方法是判断是否有键盘输入值 展开
要求:有输入提示语、随机数、控制台输出
提示:hasNext()方法是判断是否有键盘输入值 展开
展开全部
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try {
System.out.println("请输入5个数,用空格分割");
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
String[] numArr = str.split(" ");
boolean status = true;
System.out.println("5s后,每隔1s进行一次抽奖,请稍后");
Thread.sleep(5000);
while (status) {
String[] arr = new String[5];
for (int i = 0; i < 5; i++) {
arr[i] = getNum();
}
if (Arrays.equals(numArr, arr)) {
status = false;
}else {
System.out.println("抱歉未中奖,1s后再次开奖。本次中奖号码:"+Arrays.toString(arr));
Thread.sleep(1000);
}
}
if(!status) {
System.out.println("这一行基本你看不到,如果看到恭喜你可以期买彩票了!!!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getNum() {
int max = 23;
Random random = new Random();
int num = random.nextInt(max);
if (num >= 10) {
return num + "";
} else {
return "0" + num;
}
}
}
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try {
System.out.println("请输入5个数,用空格分割");
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
String[] numArr = str.split(" ");
boolean status = true;
System.out.println("5s后,每隔1s进行一次抽奖,请稍后");
Thread.sleep(5000);
while (status) {
String[] arr = new String[5];
for (int i = 0; i < 5; i++) {
arr[i] = getNum();
}
if (Arrays.equals(numArr, arr)) {
status = false;
}else {
System.out.println("抱歉未中奖,1s后再次开奖。本次中奖号码:"+Arrays.toString(arr));
Thread.sleep(1000);
}
}
if(!status) {
System.out.println("这一行基本你看不到,如果看到恭喜你可以期买彩票了!!!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getNum() {
int max = 23;
Random random = new Random();
int num = random.nextInt(max);
if (num >= 10) {
return num + "";
} else {
return "0" + num;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询