有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java程序中的数组ArrayIndexOutOfBoundsException,用于检查double是否为负数

我想写一个简单的程序(使用double类型)来检查数字是否为负数

我得到以下错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at NegativeZahl.main(NegativeZahl.java:7)

    import javax.swing.JOptionPane;

    public class NegativeZahl {

     public static void main(String[] args) {
     String eingabe;
     double zahl = Double.parseDouble(args[0]);
     eingabe = JOptionPane.showInputDialog("Geben Sie eine Zahl ein!");
     if ( zahl >= 1) {
         JOptionPane.showMessageDialog(null,zahl + "Die Zahl ist    positiv" );
     } else {
         JOptionPane.showMessageDialog(null, zahl + "Die Zahl ist negativ");
     }

    }

   }

共 (2) 个答案

  1. # 1 楼答案

    您的程序混合了两种获取数字的方法:

    从命令行args[0]填充值zahl,这意味着您必须用java NegativeZahl 123.4之类的东西运行程序,123.4是您想要测试的数字

    但是,您也会打开一个对话框,要求用户输入一个数字,然后对他的输入eingabe不做任何操作

    错误看起来像是您没有在命令行上提供号码

  2. # 2 楼答案

    我相信你还没有用java program_name这样的参数运行这个程序