簡體   English   中英

有時循環打印兩次

[英]For loop printing twice sometimes

在我的convertEuro方法中,for循環導致輸出打印兩次,但有時只打印。 我的意思是這是顯示的內容:

Converting values to Euros.
£4.00 >>> €5.45
£123.44 >>> €168.13
Converting values to Euros.
£4.00 >>> €5.45
£123.44 >>> €168.13

在測試期間,它似乎可以在10次中做到2次,我無法弄清楚原因。 以下代碼,如果有人可以請求幫助:

    import java.util.Scanner;
import java.text.DecimalFormat;
import java.util.ArrayList;
public class Conversion {

    public void mainMenu(Scanner scan, ArrayList<Double> values, DecimalFormat twoDecimal) {

        int menuChoice;

        System.out.println("1. Enter values and type -1 to stop");
        System.out.println("2. Euros");
        System.out.println("3. Dollars");
        System.out.println("4. Yen");
        System.out.println("5. Rupees");
        System.out.println("6. Exit");

        menuChoice = scan.nextInt();

        switch (menuChoice) {
        case 1:

            enterValues(scan, values, twoDecimal);

        case 2:

            scan.nextLine();
            convertEuro(scan, values, twoDecimal);

        }

    }

    public void enterValues(Scanner scan, ArrayList<Double> values, DecimalFormat twoDecimal) {
        double value = 0;

        do {
            System.out.print("Enter value. Enter -1 to stop: £");



            while (!scan.hasNextDouble()) {
                System.out.print("Please enter a double (£xx.xx): £");
                scan.nextLine(); //Consumes \n
                scan.next();
            }

            value = scan.nextDouble();

            if (value != -1) {
                values.add(value);
                System.out.println("Value entered.");
            }

        }
        while (value != -1);


        System.out.println("Returning to main menu. ");
        mainMenu(scan, values, twoDecimal);

    }

    public void convertEuro(Scanner scan, ArrayList<Double> values, DecimalFormat twoDecimal) {

        System.out.println("Converting values to Euros.");

        for (int i = 0; i < values.size(); i++) {
            System.out.println("£" + twoDecimal.format(values.get(i)) + " >>> " + "\u20ac" + twoDecimal.format(values.get(i) * 1.362));
        }
    }

    public static void main(String[] args) {

        Conversion conv = new Conversion();
        Scanner scan = new Scanner(System.in);
        ArrayList<Double> values = new ArrayList<Double>();
        DecimalFormat twoDecimal = new DecimalFormat("0.00");

        conv.mainMenu(scan, values, twoDecimal);

        scan.close();

    }

}

你需要添加break; 聲明:

case 1:
    enterValues(scan, values, twoDecimal);
    break;

switch語句遇到正確的情況時,它會執行它並繼續執行其他情況,直到並且除非它達到break語句。

正確的過程是

 switch (menuChoice) {
        case 1:
            enterValues(scan, values, twoDecimal);
            break;
        case 2:
            scan.nextLine();
            convertEuro(scan, values, twoDecimal);
            break;
        }

這就是為什么最佳實踐是在每個案例結束后放置break語句。

謝謝

暫無
暫無

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

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