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



我希望它看起来像这样:

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 Analysisn");
// 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("nFrequenciesn");
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;
}

首先是代码。然后是解释。

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 Analysisn");
// 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("nFrequenciesn");
for (int j = 0; j < frequencies.length; j++) {
System.out.printf("%d: %d%n", j, frequencies[j]);
}
}
}

我只更改了方法main中的代码。

  1. frequencies数组需要在do-while循环之前声明,而不是在循环内部声明。请注意,Java将数组中的每个元素初始化为零
  2. do-while循环中,您需要更新frequencies数组
  3. 你想得到所有数字的频率,但在你的代码中,你只检查用户输入的每个三位数中的第一个数字
  4. do-while循环结束后,您需要打印frequencies中的所有元素。在您的代码中,您只打印用户输入的最后一个三位数
  5. 您实际上只需要一个Scanner(而不是两个(,但如果使用单个Scanner,则需要在进入do-while循环之前添加对方法nextLine的调用。请参阅扫描仪在使用next((或nextFo((后跳过nextLine((
  6. 问题中的代码没有编译,因为您引用了超出其作用域的[局部]变量val。您可以在以下if语句中声明它(在do-while循环中(:
if (isNumeric(digits3)) {

因此,它的作用域是与if语句相关联的块,并且您在do-while循环之后引用它。变量val仅存在于if语句块中。在任何情况下,变量都不是必需的,也不会出现在上面的代码中。

以下是示例运行的输出。

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

编辑

我并没有真正查看方法isNUmeric的代码,因为它为您的问题中的样本数据给出了正确的结果,但现在查看它,我发现它可能给出了错误的结果,例如,对于字符串a12,该方法将返回true,这是错误的。这是我的实现:

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

最新更新