有 Java 编程相关的问题?

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

在一个Java文件中运行一个又一个程序

我制作了一些程序,第一个是最大用户输入的查找器;第二个是用户输入的乘积。我想把这些程序合并成一个,这样第一个程序运行完后,第二个程序就开始了。然而,我不知道如何做到这一点,因为我是编程新手

第一个的代码

导入java。util。扫描仪; 阶级军队{

private static Scanner in;

public static void main(String[] args){
    // declares an array of doubles
    double[] inputArray = new double[10];
    // allocates memory for 10 doubles
    System.out.println("Please enter ten numbers.");
    try {
        in = new Scanner(System.in);
        for (int j = 0; j < inputArray.length ; j++) {
            inputArray[(int) j] = in.nextDouble();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    double maxValue = inputArray[0];
    for (int i = 0; i < inputArray.length; i++) {
       if (inputArray[i] > maxValue) {
            maxValue = inputArray[i];
             // removed print from here
            } else {
             // removed print from here too
            }
      }
    System.out.println("The largest number is "+maxValue+"."); //print max from outside the loop;
            // optional: display only one answer.
}

}

秒代码

导入java。util。扫描仪; 阶级斗争{

private static Scanner in;

public static void main(String[] args){
    double[] inputArray = new double[10];
    System.out.println("Please enter ten numbers.");
    in = new Scanner(System.in);
    for (int j = 0; j < inputArray.length ; j++) {
        inputArray[(int) j] = in.nextDouble();
        }
    double product = inputArray[0];
    for (int i = 0; i < inputArray.length; i++) {
        product*=inputArray[i];
        }
    System.out.println("The product of these numbers is "+product+".");
}

}


共 (1) 个答案

  1. # 1 楼答案

    利用方法,创建两种不同的方法。将当前的两个主要方法代码移到这两个新方法中。然后从你的主页上,一个接一个地调用新方法

    public static void main() {
      findLargest();
      multiplyInputs();
    }