简体   繁体   English

我正在尝试存储和读取用户输入的数字数组

[英]I am trying to store and read an Array of number that are input by a user

I am hoping it looks like this:我希望它看起来像这样:

Lottery analysis

How many days will you analyze? 5

Day 1: 123
Day 2: 1234
too many digits
Day 2: abc
non-numeric data

Day 2: 456
Day 3: 789
Day 4: 012
Day 5: 345

Frequencies

0: 1
1: 2
2: 2
3: 2
4: 2
5: 2
6: 1
7: 1
8: 1
9: 1

public static void main(String[] args) {
    System.out.println("Lottery Analysis\n");

    // Create Scanner
    Scanner days = new Scanner(System.in);

    // Ask the user for how many days they will analyze
    System.out.print("How many days will you analyze? ");
    int analyze = days.nextInt();

    // Ask the user to enter 3 digits for each day
    System.out.println("\nEnter 3 digits for each day \n");
    Scanner scanDigits = new Scanner(System.in);
    int i = 1;

    // Make a loop that prints the number of days so the user can input the digits each time into that day
    String digits3;
    do {
        System.out.print("Day " + i + ": ");
        digits3 = scanDigits.nextLine();

        // Make sure the string used holds only 3 digits
        if (isNumeric(digits3)) {
            i++;
            // Store all digits used in digits3 in an array
            int[] frequencies = new int[10];
            int val = Character.digit(digits3.charAt(0),10);
        }
    }
    while (i <= analyze);

    // Print the Numbers and Frequencies from 0 to 9
    System.out.println("\nFrequencies\n");
    System.out.println(val);
}

public static boolean isNumeric(String digits3)
{
    boolean ret = !digits3.equals("");
    for(int i = 2; i < digits3.length(); i++)
    {
        // Checks if the characters in digits3 are only digits
        if (!Character.isDigit(digits3.charAt(i)))
        {
            System.out.println("non-numeric data");
            ret = false;
        }
    }
    // Tells the user if the characters they input are not over 3 digits
    for (int j = 3; j < digits3.length(); j++)
    {
        System.out.println("too many digits");
        ret = false;
    }
    return ret;
}

First the code.首先是代码。 Then the explanations.然后是解释。

import java.util.Scanner;

public class LotteryAnalysis {

    public static boolean isNumeric(String digits3) {
        boolean ret = !digits3.equals("");
        for (int i = 2; i < digits3.length(); i++) {
            // Checks if the characters in digits3 are only digits
            if (!Character.isDigit(digits3.charAt(i))) {
                System.out.println("non-numeric data");
                ret = false;
            }
        }
        // Tells the user if the characters they input are not over 3 digits
        for (int j = 3; j < digits3.length(); j++) {
            System.out.println("too many digits");
            ret = false;
        }
        return ret;
    }

    public static void main(String[] args) {
        System.out.println("Lottery Analysis\n");

        // Create Scanner
        Scanner days = new Scanner(System.in);

        // Ask the user for how many days they will analyze
        System.out.print("How many days will you analyze? ");
        int analyze = days.nextInt();

        // Ask the user to enter 3 digits for each day
        System.out.println("\nEnter 3 digits for each day \n");
        Scanner scanDigits = new Scanner(System.in);
        int i = 1;

        // Store all digits used in digits3 in an array
        int[] frequencies = new int[10];

        // Make a loop that prints the number of days so the user can input the digits each time into that day
        String digits3;
        do {
            System.out.print("Day " + i + ": ");
            digits3 = scanDigits.nextLine();

            // Make sure the string used holds only 3 digits
            if (isNumeric(digits3)) {
                i++;
                char[] digits = digits3.toCharArray();
                for (char digit : digits) {
                    int index = digit - '0';
                    frequencies[index]++;
                }
            }
        }
        while (i <= analyze);

        // Print the Numbers and Frequencies from 0 to 9
        System.out.println("\nFrequencies\n");
        for (int j = 0; j < frequencies.length; j++) {
            System.out.printf("%d: %d%n", j, frequencies[j]);
        }
    }
}

I only changed code in method main .我只更改了方法main中的代码。

  1. The frequencies array needs to be declared before the do-while loop and not inside the loop. frequencies数组需要在do-while循环之前声明,而不是在循环内部。 Note that Java initializes each element in the array to zero.请注意,Java 将数组中的每个元素初始化为零。
  2. Inside the do-while loop you need to update the frequencies array.do-while循环中,您需要更新frequencies数组。
  3. You want to get the frequencies of all the digits but in your code you are only checking the first digit in each three-digit number that the user enters.您想获取所有数字的频率,但在您的代码中,您只检查用户输入的每个三位数中的第一个数字。
  4. After the do-while loop terminates, you want to print all the elements in frequencies .do-while循环终止后,您想要打印frequencies中的所有元素。 In your code you are printing just the last, three-digit number that was entered by the user.在您的代码中,您只打印用户输入的最后一个三位数字。
  5. You only really need one Scanner (and not two) but if you use a single Scanner , then you need to add a call to method nextLine before you enter the do-while loop.您实际上只需要一个Scanner (而不是两个),但如果您使用一个Scanner ,那么您需要在进入do-while循环之前添加对方法nextLine的调用。 Refer to Scanner is skipping nextLine() after using next() or nextFoo()参考Scanner is skipping nextLine() after using next() or nextFoo()
  6. The code in your question does not compile because you reference [local] variable val outside of its scope.您问题中的代码无法编译,因为您在其 scope 之外引用了 [local] 变量val You declare it inside the following if statement (inside the do-while loop):您在以下if语句中声明它(在do-while循环中):
if (isNumeric(digits3)) {

Hence its scope is the block associated with the if statement and you reference it after the do-while loop.因此,它的 scope 是与if语句关联的块,您可以在do-while循环之后引用它。 The variable val only exists in the if statement block.变量val仅存在于if语句块中。 In any case, the variable is not required and does not appear in my code, above.无论如何,该变量不是必需的,也没有出现在我的代码中,上面。

Below is output from a sample run.下面是来自示例运行的 output。

Lottery Analysis

How many days will you analyze? 5

Enter 3 digits for each day 

Day 1: 123
Day 2: 1234
too many digits
Day 2: abc
non-numeric data
Day 2: 456
Day 3: 789
Day 4: 012
Day 5: 345

Frequencies

0: 1
1: 2
2: 2
3: 2
4: 2
5: 2
6: 1
7: 1
8: 1
9: 1

Edit编辑

I didn't really look at the code of method isNUmeric , since it gave correct result for sample data in your question, but looking at it now, I see that it may give incorrect result, for example for the string a12 , the method will return true and this is wrong.我并没有真正查看方法isNUmeric的代码,因为它为您的问题中的示例数据提供了正确的结果,但是现在查看它,我发现它可能会给出不正确的结果,例如对于字符串a12 ,该方法将返回true ,这是错误的。 Here is my implementation:这是我的实现:

public static boolean isNumeric(String digits3) {
    boolean ret;
    try {
        Integer.parseInt(digits3);
        ret = digits3.length() == 3;
        if (!ret) {
            System.out.println("Please enter a three digit number.");
        }
    }
    catch (NumberFormatException x) {
        System.out.println("non-numeric data");
        ret = false;
    }
    return ret;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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