当我输入2时,为什么要打印案例2?

时间:2014-08-25 04:47:21

标签: java switch-statement tostring

我正在开发一个程序,可以跟踪项目及其成本,并将其添加到您的包中。我有switch语句为1.添加新项目,2。打印总计,以及3.结束程序。

出于某种原因,当我选择案例1时,它还使用我的toString方法打印总计。但我在案例2中只有toString方法。

任何人都可以解释为什么会发生这种情况吗?

这是我的主要

import java.util.Scanner;
public class ShoppingBagTracker {

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

        float taxRate, cost;
        int items, newItems, choice;
        String receipt;
        String menu = ("1. Add items" +
        "2. Get receipt"
        + "3. Exit");

        System.out.print("Enter the sales tax rate: ");
        taxRate = in.nextFloat();
        ShoppingBag myBag = new ShoppingBag(taxRate);

        items = 0;
        do{
            System.out.println("What would you like to do?");
            System.out.println(menu);
            choice = in.nextInt();

            switch(choice){
                case 1:
                    System.out.print("Enter cost of item: ");
                    cost = in.nextFloat();     

                    System.out.print("Enter number of items: ");
                    newItems = in.nextInt();
                    items = items + newItems;

                    myBag.place(items, cost);
                    myBag.getItems();
                    myBag.getCost();
                    myBag.getTotal();
                    myBag.getAverage();
                case 2:
                   receipt = myBag.toString();
                   System.out.println(receipt);
                case 3:
                    break;
                default:
                    System.out.println("That is not an option");      
            }
        }while(choice != 3);            
    }    
}

这是我的购物袋类

public class ShoppingBag {

    public float taxRate;
    public int items;
    public float cost;
    public float average;
    public float totalCost;
    public float finalCost;

    public ShoppingBag(float taxRate)
    {
        this.taxRate = taxRate;
    }

    public void place(int newitems, float newcost)
    {
        items = newitems;
        cost = newcost;

        cost = items * cost;
    }

    public int getItems()
    {
        return items;
    }

    public float getCost()
    {
        return cost;
    }

    public float getTotal()
    {
        finalCost = cost + (cost * taxRate);
        return finalCost;
    }

    public float getAverage()
    {
        average = finalCost/items;
        return average;
    }

    @Override
    public String toString()
    {
        return("Items: " + items + " Cost: " + cost + " Total cost: " + finalCost + " Average cost: " + average);
    }
}

4 个答案:

答案 0 :(得分:3)

您需要break中的switch-cases

switch(something){
     case 1:
     // do something
     break;
     case 2:
     //do something
     break;
     default:
     // do something
     break;      

}

如果没有break,则会执行此处的所有案例。

Java中的

switch-cases

结论。

如果案件中没有return,则应对每个案例使用break

以下情况您不需要break

   switch (cases){
        case "a":
            return "a";
        case "b":
            return "b";
        default:
            return "default";
    }

答案 1 :(得分:3)

开关案例不限于执行一个'案例'。它将匹配大小写中的所有代码执行到break;或切换到结尾。

在您的情况下,在案例2之前添加break;

答案 2 :(得分:1)

您需要在案例1正文

中添加一个break语句
                case 1:
                System.out.print("Enter cost of item: ");
                cost = in.nextFloat();     

                System.out.print("Enter number of items: ");
                newItems = in.nextInt();
                items = items + newItems;

                myBag.place(items, cost);
                myBag.getItems();
                myBag.getCost();
                myBag.getTotal();
                myBag.getAverage();
                break;

这是官方tutorial

答案 3 :(得分:1)

您应该对所有案例都有休息时间,或者在所有情况下都有退货声明。否则,Java将执行匹配大小写之后的所有情况。

swicth(urSwitch){
   case 1:// logic
      break; // or return something
   case 2:// logic
      break; // or return something
   case 3:// logic
      break; // or return something
}