计算器的简单划分不起作用

时间:2014-01-02 19:56:50

标签: java swing joptionpane division shopping

我正在尝试制作一个可以计算多人之间的税,小费和分数的计算器,但最后一个功能不起作用,我无法弄清楚原因。我知道它可能是一个简单的修复,但我是Java的新手,所以我找不到它。注意:这是我的第一个实际Java程序,所以请理解我可能明显的错误。

import java.util.Scanner;
import javax.swing.JOptionPane;

public class TaxFinder { 

     static Scanner scan = new Scanner(System.in);
    public static double priceD, taxD, totalTax, total, priceTipped, pricePP;
    public static int peopleI;
    public static String price, tax, people, ifTip, tip, ifSplit;

    public static void main(String args[]){

        ifTip = JOptionPane.showInputDialog("Was your purchase in a restaurant?");

         if (ifTip.equalsIgnoreCase("yes")){ 
             price = JOptionPane.showInputDialog("What was the cost of your bill ($)?");
             priceD = Double.parseDouble(price);

             tax = JOptionPane.showInputDialog("What is your tax rate (decimal)?"); 
             taxD = Double.parseDouble(tax);

             tip = JOptionPane.showInputDialog("Do you want to tip 5, 10, 15, or 20% ?"); 

             if (tip.equals("5")){ 
                 priceTipped = priceD * .05;
             }

             else if (tip.equals("10")){ 
                 priceTipped = priceD * .1;
                 //System.out.println("Your price tipped is " + priceTipped);
             }

             else if (tip.equals("15")){ 
                 priceTipped = priceD * .15;
             }

             else if (tip.equals("20")){ 
                 priceTipped = priceD * .2;
             }

             totalTax = priceD * taxD;
             total = priceD + totalTax + priceTipped;

         } 

         else if (ifTip.equalsIgnoreCase("no")){ 
             price = JOptionPane.showInputDialog("How much did your purchase cost ($)?");
             priceD = Double.parseDouble(price);
             tax = JOptionPane.showInputDialog("What is your tax rate (decimal)?");
             taxD = Double.parseDouble(tax);

         totalTax = priceD * taxD;
        total = priceD + totalTax;
         }

        JOptionPane.showMessageDialog(null, "Your total is: $" + total, "Shopping Calculator", JOptionPane.PLAIN_MESSAGE);

        ifSplit = JOptionPane.showInputDialog("Are you splitting the cost with other people?");

        if (ifSplit.equalsIgnoreCase("yes")){ 
            people = JOptionPane.showInputDialog("How many people including you?");
            peopleI = Integer.parseInt(people);

            pricePP = total / peopleI; 
            JOptionPane.showMessageDialog(null, "The total is $" + total + " per person", "Shopping Calculator", JOptionPane.PLAIN_MESSAGE);

            JOptionPane.showMessageDialog(null, "Have a nice day!", "Shopping Calculator", JOptionPane.PLAIN_MESSAGE);
        } 

        else if (ifSplit.equalsIgnoreCase("no")){ 
            JOptionPane.showMessageDialog(null, "Have a nice day!", "Shopping Calculator", JOptionPane.PLAIN_MESSAGE);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在这里使用pricePP代替total变量:

JOptionPane.showMessageDialog(null, "The total is $" + total
                + " per person", "Shopping Calculator",
                JOptionPane.PLAIN_MESSAGE);

因为你输出了另一个没有分开的变量。