我的任务是获取数字并将它们与它们的出现一起打印。我必须用整数终止进程,0
但我用字符串来做x
。
这是我的代码:
import java.util.Scanner;
public class yasf{
public static void main(String[] args) {
int[] numbers = new int[101];
int num;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number between 0-100 inclusive. x terminates:");
String numString = scan.nextLine();
// The input treminated with x;
while (!numString.equalsIgnoreCase("x")) {
num = Integer.parseInt(numString);
numbers[num] = numbers[num] + 1;
System.out.println("Enter a number between 0-100 inclusive. x terminates:");
numString = scan.nextLine();
}
for (int i = 0; i <= 100; i++) {
if (numbers[i] != 0 && numbers[i] == 1)
System.out.println(i + ": " + numbers[i] + " time");
else if (numbers[i] > 1 && numbers[i] != 0)
System.out.println(i + ": " + numbers[i] + " times");
else
System.out.print("");
}
}
}
我怎样才能做到这一点?