Reputation: 37
I have made programs with the first one being a finder of the largest user input; the second one multiplies the user input. I would like to combine these programs into one so that after the first one finishes running, the second one begins. However, I have no idea of how to do this as I am new to programming.
Code for first
import java.util.Scanner; class Army{
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.
}
}
Code for second
import java.util.Scanner; class Battles{
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+".");
}
}
Upvotes: 1
Views: 70
Reputation: 68715
Make use of methods, create two different methods. Move your current two main method code into these two new methods. And from you main ,call new methods one after the other.
public static void main() {
findLargest();
multiplyInputs();
}
Upvotes: 3