In: Computer Science
Hello, I need to convert this java array into an array list as I am having trouble please.
import java.util.Random; import java.util.Scanner; public class TestCode { public static void main(String[] args) { String choice = "Yes"; Random random = new Random(); Scanner scanner = new Scanner(System.in); int[] data = new int[1000]; int count = 0; while (!choice.equals("No")) { int randomInt = 2 * (random.nextInt(5) + 1); System.out.println(randomInt); data[count++] = randomInt; System.out.print("Want another random number (Yes / No)? "); choice = scanner.next(); } int min = data[0], max = data[0]; for (int i = 0; i < count; i++) { if (data[i] > max) max = data[i]; if (data[i] < min) min = data[i]; } System.out.println("Percentage of Yes values is " + (((count-1)*100.0)/count)); System.out.println("Maximum value is " + max); System.out.println("Minimum value is " + min); } }
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
import java.util.*;
public class TestCode {
public static void main(String[] args) {
String choice = "Yes";
Random random = new Random();
Scanner scanner = new Scanner(System.in);
//declare an arraylist here
ArrayList<Integer> data = new
ArrayList<Integer>();
int count = 0;
while (!choice.equals("No")) {
int randomInt = 2 * (random.nextInt(5) + 1);
System.out.println(randomInt);
//change here add() method
data.add(randomInt);
count++;
System.out.print("Want another random number (Yes / No)? ");
choice = scanner.next();
}
//NOT NEEDED
// int min = data[0], max = data[0];
// for (int i = 0; i < count; i++) {
// if (data[i] > max) max = data[i];
// if (data[i] < min) min = data[i];
// }
//USE THIS
int min=Collections.min(data);
int max=Collections.max(data);
System.out.println("Percentage of Yes values is " +
(((count-1)*100.0)/count));
System.out.println("Maximum value is " + max);
System.out.println("Minimum value is " + min);
}
}
==========================
SCREENSHOT:
OUTPUT: