My teacher wants me to put a while loop into my program, where do I put it for the best outcome? public static void main(String[] args) { int min = 19; int num1=0, num2=0, num3=0; int tol1=0, tol2=0, tol3=0; int max = 50; int col[]=new int[49]; for(int i = 0; i < 49; i++){ int ran = (int)(Math.random() * (max - min + 1) + min); if( ran <= 20 && ran < 30){ num1 += 1; tol1 += ran; } if(ran <= 30 && ran < 40){ num2 += 1; tol2 += ran; } if(ran <= 40 && ran < 50){ num3 += 1; tol3 += ran; } col[i]= ran; } for(int i = 0; i < 49; i++ ){ if(i == 7 || i == 14 || i == 21 || i == 28 || i == 35 ||i == 42){ System.out.println("\n"); } System.out.printf(("%7d"),+ col[i]); } System.out.printf("\n"); System.out.printf("Found "+ num1 +" number(s) in the 20s "+ "totaling "+ tol1); System.out.printf("\nFound "+ num2 +" number(s) in the 30s "+ "totaling "+ tol2); System.out.printf("\nFound "+ num3 +" number(s) in the 40s "+ "totaling "+ tol3); } }
My teacher wants me to put a while loop into my program, where do I put it for the best outcome?
public static void main(String[] args) {
int min = 19;
int num1=0, num2=0, num3=0;
int tol1=0, tol2=0, tol3=0;
int max = 50;
int col[]=new int[49];
for(int i = 0; i < 49; i++){
int ran = (int)(Math.random() * (max - min + 1) + min);
if( ran <= 20 && ran < 30){
num1 += 1;
tol1 += ran;
}
if(ran <= 30 && ran < 40){
num2 += 1; tol2 += ran;
}
if(ran <= 40 && ran < 50){
num3 += 1; tol3 += ran;
}
col[i]= ran;
}
for(int i = 0; i < 49; i++ ){
if(i == 7 || i == 14 || i == 21 || i == 28 || i == 35 ||i == 42){
System.out.println("\n");
}
System.out.printf(("%7d"),+ col[i]);
}
System.out.printf("\n");
System.out.printf("Found "+ num1 +" number(s) in the 20s "+ "totaling "+ tol1);
System.out.printf("\nFound "+ num2 +" number(s) in the 30s "+ "totaling "+ tol2);
System.out.printf("\nFound "+ num3 +" number(s) in the 40s "+ "totaling "+ tol3);
}
}

Step by step
Solved in 2 steps









