java - 嵌入式 Switch Case 语句

标签 java switch-statement case

因此,我正在尝试测试我的程序,看看它是否会将我添加到其中的支出事件相加。

所以我使用选项 2,添加两次费用发生,以测试总计是否总计。然后我转到选项 5(费用选项),然后转到案例 2(列出总费用),此时它会重置并返回到原始 Joption 屏幕

package homebudget;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JOptionPane;

/**
*
* @author Derek
*/
public class HomeBudget 
{
//Features to add:
//Reminder 2 days before an auto deduction




public static void main(String[] args) throws Exception
{
    // TODO code application logic here

List<Bills> deductions = new ArrayList();
String billName, deductDate, resp;
double amount, totalAmount;
int cmd, year, month, date;
totalAmount = 0;

List<Spending> expenses = new ArrayList();
String type;

List<Income> deposits = new ArrayList();
String incomeType;

String fname = JOptionPane.showInputDialog("Enter the name of the budget file, none if no file");
    if (fname.compareTo("none") !=0)
    {
        FileInputStream ist = new FileInputStream(fname);
        ObjectInputStream ifile = new ObjectInputStream(ist);
        deductions = (ArrayList<Bills>) ifile.readObject();

    }
    boolean done = false;
    while(!done)
    {
        resp = JOptionPane.showInputDialog("Enter a command from: \n" 
                + "\t1:Add a new deduction\n"  //think its done
                + "\t2:Add a new expense\n"  
                + "\t3:Add a deposit\n"  
                + "\t4:Deduction options\n"  
                + "\t5:Expense Options\n"
                + "\t6:Total balances in bank\n"
                + "\t7:quit");
        cmd = Integer.parseInt(resp);
        switch(cmd)
        {
            case 1:

            billName = JOptionPane.showInputDialog("Enter the name of the bill:");
            deductDate = JOptionPane.showInputDialog("Enter the deduct date:");
            resp = JOptionPane.showInputDialog("Enter the deduct amount");
            amount = Double.parseDouble(resp);

            Bills d = new Bills(billName, deductDate, amount);
            deductions.add(d);
            break;

            case 2:
            //Give the option to add new spending occurence.
            //Give option to choose from array of spending types.

            type = JOptionPane.showInputDialog("Enter the type of the expense:"); 

            resp = JOptionPane.showInputDialog("Enter the amount of the expense:");   
            amount = Double.parseDouble(resp);
            resp = JOptionPane.showInputDialog("Enter the year of the expense:");
            year = Integer.parseInt(resp);
            resp = JOptionPane.showInputDialog("Enter the month of the expense:");
            month =  Integer.parseInt(resp);
            resp = JOptionPane.showInputDialog("Enter the date of the expense:");
            date =  Integer.parseInt(resp);
            Spending s = new Spending(amount, type, year, month, date);
            expenses.add(s);

            break;

            //Income reporting    
            case 3:

            resp = JOptionPane.showInputDialog("Enter the type of deposit:"); 
            incomeType = resp;
            resp = JOptionPane.showInputDialog("Enter the amount of the deposit:");   
            amount = Double.parseDouble(resp);
            resp = JOptionPane.showInputDialog("Enter the year of the deposit:");
            year = Integer.parseInt(resp);
            resp = JOptionPane.showInputDialog("Enter the month of the deposit:");
            month =  Integer.parseInt(resp);
            resp = JOptionPane.showInputDialog("Enter the date of the deposit:");
            date =  Integer.parseInt(resp);        
            Income i = new Income(amount, incomeType, year, month, date);

            break;

            case 4:

            //deduction options

            break;

            case 5:

            //expense options
            resp = JOptionPane.showInputDialog("Enter a command from: \n" 
                + "\t1: List all expenses in a month\n"  //done
                + "\t2: List the total spent on an expense for the month\n"  
                + "\t3: List all expenses in a year\n"  
                + "\t4: List all expenses by type in a year\n"
                + "\t5:  \n"
                + "\t6:  \n"
                + "\t7:quit");
                switch(cmd){

                    case 1:


                        resp = JOptionPane.showInputDialog("What year of expenses would you like to see?:"); 
                        year = Integer.parseInt(resp);
                        resp = JOptionPane.showInputDialog("What month of expenses would you like to see?:"); 
                        month =  Integer.parseInt(resp);

                        boolean found = false;

                        Iterator<Spending> spendIter = expenses.iterator();

                        while(!found && spendIter.hasNext())
                        {
                         s = spendIter.next();
                         if(s.getYear() == year && s.getMonth() == month)
                        {
                       //How to print out an element of this Array List?
                       System.out.println("Here is the expense list for the month number " + month + ": \n") ;
                       System.out.println("Expense Type: " + s.getType() + "Amount" + s.getAmount() + "Date" + s.getDay());


                                found = true;

                        }
                         else  
                         {
                            System.out.println("There are no expenses entered for this month.");
                            }

                        }  
                    break;

                    case 2:
                        //How to list total spending on an expense per month.
                        type = JOptionPane.showInputDialog("Enter the type of the expense:"); 
                        resp = JOptionPane.showInputDialog("Enter the month of the expense:");
                        month = Integer.parseInt(resp);
                        found = false;

                        spendIter = expenses.iterator();
                        double totalSpent = 0;
                        while(spendIter.hasNext())
                        {
                         s = spendIter.next();
                         if(s.getType() == type && s.getMonth() == month)
                        {

                         totalSpent = totalSpent + s.getAmount();
                        }
                        JOptionPane.showMessageDialog(null, "Total money spent on " + type + ": $" 
                                + totalSpent);
                        }
                break;





    }


}

}
}

最佳答案

在内部开关中,您仍在尝试打开cmd,这就是外部开关正在打开的内容。因此,在第二次切换之前,您应该执行以下操作:

int cmd2 = Integer.parseInt(resp);

然后:

switch (cmd2) {

我认为您在某个地方也错过了break,但这不是主要问题。

关于java - 嵌入式 Switch Case 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25209028/

相关文章:

java - 在eclipse中查找实现抽象类的所有具体类

java - 更新单元格表列中的选定项目

java - 是否可以将算术运算符传递给java中的方法?

php - switch 语句中的奇怪行为

SQL 返回列中的所有大写值

mysql case 语句,有些有效,有些无效

java - 非英文字符显示成这样?

c++ - 整数的模数

typescript - noFallthroughCasesInSwitch - 明确允许失败

mysql - SELECT MAX CASE 问题