簡體   English   中英

雖然循環沒有停止/中斷命令不起作用 JAVA

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

我正在嘗試創建一個隊列,您可以在其中為不同的功能使用不同的命令,並且我使用了一個 while 循環以使其可重用。 但是break; 開關盒中的命令不起作用。

這是代碼...

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");
    }
}

每當我使用代碼時,都會break; 不工作 有人可以幫助我嗎

output 就是這樣……一遍又一遍

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 對象:

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
    }
}

break 只會中斷開關而不是循環。 要打破循環,您需要 label 循環,並使用 label 打破循環。

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

您可以使用 boolean 標志來指示您要跳出while循環:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM