簡體   English   中英

帶哨兵循環的switch語句

[英]switch statement with sentinel loop

我的求和函數無法正常工作。 我正在嘗試創建一個程序,該程序使用開關結構和標記循環來詢問產品編號和數量。 它將一直運行,直到輸入0。 它應該計算輸入的產品總數和所有輸入的產品總價值。 數量很好。 這是僅適用於輸入的第一個產品的總價值。 直到按下0,我無法獲得總計的總和。 任何幫助將不勝感激!!

import java.util.Scanner;

   public class Mailorder {

      public static void main(String[] args) {

    //create a scanner
    Scanner input = new Scanner(System.in);

    //declare variables

    double product1 = 3.75;
    double product2 = 5.95;
    double product3 = 8.75;
    double product4 = 6.92;
    double product5 = 8.75;
    double product6 = 7.87;
    double total = 0.00;

    //read in product # 
    System.out.print("Enter a product number: ");
        int product = input.nextInt();

    //read in quantity sold
    System.out.print("Enter quantity sold for 1 day: ");
        int quantity = input.nextInt();

    //switch case
    switch (product) 
      {
        case 1: total = product1 * quantity; break;
        case 2: total = product2 * quantity; break;
        case 3: total = product3 * quantity; break;
        case 4: total = product4 * quantity; break;
        case 5: total = product5 * quantity; break;
        case 6: total = product6 * quantity; break;
      default: System.out.println("ERROR: Invalid product number");
      }

    //keep reading data until the input is 0
    int sum1 = 0;
            while (quantity != 0) {
                     sum1 += quantity;

    int sum2 = 0;
            while (total != 0) {
                    sum2 +=total;
        }
     //read the next data
            System.out.print("Enter a product number: ");
                    product = input.nextInt();

            System.out.print("Enter quantity sold for 1 day: ");
                    quantity = input.nextInt();
    }

    //print results
    System.out.println("The total number of products sold last week " + sum1);
    System.out.println("The total retail value of all products sold last week " + sum2);

}
}

這里有幾個范圍界定問題。

首先,將switch語句放置在循環之外。 您應該將其放入循環中。

其次, sum2存在一個范圍界定問題。 它在哨兵循環中聲明,但在外部引用。 我不確定為什么您要在sum2添加一個嵌套循環。 這是解決了這些問題的代碼:

    public class Mailorder {

    public static void main(String[] args) {

        //create a scanner
        Scanner input = new Scanner(System.in);

        //declare variables

        double product1 = 3.75;
        double product2 = 5.95;
        double product3 = 8.75;
        double product4 = 6.92;
        double product5 = 8.75;
        double product6 = 7.87;

        //read in product #
        System.out.print("Enter a product number: ");
        int product = input.nextInt();

        //read in quantity sold
        System.out.print("Enter quantity sold for 1 day: ");
        int quantity = input.nextInt();


        //keep reading data until the input is 0
        int sum1 = 0;
        int sum2 = 0;
        while (quantity != 0) {
            sum1 += quantity;

            double total = 0.00;
            //switch case
            switch (product)
            {
                case 1: total += product1 * quantity; break;
                case 2: total += product2 * quantity; break;
                case 3: total += product3 * quantity; break;
                case 4: total += product4 * quantity; break;
                case 5: total += product5 * quantity; break;
                case 6: total += product6 * quantity; break;
                default: System.out.println("ERROR: Invalid product number");
            }
            sum2 += total;

            //read the next data
            System.out.print("Enter a product number: ");
            product = input.nextInt();

            System.out.print("Enter quantity sold for 1 day: ");
            quantity = input.nextInt();
        }


        //print results
        System.out.println("The total number of products sold last week " + sum1);
        System.out.println("The total retail value of all products sold last week " + sum2);

    }
}

暫無
暫無

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

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