代码执行完毕后输出一堆零。

huangapple go评论66阅读模式
英文:

Output of a bunch of zeros after code is finished

问题

我有问题与代码打印匹配的数字。我的终端结果如下:

Choose Five Numbers: 5 6 7 8 99
Choose Mega Ball: 56
You Chose Following:
5 6 7 8 99 Mega Ball - 56.
DRAW RESULT:
15 14 5 11 19 Mega Ball - 14.
Matched Numbers are:
You are a Winner!!
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

我不确定我是否漏掉了什么,或者我的代码是否全部按正确的顺序排列,或者我是否放错了括号,导致某些内容无法读取。谢谢。

import java.util.Random;
import java.util.ArrayList;
import java.util.Scanner;

public class Class1KRP {
    public static void main(String[] args) {
        int[] randomNumbers = new int[6]; // 存储6个随机数字
        int[] inputNumbers = new int[6]; // 存储用户输入的6个数字
        ArrayList<Integer> equalNumbers = new ArrayList<Integer>(); // 存储匹配的数字
        Random rand = new Random();

        Scanner in = new Scanner(System.in);

        System.out.print("Choose Five Numbers: ");
        int[] numbers = new int[6];
        for (int i = 0; i < 5; i++) {
            numbers[i] = in.nextInt();
        }
        System.out.print("Choose Mega Ball: ");
        numbers[5] = in.nextInt();
        System.out.println("You Chose Following:");
        for (int i = 0; i < 5; i++) {
            System.out.print(numbers[i] + " ");
        }
        System.out.println("Mega Ball - " + numbers[5] + ".");

        // 生成1到20范围内的6个随机数字
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = rand.nextInt(20) + 1;
        }
        System.out.println("DRAW RESULT:");
        for (int i = 0; i < 5; i++) {
            System.out.print(numbers[i] + " ");
        }
        System.out.println("Mega Ball - " + numbers[5] + ".");

        // 比较随机数字数组和用户输入数字数组
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                if (randomNumbers[i] == inputNumbers[j]) { // 比较匹配的数字
                    equalNumbers.add(randomNumbers[i]); // 将匹配的数字添加到ArrayList中
                }
            }
        }
        // 打印最终输出
        if (!equalNumbers.isEmpty()) { // 至少有一个匹配的数字
            System.out.println("Matched Numbers are:");
            System.out.println("You are a Winner!!");
            for (int i : equalNumbers) {
                System.out.println(i); // 打印匹配的数字列表
            }
        } else { // 如果没有匹配的数字
            System.out.println("You are a Loser.");
        }
    }
}

注意:我已经修改了代码中的一些错误,但是仍然有一些问题需要解决。这段代码仍然可能不会输出所期望的结果。

英文:

I am having issues with the code printing the Matched numbers. The result of my terminal looks like:

Choose Five Numbers: 5 6 7 8 99
Choose Mega Ball: 56
You Chose Following:
5 6 7 8 99 Mega Ball - 56.
DRAW RESULT: 
15 14 5 11 19 Mega Ball - 14.
Matched Numbers are: 
You are a Winner!!
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

I am not sure what I am missing or if my code isn't all in the correct order or if I'm misplacing brackets and it's not reading something. Thanks

import java.util.Random;
import java.util.ArrayList;
import java.util.Scanner;
public class Class1KRP	 {
public static void main (String[]args)
{
int[] randomNumbers = new int[6]; // Stores 6 random numbers
int[] inputNumbers = new int[6]; // Stores 6 numbers given by user
ArrayList&lt;Integer&gt; equalNumbers = new ArrayList&lt;Integer&gt;(); // Stores matched numbers
Random rand = new Random();
Scanner in = new Scanner(System.in);
System.out.print(&quot;Choose Five Numbers: &quot;);
int[] numbers = new int[6];
for (int i = 0; i &lt; 5; i++) {
numbers[i] = in.nextInt();
}
System.out.print(&quot;Choose Mega Ball: &quot;);
numbers[5] = in.nextInt();
System.out.println(&quot;You Chose Following:&quot;);
for (int i = 0; i &lt; 5; i++) {
System.out.print(numbers[i] + &quot; &quot;);
}
System.out.println(&quot;Mega Ball - &quot; + numbers[5] + &quot;.&quot;);
//Generates 6 Random Numbers in the range 1 -20
for(int i = 0; i &lt; numbers.length; i++) {
numbers[i] = (int)(Math.random()*20 + 1);
}
System.out.println(&quot;DRAW RESULT: &quot;);
for (int i = 0; i &lt; 5; i++) {
System.out.print(numbers[i] + &quot; &quot;);
}
System.out.println(&quot;Mega Ball - &quot; + numbers[5] + &quot;.&quot;);
//Comparing random numbers array and user input numbers array
for (int i=0;i&lt;5;i++)
{
for (int j=0;j&lt;5;j++)
{
if (randomNumbers[i] == inputNumbers[j]) { // Comparing matched numbers
equalNumbers.add(randomNumbers[i]); // Adding matched number to Array list
}
}
}
// Printing final output
if (equalNumbers.size() != 0) { // At least one matched number is there
System.out.println(&quot;Matched Numbers are: &quot;);
System.out.println(&quot;You are a Winner!!&quot;);
for(int i:equalNumbers)
{
System.out.println(i); // Printing list of matched numbers
}
}
else { // If no matched numbers
System.out.println(&quot;You are a Loser.&quot;);
}
}
}

答案1

得分: 1

你从不使用你的两个数组randomNumbersinputNumbers。相反,你总是使用numbers。因此,在比较前两个数组时,它们只包含零,因此始终相等。

另外,在检查相等性时,你生成了所有ij索引的排列组合。但你实际上只对i == j的情况感兴趣,第二个嵌套循环是多余的。这就是为什么它打印了25个零,而不是仅打印五个零。将其更改为以下内容:

for (int i = 0; i < 5; i++) {
    if (randomNumbers[i] == inputNumbers[i]) { 
        equalNumbers.add(randomNumbers[i]); 
    }
}

最后,注意循环只有五次迭代。不确定是否有意,但这样你就不会比较"Megaball"号码。

英文:

You never use your two arrays randomNumbers and inputNumbers. Instead, you always use numbers. So, when comparing your first two arrays, they contain only zeros, and hence are always equal.

Also, when checking for equality, you generate all permutations of the indices i and j. But you are only really interested in the cases where i == j, the second, nested loop is redundant. This is why instead of only printing five zeros, it prints 25 zeros. Change it two the following:

for (int i = 0; i &lt; 5; i++) {
if (randomNumbers[i] == inputNumbers[i]) { 
equalNumbers.add(randomNumbers[i]); 
}
}

Finally, notice how the loop only has five iterations. Not sure if that is intended, but this way you do not compare the "Megaball".

huangapple
  • 本文由 发表于 2020年9月16日 22:02:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63921748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定