Question: rewrite/rephrase this java code WITH THE SAME OUTPUT import java.util.Random; import java.util.Collections; import java.util.ArrayList; public class Game { public static void main(String args[]) { ArrayList
rewrite/rephrase this java code WITH THE SAME OUTPUT
import java.util.Random;
import java.util.Collections;
import java.util.ArrayList;
public class Game {
public static void main(String args[]) {
ArrayList list1 = new ArrayList();
Random generator = new Random();
int sum = 45;
int sum2 = 0;
while (sum > 0) {
int x = generator.nextInt(1, 45);
if (x + sum2
list1.add(x);
sum -= x;
sum2 += x;
}
}
Collections.sort(list1);
System.out.println(list1);
ArrayList wantedList=new ArrayList();
for(int c = 1; c
The game starts with a lat of random poinive integer mambers such that their sam 45 . In each roust, you siberact ene from each mamber in the list and nomore it if it bewomss zero, add the sum of the saberacted numbers to the cad of the list. The pame is over when the list has 1, 2, 3, 4, 5, 6, 7, 8, and 9, in some order, In your proprame priot the lat after each round: Here is a sample run of the program wantedList.add(c);
}
boolean valid = true;
while (valid) {
int size = list1.size();
int substracted = 0;
for (int i = 0; i
int num = list1.get(i);
if ((num - 1) == 0) {
list1.remove(i);
size-=1;
substracted += 1;
i--;
} else {
list1.set(i, (num -1));
substracted += 1;
}
}
list1.add(substracted);
System.out.println(list1);
ArrayList copyOfList = new ArrayList(list1);
Collections.sort(copyOfList);
if (copyOfList.equals(wantedList)) {
valid = false;
Collections.sort(list1);
System.out.println(list1);
}
else {
valid = true;
}
}
}
}
sample run and question is shown below:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
