我在尝试键入代码时遇到问题,这将使我的程序区分用户输入的是字符串值还是 int 值。如果输入了一个 int 值,它将被存储到一个数组(命名数据)中,可以通过输入字符串值来打印和测试(如下面的代码中的方法所示go()
。我使用.hasNextInt
不正确吗?这是我的代码:
import java.util.Scanner;
import java.util.Random;
import java.text.DecimalFormat;
public class IntegerStatistics {
java.util.Scanner scan;
// declare storage for the integers
int[] data;
Random random;
// create a constructor
public IntegerStatistics() {
scan = new Scanner(System.in);
data = new int[10];
random = new Random();
}
private void showMenu() {
System.out.println("Menu:");
System.out.println(" p - Print the list of values");
System.out.println(" s - Print statistics for the values");
System.out.println(" f - Fill the list with random values");
System.out.println(" c - Clear the list of values");
System.out.println(" h - Print out this menu");
System.out.println(" x - Exit the program");
}
private void clearValues() {
System.out.print("The values: [0");
// empty (zero out) the array
int i = 1;
while(i < data.length) {
System.out.print(", 0");
i++;
} System.out.println("]");
}
private void fillList() {
data[0] = (random.nextInt(26) - 10);
System.out.print("The values: [" + data[0]);
for(int i = 1; i < data.length; i++) {
data[i] = (random.nextInt(26) - 10);
System.out.print(", " + data[i]);
} System.out.println("]");
}
private void printValues() {
data[0] = 1;
System.out.print("The values: [" + data[0]);
// print the values
for(int i = 1; i < data.length; i++) {
data[i] = i + 1;
System.out.print(", " + data[i]);
} System.out.println("]");
}
private void printStats() {
int sum = 0;
int max = data[0];
int min = data[0];
// calculate the stat values of the array
for(int i = 0; i < data.length; i++) {
// calculate sum of values
sum += data[i];
// find maximum value in array
if(data[i] > max) {
max = data[i];
// find minumum value in array
} else if(data[i] < min) {
min = data[i];
}
}
// caculate average of values
DecimalFormat df = new DecimalFormat("#.000");
double avgValue = (sum / ((double)data.length));
// print stat values of array
System.out.println("Sum of the values: " + sum);
System.out.println("Maximum value: " + max);
System.out.println("Minimum value: " + min);
System.out.printf("Average value: " + df.format(avgValue) + "\n");
}
public void go() {
System.out.println("Welcome to Simple Statistics Program\n");
String input;
int inputNum = Integer.parseInt(s);
showMenu();
int index = 0;
do {
System.out.print("Enter a command or integer: ");
input = scan.next();
inputNum = scan.nextInt();
if(inputNum.hasNextInt()) {
data[index] = input.hasNexInt();
} else if(input.equals("p")) {
printValues();
} else if(input.equals("s")) {
printStats();
} else if(input.equals("f")) {
fillList();
} else if(input.equals("c")) {
clearValues();
} else if(input.equals("h")) {
showMenu();
} else if(input.equals("x")) {
// do nothing
} else {
System.out.println("Unrecognized command. Try again.");
showMenu();
}
} while( ! input.equals("x"));
System.out.println("\nThank you for using the Simple Statistics Program");
}
public static void main(String[] args) {
new IntegerStatistics().go();
}
}
通过 int 的输入创建的数组可以部分填充,但不能超过十个整数的长度。让我知道我需要澄清。