我的程序有以下要求:
到目前为止我已经这样做了:
import java.util.Scanner;
public class Countingintegers
{
public static void main(String[] args) {
int[] inputArray = new int[51];
Scanner keyboard = new Scanner(System.in);
int[] frequency = new int[50];
for(int i = 0; i < inputArray.length; i++)
{
System.out.println("Input: ");
inputArray[i] = keyboard.nextInt();
if (inputArray[i] > 0 || inputArray[i] <= 50)
{
++frequency[inputArray[i]];
}
else
{
break;
}
}
System.out.printf("%s%10s%n", "Number", "Frequency");
for (int number = 1; number < frequency.length; number++)
System.out.printf("%6d%10d%n", number, frequency[number]);
}
}
我的目标是否实现了?
答案 0 :(得分:1)
您也可以使用Map
轻松完成此操作。如果您使用TreeMap
,则会对输入进行排序。我还建议不要将所有代码放在main方法中。您应该在类中创建公共方法,并创建类的实例以调用方法,将主要代码保持在最低限度,最多用于收集输入。
以下是一个示例,利用地图来跟踪出现次数:
import java.util.Scanner;
import java.util.*;
public class Countingintegers{
private int min, max;
private Map<Integer,Integer> inputs = new TreeMap<>();
public Countingintegers(int min, int max){
this.min = min;
this.max = max;
}
public void addInt(Integer value) throws IllegalArgumentException{
if(value < min || value > max){
throw new IllegalArgumentException();
}
if(inputs.containsKey(value)){
inputs.put(value, inputs.get(value)+1);
}
else{
inputs.put(value, 1);
}
}
public void printCounts(){
System.out.printf("%s%10s%n", "Number", "Frequency:");
System.out.println(inputs);
}
public static void main(String[] args) {
int totalInputs = 50;
int numIntsLoaded = 0;
int input = -1;
Scanner keyboard = new Scanner(System.in);
Countingintegers counter = new Countingintegers(1, 50);
while(numIntsLoaded < totalInputs && input != 0){
System.out.println("Input a number between 1-50, or 0 to exit (" + (totalInputs-numIntsLoaded) + " to go):");
try{
input = keyboard.nextInt();
counter.addInt(input);
numIntsLoaded++;
}
catch(Exception e){
System.out.println("You entered an invalid input. Please try again!");
keyboard.nextLine();
}
}
counter.printCounts();
}
}
答案 1 :(得分:0)
我在代码中的评论会对你有所帮助,你的代码是一个非常可靠的尝试。你真的太复杂了。下次尝试更简单地思考。你只需要一个数组,
import java.util.Scanner;
public class Countingintegers {
public static void main(String[] args) {
//Hi my name is COMMENTS please use me
@SuppressWarnings("resource")
//Please name your scanner objects scanner, its not for you but for other programmers. Gives us a headache when you dont.
Scanner scanner = new Scanner(System.in);
//You only need one array...
int[] frequency = new int[50];
System.out.println("Input: ");
//This for loop goes through 51 iterations of our array.
for (int i = 0; i < 51; i++) {
//Make a num variable so you do not have to keep on typing scanner.nextInt(), also I recomend you get familiar with variable scope.
Integer num = scanner.nextInt();
//You originally made this statement incorrectly. It needs to be between 0 and 50 right? || is for or (explicit) you better learn what explicit and implict for || (bitwise or) is too while your learning...
if (num > 0 && num <= 50)
//frequency[num] just represents the slot in the frequency array. A tip, an array will give you zeros if you don't assign numbers to all the slots. So frequency[10], the 11th slot in your array will be equal to zero when you create it.
frequency[num] = frequency[num] + 1;
//In programming your not really supposed to use breaks (I have honestly no idea why, but all the senior programmers keep telling me so...), but you are def a beginner so don't worry about it for now.
if (num== 0) break;
}
//The rest is history
System.out.printf("%s%10s%n", "Number", "Frequency");
for (int number = 1; number < frequency.length; number++)
System.out.printf("%6d%10d%n", number, frequency[number]);
}
}
答案 2 :(得分:0)
尝试更改代码:
if (inputArray[i] > 0 || inputArray[i] <= 50)
{
++frequency[inputArray[i]];
}
else
{
break;
}
这一个:
if (inputArray[i] == 0) {
break;
}
if (inputArray[i] < 0 || inputArray[i] > 50) {
System.out.printf("Value enterd ouy of range [1-50]!");
throw new IllegalArgumentException("Value enterd ouy of range [1-50]!");
}
++frequency[inputArray[i]];
它会
terminate input
在0输入上,如果输入值超出预期范围,则抛出异常。