简体   繁体   中英

While loop is not stopping/ break command doesn't work JAVA

I'm trying to make a Queue where you can have different commands for different functions and I used a while loop in order for it to be reusable. But the break; command in the switch case is not working.

Here is the code...

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;

public class Assign3Besin {
    public static void main(String[] args) {
        Queue<Person> persons=new LinkedList<Person>();
        while(true){
            System.out.println("    ");
            System.out.println("Functions Available");
            System.out.println("A - Display current state of Queue");
            System.out.println("B - Enqueue");
            System.out.println("C - Dequeue");
            System.out.println("D - Sort by Name in Ascension");
            System.out.println("E - Sort by Name in Descension");
            System.out.println("F - Sort by Age in Ascension");
            System.out.println("G - Sort by Age in Descension");
            System.out.println("H - Size of the queue");
            System.out.println("X - exit program");
            System.out.println("    ");
            System.out.print("Choose your Function: ");
            Scanner scan = new Scanner(System.in);
            String a=scan.nextLine();
            switch(a){
                case "A":
                    DisplayQ(persons);
                    break;
                case "B":
                    NQ(persons);
                    break;
                case "C":
                    DQ(persons);
                    break;
                case "D":
                    AlphaSortASC(persons);
                    break;
                case "E":
                    AlphaSortDSC(persons);
                    break;
                case "F":
                    AgeSortASC(persons);
                    break;
                case "G":
                    AgeSortDSC(persons);
                    break;
                case "H":
                    QSize(persons);
                    break;
                case "X":
                    break;
                
            }    
        }
    }
    
    public static void NQ(Queue<Person> persons) {
        Scanner scan= new Scanner(System.in);
        System.out.print("How many names are you adding?: ");
        int num=Integer.parseInt(scan.nextLine());
        System.out.println("Enter "+num+" People's names and Ages");
        System.out.println("/LastName, / /FirstName, / /MiddleInitial/Name, / /Age/");
        for (int i = 0; i < num; i++) {
            System.out.println("Enter full name and age:");
            persons.add(new Person(scan.nextLine(),scan.nextLine(),scan.nextLine(),Integer.parseInt(scan.nextLine())));
        }
        System.out.println("queue now has:"+persons.size()+"Names");
    }

    public static void DQ(Queue<Person> persons){
        Scanner scan= new Scanner(System.in);
        System.out.print("How many do you want to remove?: ");
        int rmv= Integer.parseInt(scan.nextLine());
        for (int i = 0; i < rmv; i++) {
            persons.poll();
        }
        if(persons.isEmpty()){
            System.out.println("Queue is empty");
        }
    }

    public static void DisplayQ(Queue<Person> persons) {
        for (Person peeps:persons){
            System.out.println(peeps);
        }
        if(persons.isEmpty()){
            System.out.println("Queue is empty");
        }
    }

    public static void AlphaSortASC(Queue<Person> persons){
        List<Person> lists = (List<Person>) persons; 
        Collections.sort(lists, new Comparator<Person>() {

            public int compare(Person t, Person t1) {
                int comp = t.getLname().compareTo(t1.getLname());
                if (comp != 0) {    // names are different
                    return comp;
                }
                return t.getAge() - t1.getAge();
            }
        });
        System.out.println("Queue sorted by name in ascension");
        for (Person listy:lists) {
            System.out.println(listy);
        }
        if(persons.isEmpty()){
            System.out.println("Queue is empty");
        }
    }

    public static void AlphaSortDSC(Queue<Person> persons){
        List<Person> lists = (List<Person>) persons; 
        Collections.sort(lists, new Comparator<Person>() {

            public int compare(Person t1, Person t) {
                int comp = t.getLname().compareTo(t1.getLname());
                if (comp != 0) {    // names are different
                    return comp;
                }
                return t.getAge() - t1.getAge();
            }
        });
        System.out.println("Queue sorted by name in descension");
        for (Person listy:lists) {
            System.out.println(listy);
        }
        if(persons.isEmpty()){
            System.out.println("Queue is empty");
        }
    }

    public static void AgeSortASC(Queue<Person> persons){
        List<Person> lists = (List<Person>) persons; 
            Collections.sort(lists, new Comparator<Person>() { 
                @Override
                public int compare(Person t, Person t1) {
                return t.getAge() - t1.getAge();
            }
        });
        System.out.println("Queue sorted by age in ascension");
        for (Person listy:lists) {
            System.out.println(listy);
        }
        if(persons.isEmpty()){
            System.out.println("Queue is empty");
        }
    }

    public static void AgeSortDSC(Queue<Person> persons){
        List<Person> lists = (List<Person>) persons; 
            Collections.sort(lists, new Comparator<Person>() { 
                @Override
                public int compare(Person t1, Person t) {
                return t.getAge() - t1.getAge();
            }
        });
        System.out.println("Queue sorted by age in descension");
        for (Person listy:lists) {
            System.out.println(listy);
        }
        if(persons.isEmpty()){
            System.out.println("Queue is empty");
        }
    }

    public static void QSize(Queue<Person> persons) {
        System.out.println("The queue has "+persons.size()+" Names");
    }
}

Whenever I use the code, the break; does not work Can someone please help meee

The output is just this... over and over and over again

Functions Available
A - Display current state of Queue
B - Enqueue
C - Dequeue
D - Sort by Name in Ascension
E - Sort by Name in Descension
F - Sort by Age in Ascension
G - Sort by Age in Descension
H - Size of the queue
X - exit program
    
Choose your Function: X
    
    
Functions Available
A - Display current state of Queue
B - Enqueue
C - Dequeue
D - Sort by Name in Ascension
E - Sort by Name in Descension
F - Sort by Age in Ascension
G - Sort by Age in Descension
H - Size of the queue
X - exit program
    
Choose your Function: X
    
    
Functions Available
A - Display current state of Queue
B - Enqueue
C - Dequeue
D - Sort by Name in Ascension
E - Sort by Name in Descension
F - Sort by Age in Ascension
G - Sort by Age in Descension
H - Size of the queue
X - exit program
    
Choose your Function: 

java Objects:

class Person {
    String Lname;   //setting string for last name
    String Fname;   //setting string for first name
    String Mname;   //setting string for middle name
    int Age;    //setting integer for age

    public String getLname(){ //get last name
        return Lname;
    }
    public String getFname(){ // get first name
        return Fname;
    }
    public String getMname(){ // get middle name or initial
        return Mname;
    }
    public int getAge(){ // get age
        return Age;
    }
    public void setLname(String Lname){ 
        this.Lname=Lname;
    }
    public void setFname(String Fname){
        this.Fname=Fname;
    }
    public void setMname(String Mname){
        this.Mname=Mname;
    }
    public void setAge(int Age){
        this.Age=Age;
    }
    public Person (String Lname, String Fname, String Mname, int Age){ // the new object for the nodes
        this.Lname=Lname;
        this.Fname=Fname;
        this.Mname=Mname;
        this.Age=Age;
    }
    @Override
    public String toString() {
        return "Name:" + Lname + ", " + Fname + " " + Mname + "| Age:" + Age; // this is for the object to be printed into a string
    }
}

The break only break the switch and not the loop. To break the loop, you need to label the loop, and use the label to break the loop.

        loop: while(true){
            switch(a){
                case "X": break loop;
            }    
        }

You could use a boolean flag to indicate you want to break out of the while loop:

while(true){
    boolean breakWhile = false;
    switch(a){
        case "X": break loop;
            breakWhile = true; 
            break;
    }  
    if (breakWhile) {
        break;
    }  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM