首页 文章

在Java中作为命令行参数运行此程序时出错,我该如何解决这个问题?

提问于
浏览
0

当我尝试在Eclipse中将此程序作为命令行参数运行时,我收到错误 . 这就是错误所说的:

错误:在类中找不到主方法,请将main方法定义为:public static void main(String [] args)或JavaFX应用程序类必须扩展javafx.application.Application

当我将main方法更改为String时,我无法调用第二个方法,因为String main方法与int returnSum方法不兼容 .

我需要根据要求将returnSums方法设置为int类型方法,但我无法弄清楚如何在不出错的情况下执行此操作 . 它在要求中说我需要为方法使用可变数量的参数,但很难掌握这个想法 .

有人可以帮我这个吗?这是我的代码:

public static void main(int[] args) {

    // Printing the entered digits
    System.out.print("Passing");
    System.out.print(" [ ");

    for (int nums = 0; nums < args.length; nums++) {
        System.out.print(args[nums] + " ");

    }
    System.out.print("] ");
    System.out.println("\nSum is " + returnSum(args)); // Calling on the second method for the sum

}
// Taking the nums from the main method as arguments 
public static int returnSum(int...args) {
    int sum = 0;
    // Calculating the sum
    for (int nums = 0; nums < args.length; nums++) {
        sum = sum + nums;
    }
    return sum;

}

谢谢!

4 回答

  • 1

    试试以下代码:

    public class SumOfNumbers {
        public static void main(String[] args) {
    
            int[] intArgs = new int[args.length];
            for (int x = 0; x < args.length; x++) {
                if (args[x].matches("\\d+")) {
                    intArgs[x] = Integer.parseInt(args[x]);
                } else {
                    System.out.println(args[x] + " is not a Integer hence skiped in this program");
                }
            }
    
            // Printing the entered digits
            System.out.print("Passing");
            System.out.print(" [ ");
    
            for (int nums = 0; nums < intArgs.length; nums++) {
                System.out.print(intArgs[nums] + " ");
    
            }
            System.out.print("] ");
            System.out.println("\nSum is " + returnSum(intArgs)); // Calling on the second method for the sum
    
        }
    
        // Taking the nums from the main method as arguments
        public static int returnSum(int... args) {
            int sum = 0;
            // Calculating the sum
            for (int nums = 0; nums < args.length; nums++) {
                sum = sum + args[nums];
            }
            return sum;
        }
    }
    
  • 4

    @Sanjay:如果您尝试使用参数: 10, 20 , 30 ,您将得到以下输出:

    10, is not a Integer hence skiped in this program
    , is not a Integer hence skiped in this program
    Passing [ 0 20 0 30 ] 
    Sum is 50
    

    10, 不应该被忽略 , 应该被忽略 . 它也 intArgs 应该是 size 3 including 10 or size 2 excluding 10 it should not be of size 4.

  • 3

    将main方法的参数更改回 String[] ,因为这是必需的签名 .

    在main方法中,将每个参数解析为 int

    int[] intArgs = new int[args.length];
    for(int x=0; x<args.length; x++)
        intArgs[x] = Integer.parseInt(args[x]);
    

    请注意,如果您不传递参数或不传递整数的参数,则会中断 . 如果你想处理这个问题,可以先查看 args!=null 并在此代码周围找到 NumberFormatException .

  • 1

    您需要将主方法签名设置为 (String[] args) ,但是您可以在代码中循环 args 数组,使用 Integer.parseInt() 将每个元素转换为 int ,将它们存储在新数组(类型为 int[] )中,并将此新数组发送到 returnSum .

相关问题