簡體   English   中英

如何使輸入不讀取特定數字

[英]how to make the input not read a certain number

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter number (0 to quit): ");
        //largest to-do
        double largest = scan.nextDouble();
        double count = 0;

        while (largest != 0) {
            double input = scan.nextDouble();
            //max
            if (input > largest) {
                // not zero
                // while (input > largest){
                //
                // }

                largest = input;
                //counter
                count = 0;

            }
            //counter start
            if(input==largest){
                count++;
            }
            if (input == 0) {
                System.out.println("Largest #: " + largest);
                System.out.println("Occurance: " + count);
            }
        }
    }
}

該程序有效! 但是,我認為它並不完整...就像用戶嘗試輸入

-17 -5 -2 -1 -1 -1 0

它輸出:

Max: 0
Occurrence: 1

雖然:在我看來,我希望它是:

Max: -1
Occurrence: 3 

我將如何在不使用數組的情況下做到這一點? 我在櫃台上方開始的那部分代碼中就知道了。

提前致謝。

為什么不這樣呢?

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.print("Enter number (0 to quit): ");

        double largest = 0, nextDouble;
        int count = 0;

        while ((nextDouble = scan.nextDouble()) != 0) {
            if (nextDouble > largest || largest == 0) {
                largest = nextDouble;
                count = 1;
            }
            else if (nextDouble == largest) count++;
        }

        scan.close();

        if (count == 0) {
            System.out.println("No number entered!");
            return;
        }

        System.out.println("Largest #: " + largest);
        System.out.println("Occurance: " + count);

    }
}

暫無
暫無

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

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