Java Program Instead of using 5 separate integers, store each valid integer from
ID: 3909701 • Letter: J
Question
Java Program
Instead of using 5 separate integers, store each valid integer from code below into an array
__________________________________________________________________________________________________
import java.util.Scanner;
public class CourseEvalLoopsAdded
{
private static Scanner sc;
public static void main(String[] args)
{
sc = new Scanner(System.in);
int error1 = 1;
int error2 = 1;
int error3 = 1;
int error4 = 1;
int error5 = 1;
int input1 = 1;
int input2 = 1;
int input3 = 1;
int input4 = 1;
int input5 = 1;
int eval1 = 1;
int eval2 = 1;
int eval3 = 1;
int eval4 = 1;
int eval5 = 1;
//1 as a rating is Very Good
while (error1 == 1)
{
System.out.print ("Please enter the total number of students who entered 1:");
input1 = sc.nextInt();
if (input1 >= 0 && input1 <= 30 )
{
eval1 = input1 * 5;
error1 = -1;
}
}
//2 as a rating is Good
while (error2 == 1)
{
System.out.println ("Please enter the total number of students who entered 2:");
input2 = sc.nextInt();
if (input2 >= 0 && input2 <= 30 )
{
eval2 = input2 * 4;
error2 = -1;
}
}
// 3 is Neutral
while (error3 == 1)
{
System.out.println("Please enter the total number of students who entered 3:");
input3 = sc.nextInt();
if (input3 >= 0 && input3 <= 30 )
{
eval3 = input3 * 3;
error3 = -1;
}
}
// 4 is Poor
while (error4 == 1)
{
System.out.println("Please enter the total number of students who entered 4:");
input4 = sc.nextInt();
if (input4 >= 0 && input4 <= 30 )
{
eval4 = input4 * 2;
error4 = -1;
}
}
// 5 is Very Poor
while (error5 == 1)
{
System.out.println("Please enter the total number of students who entered 5:");
input5 = sc.nextInt();
if (input5 >= 0 && input5 <= 30 )
{
eval5 = input5 * 1;
error5 = -1;
}
}
System.out.println("The total number of students who entered 1 is " + eval1);
System.out.println("The total number of students who entered 2 is " + eval2);
System.out.println("The total number of students who entered 3 is " + eval3);
System.out.println("The total number of students who entered 4 is " + eval4);
System.out.println("The total number of students who entered 5 is " + eval5);
float total = input1 +input2 +input3 + input4 + input5;
System.out.println("The final evaluation is " +(eval1 + eval2 + eval3 + eval4 + eval5)/total);
}
}
Explanation / Answer
Modified code with the help of array instead of using separate integer for storing integer is following:
import java.util.Scanner;
public class CourseEvalLoopsAdded
{
private static Scanner sc;
public static void main(String[] args)
{
sc = new Scanner(System.in);
int error[]={1,1,1,1,1};
int input[]={1,1,1,1,1};
int eval[]={1,1,1,1,1};
//1 as a rating is Very Good
while (error[0]==1)
{
System.out.print ("Please enter the total number of students who entered 1:");
input[0] = sc.nextInt();
if (input[0] >= 0 && input[0] <= 30 )
{
eval[0] = input[0]*5;
error[0]=-1;
}
}
//2 as a rating is Good
while (error[1]==1)
{
System.out.println ("Please enter the total number of students who entered 2:");
input[1]= sc.nextInt();
if (input[1]>=0 && input[1]<= 30)
{
eval[1]= input[1]*4;
error[1]=-1;
}
}
// 3 is Neutral
while (error[2]==1)
{
System.out.println("Please enter the total number of students who entered 3:");
input[2]= sc.nextInt();
if (input[2]>= 0 && input[2] <= 30 )
{
eval[2] = input[2] * 3;
error[2] = -1;
}
}
// 4 is Poor
while (error[3] == 1)
{
System.out.println("Please enter the total number of students who entered 4:");
input[3] = sc.nextInt();
if (input[3] >= 0 && input[3] <= 30 )
{
eval[3] = input[3] * 2;
error[3] = -1;
}
}
// 5 is Very Poor
while (error[4] == 1)
{
System.out.println("Please enter the total number of students who entered 5:");
input[4] = sc.nextInt();
if (input[4] >= 0 && input[4] <= 30 )
{
eval[4] = input[4] * 1;
error[4] = -1;
}
}
System.out.println("The total number of students who entered 1 is " + eval[0]);
System.out.println("The total number of students who entered 2 is " + eval[1]);
System.out.println("The total number of students who entered 3 is " + eval[2]);
System.out.println("The total number of students who entered 4 is " + eval[3]);
System.out.println("The total number of students who entered 5 is " + eval[4]);
float total = input[0] +input[1] +input[2] + input[3] + input[4];
System.out.println("The final evaluation is " +(eval[0]+eval[1]+eval[2]+eval[3]+eval[4])/total);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.