簡體   English   中英

有人能告訴我為什么每次將掃描儀讀取的fVI設置為第一個有效輸入時,fVI都返回0嗎

[英]Can somebody tell me why fVI is returning 0 every time when I am setting it to the first valid input the Scanner reads

在方法getHighestValue中,為什么fVI每次都返回0? 如何獲得將其設置為第一個有效輸入的信息? 我想最后將它與輸入的每個輸入進行比較以找到最大值。

public static void main(String[] args) {
    int numCount;
    int numScores = 0;
    int values;
    int fVS; //fVS = First Valid Score

    Scanner input = new Scanner(System.in);
    System.out.println("Enter test scores between 50 and 109");

    numScores = getCount(input);
    fVS = getHighestValue(input);
    if (numScores == 0) {
        System.out.println("No scores read");
    } else {
        System.out.println(numScores + " " + fVS);
    }
}// ends main

//Counts the number of inputs
public static int getCount(Scanner input) {
    int values;
    int numberOfScores = 0;

    while (input.hasNextInt()) {
        values = input.nextInt();

        if (values > 49 && values < 110) {
            numberOfScores++;
        }
    } // ends while loop that scans for next int and counts each score input
    return numberOfScores;
}// ends getCount

//Finds the highest value
    public static int getHighestValue(Scanner input) {
        int fVI = 0;
        int value;
        int numberOfScores = 0;


        while (input.hasNextInt()) {
            value = input.nextInt();

            if (value > 49 && value < 110) {
                fVI = value;
                numberOfScores++;

            }

        } 
        return fVI;
    }// ends getHighestValue

您有一台掃描儀,其getCount()將從中讀取整數,直到不再剩余為止。 然后, getHighestValue()還將從該掃描儀讀取整數,直到沒有整數為止。

但是, getHighestValue()的是, getCount() 已經用盡了掃描程序,因此無法再保留getHighestValue()

最好的選擇是將它們讀入一些描述的集合中,以便兩個功能都可以使用它們

因此,有一個新功能可以一次讀取掃描程序然后填充並返回(例如)一個Vector ,然后將該矢量傳遞給兩個函數進行處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM