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