使用数组,如何存储和显示唯一数字?

时间:2014-11-29 01:24:27

标签: java arrays unique

再次问好Stack Overflow,我现在只是在编程类中学习数组,现在它对我来说变得非常混乱,我似乎无法为它编写正确的代码。我设法完成大部分工作,我似乎无法在第25行获得if语句的正确代码,而且我在第12行也找不到符号。程序的输出应该在用户输入后显示该号码并继续一个小列表,让用户知道他们是否已输入相同的号码。输出看起来类似于:

Enter number: 11
11
Enter number: 85
11 85
Enter number: 26
11 85 26
Enter number: 11
11 has already been entered
11 85 26

到目前为止,这是我的代码:

import java.util.Scanner;

public class Unique
{
public void getNumbers()
{
    Scanner input = new Scanner(System.in);
    int[] numbers = new int [5];
    int count = 0; // unique numbers read
    int entered = 0; // entered numbers

    while( entered < numbers.length)
    {
        System.out.print("Enter number: ");
        int number = input.nextInt();

        if (number >= 10 && number <= 100)
        {
            boolean containsNumber = false;
            entered++;

            for (int a = 0; a < array.length; a++)
            numbers[a]=number;                  

            if (!containsNumber)
            {
                count++;
            }
            else
                System.out.printf("%d has already been entered\n", number);
        }
        else 
            System.out.println("number must be between 10 and 100");
            System.out.printf("%d\n",number);
            for(int value:numbers)
            System.out.printf("   %d\n", value);
    }
  }
}

有人能告诉我到底我做错了什么,因为我觉得我的陈述也是错的。

2 个答案:

答案 0 :(得分:2)

你关闭,非常关闭。你的for循环几乎完成了,你只需要把一个if放在里面。由此:

for (int a = 0; a < array.length; a++)
    numbers[a]=number;

对于这样的事情:

for (int a = 0; a < array.length; a++)
{
    if (array[a] == number)
    {
        containsNumber = true;
        break; //This exits the for loop, so you don't continue unneedlessly
    }
}

然后,你的if语句增加计数将起作用,你只需要更改它以将数字添加到数组

答案 1 :(得分:1)

for (int a = 0; a < array.length; a++)
numbers[a]=number;

假设从for周围的代码中,您可能想要检查是否已输入number。然后,它必须是

for (int a = 0; a < numbers.length; a++)
{
    if (numbers[a] == number)
    {
        containsNumber = true;
        break;
    }
}

如果用户输入尚未输入,则应附加用户输入。那么,你应该这样做。

if (!containsNumber)
{
    numbers[count] = number;
    count++;
}

此外,不需要entered变量 - numbers的大小为count,不是吗?

while (count < numbers.length)
{
    ...

最后,这段代码错了。

System.out.printf("%d\n",number);
for(int value:numbers)
System.out.printf("   %d\n", value);

虽然numbers.length 总是 5,但真实数字内容的大小为count。所以,我们应该做

for (int i = 0; i < count; i++)
{
    System.out.printf("   %d\n", numbers[i]);
}

如果已输入号码,则不应执行打印代码System.out.printf("%d\n",number);,因为System.out.printf("%d has already been entered\n", number);已打印消息。所以,它应该移到

if (!containsNumber)
{
    numbers[count] = number;
    count++;
    // here
    System.out.printf("%d\n",number);
}

因此,代码应为

import java.util.Scanner;

class Unique
{
    public void getNumbers()
    {
        Scanner input = new Scanner(System.in);
        int[] numbers = new int [5];
        int count = 0; // unique numbers read

        while (count < numbers.length)
        {
            System.out.print("Enter number: ");
            int number = input.nextInt();

            if (number >= 10 && number <= 100)
            {
                boolean containsNumber = false;

                for (int a = 0; a < numbers.length; a++)
                {
                    if (numbers[a] == number)
                    {
                        containsNumber = true;
                        break;
                    }
                }                 

                if (!containsNumber)
                {
                    numbers[count] = number;
                    count++;

                    System.out.printf("%d\n",number);
                }
                else
                {
                    System.out.printf("%d has already been entered\n", number);
                }
            }
            else 
            {
                System.out.println("number must be between 10 and 100");
            }

            for (int i = 0; i < count; i++)
            {
                System.out.printf("   %d\n", numbers[i]);
            }
        }
    }
}

(live example)

请注意我的代码格式。您的原始代码很难阅读&gt; o&lt;