2014-10-02 32 views
1

我已经制作了第一个程序,它是最大用户输入的查找程序;第二个乘以用户输入。我想将这些程序合并为一个,以便在第一个程序运行结束后,第二个程序开始。然而,我不知道如何做到这一点,因为我是编程新手。在另一个Java文件中运行程序

第一代码

import java.util.Scanner; 类陆军{

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.Scanner的; 阶级斗争{

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+"."); 
} 

}

回答

3

使用方法,创建两个不同的方法。将当前的两个主要方法代码移到这两个新方法中。并且从你主要来说,一个接一个地调用新的方法。

public static void main() { 
    findLargest(); 
    multiplyInputs(); 
} 
相关问题