【问题标题】:JOptionPane error on EclipseEclipse 上的 JOptionPane 错误
【发布时间】:2013-09-20 00:44:21
【问题描述】:

我对 Java 非常陌生(即 AP 计算机科学 A 第 2 周)。我们必须使用 JOptionPane 来制作用于数学运算的消息框。我的代码在学校很好,但我不得不重新格式化它,因为我把它保存在 Facebook 上(没有其他选择)。现在,我在所有 JOptionPane 行上都收到错误消息,上面写着“此行有多个标记”和“令牌上的语法错误”。 我该如何解决。这是代码(请只修复我的要求,别无其他,我知道代码可能很奇怪)

import javax.swing.JOptionPane;

public class OptioPane {

public static void main(String[] args) {    
}

    String a = JOptionPane.showInputDialog("Enter an integer");
    String b = JOptionPane.showInputDialog("Input another");
    int x = Integer.parseInt(a);
    int y = Integer.parseInt(b);
    JOptionPane.showMessageDiaglog(null, "The numbers added together is " +(x+y));

    String c = JOptionPane.showInputDialog("Enter an integer");
    String d = JOptionPane.showInputDialog("Input another");
    int f = Integer.parseInt(c);
    int g = Integer.parseInt(d);
    JOptionPane.showMessageDialog(null, "The second number subtracted from the first number is " +(f-g));

    String s = JOptionPane.showInputDialog("Enter an integer");
    String r = JOptionPane.showInputDialog("Input another");
    int w = Integer.parseInt(a);
    int q = Integer.parseInt(b);
    JOptionPane.showMessageDialog(null, "The numbers multiplied together is " +(w*q));

    String k = JOptionPane.showInputDialog("Enter an integer");
    String j = JOptionPane.showInputDialog("Input another");
    int n = Integer.parseInt(a);
    int m = Integer.parseInt(b);
    JOptionPane.showMessageDialog(null, "The first number divided by the second number is " +(n/m));

    String fir = JOptionPane.showInputDialog("Enter an integer");
    String tir = JOptionPane.showInputDialog("Input another");
    int ah = Integer.parseInt(a);
    int bh = Integer.parseInt(b);
    JOptionPane.showMessageDialog(null, "The first number modulated by the second number is " +(ah*bh));
    }

【问题讨论】:

  • 请注意您在每种情况下使用的变量。你有几行像int w = Integer.parseInt(a); 这样a 应该真的是s 或其他一些变量。

标签: java eclipse swing computer-science joptionpane


【解决方案1】:

基本上,您的代码主体在任何可执行上下文之外(不是它所属的位置)......

public class OptioPane {

    public static void main(String[] args) {    
    }

    /* All your stuff - out of bounds and behaving badly */

}

相反,您需要将此代码放在可执行上下文中,例如main 方法...

public class OptioPane {

    public static void main(String[] args) {    
        /* All your stuff - playing nicely */
    }


}

哦,添加这一行...

JOptionPane.showMessageDiaglog(null, "The numbers added together is " +(x+y));

错了,看Dialog中多余的g,应该是……

JOptionPane.showMessageDialog(null, "The numbers added together is " +(x+y));

【讨论】:

    【解决方案2】:

    您似乎在重新格式化代码时做错了,因为整个代码块应该在 main 方法的大括号内

    【讨论】:

    • 这修复了它!唯一的问题是我的乘法结果是错误的。例如 5 乘以 5 等于 30...
    【解决方案3】:

    我看到了你关于乘法的评论,如果你仍然有问题,那是因为这个

      String s = JOptionPane.showInputDialog("Enter an integer");
        String r = JOptionPane.showInputDialog("Input another");
        int w = Integer.parseInt(a);
        int q = Integer.parseInt(b);
        JOptionPane.showMessageDialog(null, "The numbers multiplied together is " +(w*q));
    

    当你将 5 输入到 's' 和 5 输入到 'r' 并得到 30 时,你可能在 'a' 和 'b' 中分别有 5 和 6。

    【讨论】:

      【解决方案4】:

      试试这个:

      import javax.swing.JOptionPane;
      public class Dialogue1 {
      
          public static void main(String[]arg)
          {
              String number1,number2;
              number1=JOptionPane.showInputDialog("enter first number");
              number2=JOptionPane.showInputDialog("enter second number");
              int num1=Integer.parseInt(number1);
              int num2=Integer.parseInt(number2);
              int sum=num1+num2;
              String message=String.format("the sum is %d",sum);
              JOptionPane.showMessageDialog(null,sum);
          }
      }
      

      将结果存储在另一个变量中,然后显示它。它肯定可以正常工作,没有任何错误。

      【讨论】:

        【解决方案5】:

        如果您遇到任何问题,我想在 Eclipse 的错误下方会给出一些建议。我也遇到了同样的错误,它建议我导入 java swing 包然后它得到了修复。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-12
          相关资源
          最近更新 更多