Java-在while循环的最后一行打印所有随机数

时间:2018-10-31 02:22:59

标签: java random

我的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个匹配数字

3 个答案:

答案 0 :(得分:0)

否则,您可以在将它们转换为字符串后将它们存储在字符串变量中

  String randomNums = "";
  randomNums += randomNums +" - "+ String.ValueOf(randomNumber); 

答案 1 :(得分:0)

我认为应该是这样,因为您不想将数字存储在数组或数组列表中,因此必须使用字符串连接。

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));

我希望这可以为您提供解决方法的思路

答案 2 :(得分:0)

这是一段代码, 只需使用字符串变量来隐藏随机数

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);
     }
}

输出:

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