Reputation: 13
I'm currently working on the code that will prompt student level type in code (switch case), and to determine the amount of cash for each type of student will receive. Then I want to sum all the cash received by each student level. I am fairly a beginner and have only learn java for a few weeks, any suggestion is very appreciated. Here is what I have worked on, there are also notes.
System.out.print("Level code :");
levelCode = input.next().charAt(0);
do {
switch (levelCode) {
case 'F':
System.out.print("Total Foundation student = ");
studentFoundation = input.nextInt();
foundationCash = studentFoundation * 50;
break;
case 'D':
System.out.print("Total Diploma student = ");
studentDiploma = input.nextInt();
diplomaCash = studentDiploma * 50;
break;
case 'B':
System.out.print("Total Bachelor student = ");
studentBachelor = input.nextInt();
bachelorCash = studentBachelor * 70;
break;
case 'M':
System.out.print("Total Master student = ");
studentMaster = input.nextInt();
masterCash = studentMaster * 90;
break;
case 'P':
System.out.println("Total PhD student = ");
studentPhd = input.nextInt();
phdCash = studentPhd * 90;
break;
default:
System.out.println("Invalid code");
break;
}
} while (levelCode < 5); //i dont know what to put here in order to loop the switch case
totalCash = foundationCash + diplomaCash + bachelorCash + masterCash + phdCash; //here i want to calculate all the input entered
System.out.println("Total cash amount = " + totalCash);
Upvotes: 1
Views: 2194
Reputation: 13
So I've tried using the for loop method, thanks to Andreas for giving an example :), and it worked!
Right now I dont know how to prompt user to input code again to continue the loop, but I have typed in a few lines to prompt the code and it works as intended, but Im sure this is not the most efficient way of doing it?
And Im planning to break the loop once user entered the last input/entered an invalid code
Here's the new modified code
LOOP: for (;;){
switch(levelCode){
case 'F' : System.out.print("Total Foundation student = ");
studentFoundation = input.nextInt();
foundationCash = studentFoundation * 50;
System.out.print("Level code :");//here im trying to prompt the user to enter another code to continue the loop
levelCode = input.next().charAt(0);
break;
case 'D' : System.out.print("Total Diploma student = ");
studentDiploma = input.nextInt();
diplomaCash = studentDiploma * 50;
System.out.print("Level code :");
levelCode = input.next().charAt(0);
break;
case 'B' : System.out.print("Total Bachelor student = ");
studentBachelor = input.nextInt();
bachelorCash = studentBachelor * 70;
System.out.print("Level code :");
levelCode = input.next().charAt(0);
break;
case 'M' : System.out.print("Total Master student = ");
studentMaster = input.nextInt();
masterCash = studentMaster * 90;
System.out.print("Level code :");
levelCode = input.next().charAt(0);
break;
case 'P' : System.out.println("Total PhD student = ");
studentPhd = input.nextInt();
phdCash = studentPhd * 90;
break LOOP;
default : System.out.println("Invalid code :");
break LOOP;
}
}
Upvotes: 0
Reputation: 159096
Assuming you only want the loop to go back if the default
case is executed, you can do it in different ways.
Using a label
LOOP: for (;;) { // forever loop
System.out.print("Level code :");
levelCode = input.next().charAt(0);
switch (levelCode) {
case 'F':
// code here
break LOOP; // <=== break out of the loop, not the switch statement
case 'D':
// code here
break LOOP; // <=== break out of the loop, not the switch statement
...
default:
// code here
}
}
Using a boolean
boolean error;
do {
System.out.print("Level code :");
levelCode = input.next().charAt(0);
error = false;
switch (levelCode) {
case 'F':
// code here
break;
case 'D':
// code here
break;
...
default:
error = true;
// code here
}
} while (error);
Using a special value, e.g. null
String levelCode;
do {
System.out.print("Level code :");
levelCode = input.next();
switch (levelCode) {
case "F":
// code here
break;
case "D":
// code here
break;
...
default:
// code here
levelCode = null;
}
} while (levelCode == null);
Upvotes: 1
Reputation: 36
I think a simple solution would be to first prompt the user to type how many students there are and then put your switch statement in a for loop.
Upvotes: 0