1

/When i run the program . After the total credits is over 20 after adding credits in the if statement in switch , the loop/switch wont break out the loop . It continues to run , adding the credits . What needs to be fixed??/

import java.util.ArrayList;
import java.util.Scanner;
public class CoursesRegistration {

    public static void main(String[] args) {
        Scanner kb=new Scanner(System.in);
        int choose=0;
        int option;

        ArrayList<String> CourseOffer = new ArrayList<String>();

        CourseOffer.add(" CS1001-Basic Programming              3 Credit       RM300.00");
        CourseOffer.add(" CS1002-Python Programming             4 Credit       RM400.00");
        CourseOffer.add(" CS1004-Operating System               3 Credit       RM300.00");
        CourseOffer.add(" CS1005-Web Graphical Interface (GUI)  3 Credit       RM300.00");
        CourseOffer.add(" CS1006-Web Programming                4 Credit       RM400.00");
        CourseOffer.add(" CS1007-Agile Development              3 Credit       RM300.00");
        CourseOffer.add(" CS1008-Database Implementation        3 Credit       RM300.00");
        CourseOffer.add(" CS1009-System Security                3 Credit       RM300.00");
        CourseOffer.add(" CS1010-Software Testing               3 Credit       RM300.00");
        CourseOffer.add(" NA1001-Culture and Social             2 Credit       RM300.00");

        do{

            double cost=0;
            for(int i=0;i<CourseOffer.size();i++)
            {
                System.out.println((i+1)+"-"+CourseOffer.get(i) );
            }   
            System.out.println("");

            int total=0;
            while(choose != 99)
            {
                if(total >= 20)
                {
                    break;
                }

                System.out.print("Please choose your subject recomend for this semester: ");
                choose=kb.nextInt();

                    switch(choose){
                            case 1:
                            if(total+3>20)
                            {
                                break;
                            }
                            else
                            {
                                System.out.println("CS1001  Basic Programming");
                                total +=3;
                                cost+=300;
                            }
                            break;

                            case 2:
                            if(total+4>20)
                            {
                                break;
                            }
                            else
                            {
                                System.out.println("CS1002 Python Programming ");
                                total +=4;
                                cost+=400;
                            }
                            break;

                            case 3:
                            if(total+3>20)
                            {
                                break;
                            }
                            else
                            {
                                System.out.println("CS1004 Operating System");
                                total += 3;
                                cost+=300;
                            }
                            break;

                            case 4:
                            if(total+3>20)
                            {
                                break;
                            }
                            else
                            {
                                System.out.println("CS1005 Web Graphical Interface (GUI ");
                                total += 3;
                                cost+=300;
                            }
                            break;

                            case 5:
                            if(total+4>20)
                            {
                                break;
                            }
                            else
                            {
                                System.out.println("CS1006 Web Programming ");
                                total += 4;
                                cost+=400;
                            }
                            break;

                            case 6:
                            if(total+3>20)
                            {
                                break;
                            }
                            else
                            {
                                System.out.println("CS1007 Agile Development ");
                                total+= 3;
                                cost+=300;
                            }
                            break;

                            case 7:
                            if(total+3>20)
                            {
                                break;
                            }
                            else
                            {
                                System.out.println("CS1008 Database Implementation ");
                                total+= 3;
                                cost+=300;
                            }
                            break;

                            case 8:
                            if(total+3>20)
                            {
                                break;
                            }
                            else
                            {
                                System.out.println("CS1009 System Security ");
                                total+= 3;
                                cost+=300;
                            }
                            break;

                            case 9:
                            if(total+3>20)
                            {
                                break;
                            }
                            else 
                            {
                                System.out.println("CS1010 Software Testing ");
                                total+= 3;
                                cost+=300;
                            }
                            break;

                            case 10:
                            if(total+3>20)
                            {
                                break;
                            }
                            else
                            {
                                System.out.println("NA1001 Culture and Social ");
                                total+= 2;
                                cost+=200;
                            }
                            break;

                    }

                }

                System.out.println("");
                System.out.println("Total credit hour for this semester is "+total);
                System.out.println("");

                System.out.println("Registration slip of ABC's College Registration Course ");
                System.out.println("");

                for(int i=0;i<CourseOffer.size();i++)
                {
                    System.out.println(""+CourseOffer.get(i) );
                }
                System.out.print("Total student payment : RM");
                System.out.printf("%.2f",cost);

                System.out.println("\nPlease enter 1 to continue and 0 to exit: ");
                option=kb.nextInt();

       }while(option==1);           
    }
}
  • 2
    You're breaking out of the `switch` statement not the `loop`... – brso05 Dec 02 '15 at 16:23
  • This program is practically *begging* for a class with which to represent all the properties of a course, and a `List` or array of instances representing the course options and their UI index numbers. The whole ginormous switch statement could then be replaced by about ten lines of code, and changing the list of course offerings would be a breeze. – John Bollinger Dec 02 '15 at 16:32

3 Answers3

1

You're breaking out of the switch statement not the loop. Maybe create a boolean variable and set it to true if it should break from the loop then after the switch statement check the variable to see if it's true if it is do another break...

brso05
  • 13,142
  • 2
  • 21
  • 40
1

Change

while(choose != 99)
{

to

myLoop:
while(choose != 99)
{

& inside the switch block, you can break out of the loop using

break myLoop;
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
0

Why not do:

while(choose != 99 && total <= 20){

Add the condition within loop condition check...

Jaskaranbir Singh
  • 2,034
  • 3
  • 17
  • 33