0

我有一个任务,我必须设置一个由用户形成的数组(通过 cmd 行),用户确定长度和其中的数字。我似乎根本无法让它提示用户,但是当我运行它时不会出现错误。

这是我到目前为止所拥有的:

class hw8064 {
      static void createArray(int x1, int y1){

     int numElements;

     double[] a1;
     a1 = new double[x1];
     double[] a2;
     a2 = new double[y1];
     for (int i=0; i<=a1.length; i++){
         System.out.println("Please enter a double for array 1[<i>] and one for array 2[<i>]:");
     }


     }  
     public static void main(String [] args){
      int numElements;
      if (args.length > 0){
          try{
              numElements = Integer.parseInt(args[0]);
          } catch (NumberFormatException e){
              System.out.println("Argument must be an integer");
              System.exit(1);
              }
          }
      }
    }

另外我知道当我将项目文件配置为能够解析 JRE 中的 int 时,我需要为参数添加一些内容,但我不确定是什么。在此先感谢您的帮助!

编辑:我基本上已经设置好了,除了我现在找不到变量......

导入 java.util.Scanner;

类 hw8064 {

public static void createArray(double x1, double y1, int numElements) {
    Scanner in = new Scanner(System.in);

    double a1[] = {x1};
    a1 = new double[numElements];
    double a2[] = {y1};
    a2 = new double[numElements];
    for (int i = 0; i <= a1.length; i++) {
        System.out.println("Please enter a double for array 1[<" + i + ">]and one for array 2[<" + i + ">]:");

    }

}

public static void main(String[] args) {

    if (args.length > 0) {

        try {
            int numElements = Integer.parseInt(args[0]);

        } catch (NumberFormatException e) {
            System.out.println("Argument must be an integer");
            System.exit(1);
        }
        try {
            int numElements = Integer.parseInt(args[0]);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Please enter an integer for the argument");
            System.exit(1);
        }

    }
    createArray(***numElements***);
}

}

我调用该方法的最后一行给我带来了问题

4

1 回答 1

1

你需要像这样调用你的creatArray方法main

public static void main(String [] args){
   int numElements;
   if (args.length > 0){
      try{
          numElements = Integer.parseInt(args[0]);
      } catch (NumberFormatException e){
          System.out.println("Argument must be an integer");
          System.exit(1);
      }
   }

   createArray(numElements);
}

由于您只能从命令行获取一个参数,因此您应该只为该方法提供一个参数

 static void createArray(int numElements){
     double[] a1 = new double[x1];

     for (int i=0; i<=a1.length; i++){
         System.out.println(
                   "Please enter a double for array [<" + i + ">] " 
                    + "and one for array 2[<" + i + ">]:");
 }

如果你想从命令行获取两个参数,你应该在你的main

if (args.length != 2){
    System.out.println(Usage: numOfElements1 numOfElments2);
    System.exit(0);
} else {
    try {

     } catch ( ..... ) {

     }
}

编辑:完成

public static void main(String[] args){
    int num1;
    int num2;

    if (args.length != 2){
        System.out.println(Usage: numOfElements1 numOfElments2);
        System.exit(0);
    } else {
        try {
            num1 = Integer.parseInt(args[0]);
            num2 = Integer.parseInt(args[1]);

        } catch (Exception ex) {
            System.out.println("Arguments must be an integers");
            System.exit(1);  
        }
    }
    createArray(num1, num1);
}

static void createArray(int num`, int num2){
     double[] a1 = new double[num1];
     double[] a2 = new double[num2];

     Scanner scanner = new Scanner(System.in);

     for (int i=0; i<=a1.length; i++){
         System.out.println(
                   "Please enter a double for array1[<" + i + ">] ");
         double number1 = scanner.nextDouble();
         a1[i] = number1;

         System.out.println(
                   "Please enter a double for array2[<" + i + ">] "); 
         double number2 = scanner.nextDouble();
         a2[i] = number2;            
     }
 }
于 2013-11-12T01:18:47.563 回答