繁体   English   中英

我想不明白

[英]I can not figure it out

我对程序有以下要求:

  • 编写一个程序,该程序从键盘读取1到50范围内的任意整数,然后输出每个值被读取的数量。
  • 不输出零计数。
  • 提示用户输入输入,如示例运行中所示。
  • 让用户输入0终止输入。
  • 超出1到50范围的任何其他值都将收到错误消息。

到目前为止,我已经做到了:

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]);

    }


}

我的所有目标是否都实现了?

您也可以使用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();
        }
    }

我在代码中的注释将对您有所帮助,您的代码是不错的尝试。 您真的真的太复杂了。 下次尝试更简单地思考。 您只需要一个数组,

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

}

尝试更改您的代码:

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个输入上,如果输入值超出预期范围,则引发异常。

暂无
暂无

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

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