如何使这个If语句适用于所有内容?

时间:2016-05-12 20:06:35

标签: java calculator

对于我的程序,我正在尝试制作一个简单的计算器,它可以进行两个数字的加法,减法,乘法和除法,但是我用if语句打了一个路障。如何使用我的选择字符串使这个Statement工作,还可以使用我的加法,减法,乘法和除法字符串?我正在使用Java

import javax.swing.JOptionPane;

public class Calculator
{

public static void main (String args [])
{
    String MainMenu = " Calculator Options \n\n1. Addition\n2" +
    " Subtraction\n3. Multiplication\n4. Division\n5. Exit";

    menu (MainMenu);
}

 public static String menu (String info)
{
    String choice = JOptionPane.showInputDialog (info);
    return choice;

}


{
if (choice.equals ("1"))
  Add();
    else
       if (choice.equals ("2"))
        Sub();
          else
            if (choice.equals ("3"))
             Mult();
               else 
                if (choice.equals ("4"))
                 Div();
}


public static int Addition (String Add)
{
    String add1 = JOptionPane.showInputDialog ("Number 1 is?");
    int numadd1 = Integer.parseInt (add1);

    String add2 = JOptionPane.showInputDialog ("Number 2 is?");
    int numadd2 = Integer.parseInt (add2);

    int addEquals = numadd1 + numadd2;

    JOptionPane.showMessageDialog (null, "The Answer Is: " + addEquals);

    return addEquals;
}

public static int Subtraction (String Sub)
{
    String sub1 = JOptionPane.showInputDialog ("Number 1 is?");
    int numsub1 = Integer.parseInt (sub1);

    String sub2 = JOptionPane.showInputDialog ("Number 2 is?");
    int numsub2 = Integer.parseInt (sub2);

    int subEquals = numsub1 - numsub2;

    JOptionPane.showMessageDialog (null, "The Answer Is: " + subEquals);

    return subEquals;
}

public static int Multiplication (String Mult)
{
    String mult1 = JOptionPane.showInputDialog ("Number 1 is?");
    int nummult1 = Integer.parseInt (mult1);

    String mult2 = JOptionPane.showInputDialog ("Number 2 is?");
    int nummult2 = Integer.parseInt (mult2);

    int multEquals = nummult1 * nummult2;

    JOptionPane.showMessageDialog (null, "The Answer Is: " + multEquals);

    return multEquals;
}

public static int Divistion (String Div)
{
    String div1 = JOptionPane.showInputDialog ("Number 1 is?");
    int numdiv1 = Integer.parseInt (div1);

    String div2 = JOptionPane.showInputDialog ("Number 2 is?");
    int numdiv2 = Integer.parseInt (div2);

    int divEquals = numdiv1 / numdiv2;

    JOptionPane.showMessageDialog (null, "The Answer Is: " + divEquals);

    return divEquals;
}
}

4 个答案:

答案 0 :(得分:2)

您的代码存在很多问题,因此这是一个修订版本:

public static void main(String[] args) {
    String mainMenu = " Calculator Options \n\n1. Addition\n2" +
            " Subtraction\n3. Multiplication\n4. Division\n5. Exit";

    String choice = JOptionPane.showInputDialog(mainMenu);

    int num1 = Integer.parseInt(JOptionPane.showInputDialog("Number 1 is?"));
    int num2 = Integer.parseInt(JOptionPane.showInputDialog("Number 2 is?"));

    int result;
    if (choice.equals("1")) {
        result = num1 + num2;
    } else if (choice.equals("2")) {
        result = num1 - num2;
    } else if (choice.equals("3")) {
        result = num1 * num2;
    } else if (choice.equals("4")) {
        result = num1 / num2;
    } else {
        return;
    }

    JOptionPane.showMessageDialog(null, "The Answer Is: " + result);
}

对于switch,这会更清晰,但我已根据您的要求使用了if

答案 1 :(得分:1)

好的,我做了什么来解决它:

  1. 将if块移动到main方法中:因为它不能编译
  2. 更改方法调用以匹配方法名称
  3. 删除方法参数 - 当您从JOptionPane获取值时,不需要它们
  4. 将main更改为return和int。
  5. 你去那里的好应用〜干杯

    import javax.swing.JOptionPane;
    
    public class Calculator {
    
        public static void main (String args []) {
            String MainMenu = " Calculator Options \n\n1. Addition\n2" +
                    " Subtraction\n3. Multiplication\n4. Division\n5. Exit";
            menu (MainMenu);
        }
    
        public static int menu (String info) {
            String choice = JOptionPane.showInputDialog (info);
    
            if (choice.equals ("1"))
                return Addition();
            else if (choice.equals ("2"))
                return Subtraction();
            else if (choice.equals ("3"))
                return  Multiplication();
            else if (choice.equals ("4"))
                return  Division();
            else
                return 0;
        }
    
        public static int Addition () {
            String add1 = JOptionPane.showInputDialog ("Number 1 is?");
            int numadd1 = Integer.parseInt (add1);
    
            String add2 = JOptionPane.showInputDialog ("Number 2 is?");
            int numadd2 = Integer.parseInt (add2);
    
            int addEquals = numadd1 + numadd2;
    
            JOptionPane.showMessageDialog (null, "The Answer Is: " + addEquals);
    
            return addEquals;
        }
    
        public static int Subtraction ()
        {
            String sub1 = JOptionPane.showInputDialog ("Number 1 is?");
            int numsub1 = Integer.parseInt (sub1);
    
            String sub2 = JOptionPane.showInputDialog ("Number 2 is?");
            int numsub2 = Integer.parseInt (sub2);
    
            int subEquals = numsub1 - numsub2;
    
            JOptionPane.showMessageDialog (null, "The Answer Is: " + subEquals);
    
            return subEquals;
        }
    
        public static int Multiplication ()
        {
            String mult1 = JOptionPane.showInputDialog ("Number 1 is?");
            int nummult1 = Integer.parseInt (mult1);
    
            String mult2 = JOptionPane.showInputDialog ("Number 2 is?");
            int nummult2 = Integer.parseInt (mult2);
    
            int multEquals = nummult1 * nummult2;
    
            JOptionPane.showMessageDialog (null, "The Answer Is: " + multEquals);
    
            return multEquals;
        }
    
        public static int Division ()
        {
            String div1 = JOptionPane.showInputDialog ("Number 1 is?");
            int numdiv1 = Integer.parseInt (div1);
    
            String div2 = JOptionPane.showInputDialog ("Number 2 is?");
            int numdiv2 = Integer.parseInt (div2);
    
            int divEquals = numdiv1 / numdiv2;
    
            JOptionPane.showMessageDialog (null, "The Answer Is: " + divEquals);
    
            return divEquals;
        }
    }
    

答案 2 :(得分:0)

首先,考虑使用switch语句而不是这些嵌套ifs。如果您选择了a,b,c,d,那么默认情况下可以假设一个数字并尝试解析它。

答案 3 :(得分:0)

自从我编写Java以来​​已经有一段时间了,但我认为你的代码存在一些问题。首先,if语句位于匿名块中。其次,您正在调用方法菜单,该菜单返回一个值,但您不能将该值设置为任何值。 D,你错了拼写" Division。"最后,这不会导致代码失败,但是您使用大写字母表示方法的第一个字母。我认为编写Java应用程序的正常方法是大写类,但方法通常是首字母大写,其余的是CamelCase(包通常也是undercase / CamelCase)。如果我错了,请有人纠正我。

编辑: PS。我忘记了,你显然是以错误的方式调用这些方法(你不能用Add()调用" Addition"方法)和菜单方法一样,你没有设置方法返回的任何值。