JOptionPane Input to int

前端 未结 5 1561
面向向阳花
面向向阳花 2021-01-02 14:31

I am trying to make a JOptionPane get an input and assign it to an int but I am getting some problems with the variable types.

I am trying something like this:

5条回答
  •  臣服心动
    2021-01-02 15:14

    // sample code for addition using JOptionPane
    
    import javax.swing.JOptionPane;
    
    public class Addition {
    
        public static void main(String[] args) {
    
            String firstNumber = JOptionPane.showInputDialog("Input ");
    
            String secondNumber = JOptionPane.showInputDialog("Input ");
    
            int num1 = Integer.parseInt(firstNumber);
            int num2 = Integer.parseInt(secondNumber);
            int sum = num1 + num2;
            JOptionPane.showMessageDialog(null, "Sum is" + sum, "Sum of two Integers", JOptionPane.PLAIN_MESSAGE);
        }
    }
    

提交回复
热议问题