[英]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.