Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 将此JOptionPane程序的字符串转换为字符_String_Char_Joptionpane - Fatal编程技术网

String 将此JOptionPane程序的字符串转换为字符

String 将此JOptionPane程序的字符串转换为字符,string,char,joptionpane,String,Char,Joptionpane,我正在尝试将我的操作变量转换为char,这样它就可以与if语句一起工作。我该怎么做呢?以下是我的代码: import java.util.Scanner; import javax.swing.JOptionPane; public class Java_Calculator { public static void main(String[] args) { String firstnumber, secondnumber, operation;

我正在尝试将我的操作变量转换为char,这样它就可以与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语句是正确的。正在寻找将该操作变量转换为字符的最佳方法。

假设输入正确,您的运算符是第一个字符:
操作。字符(0)
请添加一些关于如何解决此问题的解释
 String a=JOptionPane.showInputDialog ("Enter the Operator: ");
    char ch=a.charAt(0); //charAt(int index of string)
    if(ch=='+'){
    .......
    }
    .....
    .....
    .....