0

我正在尝试将我的操作变量转换为一个字符,以便它可以与我的 if 语句一起使用。我该怎么做呢?以下是我的代码:

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

public class Java_Calculator
{
    public static void main(String[] args)
    {
        String firstnumber, secondnumber, operation;

        double num1;
        double num2;
        double sum;
        double sub;
        double div;
        double mul;
        double mod;

        firstnumber = 
            JOptionPane.showInputDialog ("Enter the first Operand: ");

        secondnumber =
            JOptionPane.showInputDialog ("Enter the second Operand: ");

        operation =
            JOptionPane.showInputDialog ("Enter the Operator: ");

         num1 = Double.parseDouble (firstnumber);
         num2 = Double.parseDouble (secondnumber);

         sum= num1 + num2;
         sub= num1 - num2;
         div= num1 / num2;
         mul= num1 * num2;
         mod= num1 % num2;

         if (operation == '+'){
               JOptionPane.showMessageDialog (null, sum);}

         if (operation == '-'){
               JOptionPane.showMessageDialog (null, sub);
         }

         if (operation == '/'){
               JOptionPane.showMessageDialog (null, div);
         }

         if (operation == '*'){
               JOptionPane.showMessageDialog (null, mul);
         }

         if (operation == '%'){
               JOptionPane.showMessageDialog (null, mod);
         }
    }
} 

我将假设我的 if 语句现在是正确的。只是寻找将该操作变量转换为字符的最佳方法。

4

1 回答 1

0
 String a=JOptionPane.showInputDialog ("Enter the Operator: ");
    char ch=a.charAt(0); //charAt(int index of string)
    if(ch=='+'){
    .......
    }
    .....
    .....
    .....
于 2015-01-26T15:19:08.743 回答