簡體   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