简体   繁体   English

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

[英]Java - printing all random numbers at the last line of the while loop

I have the while loop running and processing properly. 我有while循环运行并正确处理。 When the numbers that the user entered do not match the random number, the code prints out "Sorry ...(and proceeds to explain what the right number is)" each time the user is wrong. 当用户输入的数字与随机数不匹配时,每次用户输入错误时,代码都会打印“对不起...(并继续说明正确的数字是什么)”。 However, I cannot get the code to print out those same exact random numbers at the very end of the last loop before it terminates. 但是,我无法获得代码以在终止之前的最后一个循环的最后打印出完全相同的随机数。 Any suggestions? 有什么建议么?

   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 + ".");
        }

The bottom of the code has the winning number, but I want all of those same exact random numbers (which were randomized earlier) to show up at the end of the sixth loop. 代码的底部有中奖号码,但我希望所有这些完全相同的随机数(之前已被随机分配)显示在第六个循环的末尾。 Also, the order of the user input does not influence the results. 同样,用户输入的顺序也不影响结果。 If the user chooses 1-13-8-34-56-2 and the computer had come up with 1-8-56-2-14-34…there would still be 5 matching numbers 如果用户选择1-13-8-34-56-2并且计算机拿出1-8-56-2-14-34…仍然有5个匹配数字

otherwise you can store them in a string variable after casting them to string by doing this 否则,可以通过将它们转换为字符串后将它们存储在字符串变量中

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

i think it would be something like this, since you do not want store the numbers in an array or array list, you will have to work with string concatenation. 我认为应该是这样,因为您不想将数字存储在数组或数组列表中,因此必须使用字符串连接。

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

i hope this gives you an idea on how to solve it 我希望这能给您一个解决方法的想法

Here is the piece of code, Just keep concating the random numbers with a string variable 这是一段代码,只需保持随机数与字符串变量一致即可

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: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM