[英]Java - printing all random numbers at the last line of the while loop
我有while循環運行並正確處理。 當用戶輸入的數字與隨機數不匹配時,每次用戶輸入錯誤時,代碼都會打印“對不起...(並繼續說明正確的數字是什么)”。 但是,我無法獲得代碼以在終止之前的最后一個循環的最后打印出完全相同的隨機數。 有什么建議么?
while (counter < 6)
{
counter++;
System.out.println("Enter a number between 1-60: ");
userInput = scan.nextInt();
if (userInput > 60 || userInput < 1)
System.out.println("Invalid input");
int randomNumber = (int) (Math.random() * 60) + 1;
if (userInput == randomNumber)
System.out.println("Congrats, you have won!");
else
System.out.println("Sorry, you didn't choose the winning number." + "The winning number is " + randomNumber + ".");
}
代碼的底部有中獎號碼,但我希望所有這些完全相同的隨機數(之前已被隨機分配)顯示在第六個循環的末尾。 同樣,用戶輸入的順序也不影響結果。 如果用戶選擇1-13-8-34-56-2並且計算機拿出1-8-56-2-14-34…仍然有5個匹配數字
否則,可以通過將它們轉換為字符串后將它們存儲在字符串變量中
String randomNums = "";
randomNums += randomNums +" - "+ String.ValueOf(randomNumber);
我認為應該是這樣,因為您不想將數字存儲在數組或數組列表中,因此必須使用字符串連接。
string keepValue="";
while (counter < 6)
{
counter++;
System.out.println("Enter a number between 1-60: ");
userInput = scan.nextInt();
if (userInput > 60 || userInput < 1)
System.out.println("Invalid input");
int randomNumber = (int) (Math.random() * 60) + 1;
keepValue=keepValue+randomNumber+"-";
if (userInput == randomNumber)
System.out.println("Congrats, you have won!");
else
System.out.println("Sorry, you didn't choose the winning number." + "The
winning number is " + randomNumber + ".");
}
System.out.println(keepValue.substring(0, keepValue.length() - 1));
我希望這能給您一個解決方法的想法
這是一段代碼,只需保持隨機數與字符串變量一致即可
import java.util.Scanner;
public class RandomNumbers{
public static void main(String []args){
int counter =0;
int userInput=0;
String userEntries="";
String randomEntries="";
Scanner scan=new Scanner(System.in);
while (counter < 6)
{
counter++;
System.out.println("Enter a number between 1-60: ");
userInput = scan.nextInt();
if (userInput > 60 || userInput < 1)
System.out.println("Invalid input");
int randomNumber = (int) (Math.random() * 60) + 1;
if (userInput == randomNumber)
System.out.println("Congrats, you have won!");
else
System.out.println("Sorry, you didn't choose the winning number." + "The winning number is " + randomNumber + ".");
userEntries+=userInput+ ((counter < 5) ? "-" : "");
randomEntries+=randomNumber+((counter < 5) ? "-" : "");
}
System.out.println(userEntries);
System.out.println(randomEntries);
}
}
OUTPUT:
Enter a number between 1-60:
Sorry, you didn't choose the winning number.The winning number is 28.
Enter a number between 1-60:
Sorry, you didn't choose the winning number.The winning number is 39.
Enter a number between 1-60:
Sorry, you didn't choose the winning number.The winning number is 13.
Enter a number between 1-60:
Sorry, you didn't choose the winning number.The winning number is 13.
Enter a number between 1-60:
Sorry, you didn't choose the winning number.The winning number is 8.
Enter a number between 1-60:
Sorry, you didn't choose the winning number.The winning number is 42.
1-2-3-4-56
28-39-13-13-842
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.