嵌套循环问题/循环 2 次并且 运行 默认为 switch case
Nested Loop Problem/ Looped 2 times and also running default in switch case
我的嵌套循环有问题。每次我请求显示特定数据类型时,它都会显示,但问题是。它总是使输出加倍。我该如何阻止它? Else 也显示了任何一个。我在这里搜索了所有嵌套解决方案,但在我的代码中没有看到类似的代码解决方案。或我的代码的任何相似答案。
NOTE: Im new in Programming JAVA and this is my first Project so please be patient on me and explain everything what i did wrong and where i get this error/bug :) Thank you in advance
这里是代码
case '2':
System.out.print("Enter Subject: ");
SB = buff.readLine();
File readSUB = new File("E:\Onceng Files\JAVA GROUP PROJECT\subject.txt");
File readN = new File("E:\Onceng Files\JAVA GROUP PROJECT\name.txt");
File readSN = new File ("E:\Onceng Files\JAVA GROUP PROJECT\studNo.txt");
File readSEC = new File("E:\Onceng Files\JAVA GROUP PROJECT\section.txt");
File readTD = new File ("E:\Onceng Files\JAVA GROUP PROJECT\TimeDate.txt");
Scanner scaSUB = new Scanner(readSUB);
Scanner scaN = new Scanner(readN);
Scanner scaSN = new Scanner(readSN);
Scanner scaSEC = new Scanner(readSEC);
Scanner scaT = new Scanner(readTD);
while(scaSUB.hasNextLine()) {
String s = scaSUB.nextLine();
SUBJECT.add(s);
}
while(scaN.hasNextLine()) {
String s = scaN.nextLine();
NAME.add(s);
}
while(scaSN.hasNextLine()) {
String s = scaSN.nextLine();
STUDNO.add(s);
}
while(scaSEC.hasNextLine()) {
String s = scaSEC.nextLine();
SECTION.add(s);
}
while(scaT.hasNextLine()) {
String s = scaT.nextLine();
TIME.add(s);
}
System.out.println("Attendance List by SUBJECT: "+SB);
System.out.println("SUBJECT\t\tNAME\t\t\tSTUDENT NO.\t\tTIME & DATE");
for(int x=0;x<SUBJECT.size();x++) {
if(SB.equals(SUBJECT.get(x))) {
System.out.println(SUBJECT.get(x)+"\t\t"+NAME.get(x)+"\t\t"+STUDNO.get(x)+"\t\t"+TIME.get(x));
} else
System.out.println("No Record of "+SB+" Subject");
}
break;
这里是输出
@#@#@#@#@ MENU @#@#@#@#@
[1] LOGIN ATTENDANCE
[2] SHOW ATTENDANCE BY SUBJECT
[3] SHOW ATTENDANCE BY SECTION
[4] SHOW ALL RECORD
Input Number: 2
Enter Subject: JAVA
Attendance List by SUBJECT: JAVA
SUBJECT NAME STUDENT NO. TIME & DATE
No Record of JAVA Subject
No Record of JAVA Subject
No Record of JAVA Subject
JAVA Saludaga Joshua 19-01297 Tue, 12/03/2019 18:02
JAVA Pulano Hardhie 19-00900 Tue, 12/03/2019 18:02
JAVA Tatoy Cherrylyn 19-00751 Tue, 12/03/2019 18:02
for(int x=0;x<SUBJECT.size();x++) {
if(SB.equals(SUBJECT.get(x))) {
System.out.println(SUBJECT.get(x)+"\t\t"+NAME.get(x)+"\t\t"+STUDNO.get(x)+"\t\t"+TIME.get(x));
} else
System.out.println("No Record of "+SB+" Subject");
}
上面应该写成.
boolean found = false;
for(int x=0;x<SUBJECT.size();x++) {
if(SB.equals(SUBJECT.get(x))) {
System.out.println(SUBJECT.get(x)+"\t\t"+NAME.get(x)+"\t\t"+STUDNO.get(x)+"\t\t"+TIME.get(x));
found =true;
}
}
if(!found)
System.out.println("No Record of "+SB+" Subject");
希望这能解决您的问题。
我的嵌套循环有问题。每次我请求显示特定数据类型时,它都会显示,但问题是。它总是使输出加倍。我该如何阻止它? Else 也显示了任何一个。我在这里搜索了所有嵌套解决方案,但在我的代码中没有看到类似的代码解决方案。或我的代码的任何相似答案。
NOTE: Im new in Programming JAVA and this is my first Project so please be patient on me and explain everything what i did wrong and where i get this error/bug :) Thank you in advance
这里是代码
case '2':
System.out.print("Enter Subject: ");
SB = buff.readLine();
File readSUB = new File("E:\Onceng Files\JAVA GROUP PROJECT\subject.txt");
File readN = new File("E:\Onceng Files\JAVA GROUP PROJECT\name.txt");
File readSN = new File ("E:\Onceng Files\JAVA GROUP PROJECT\studNo.txt");
File readSEC = new File("E:\Onceng Files\JAVA GROUP PROJECT\section.txt");
File readTD = new File ("E:\Onceng Files\JAVA GROUP PROJECT\TimeDate.txt");
Scanner scaSUB = new Scanner(readSUB);
Scanner scaN = new Scanner(readN);
Scanner scaSN = new Scanner(readSN);
Scanner scaSEC = new Scanner(readSEC);
Scanner scaT = new Scanner(readTD);
while(scaSUB.hasNextLine()) {
String s = scaSUB.nextLine();
SUBJECT.add(s);
}
while(scaN.hasNextLine()) {
String s = scaN.nextLine();
NAME.add(s);
}
while(scaSN.hasNextLine()) {
String s = scaSN.nextLine();
STUDNO.add(s);
}
while(scaSEC.hasNextLine()) {
String s = scaSEC.nextLine();
SECTION.add(s);
}
while(scaT.hasNextLine()) {
String s = scaT.nextLine();
TIME.add(s);
}
System.out.println("Attendance List by SUBJECT: "+SB);
System.out.println("SUBJECT\t\tNAME\t\t\tSTUDENT NO.\t\tTIME & DATE");
for(int x=0;x<SUBJECT.size();x++) {
if(SB.equals(SUBJECT.get(x))) {
System.out.println(SUBJECT.get(x)+"\t\t"+NAME.get(x)+"\t\t"+STUDNO.get(x)+"\t\t"+TIME.get(x));
} else
System.out.println("No Record of "+SB+" Subject");
}
break;
这里是输出
@#@#@#@#@ MENU @#@#@#@#@
[1] LOGIN ATTENDANCE
[2] SHOW ATTENDANCE BY SUBJECT
[3] SHOW ATTENDANCE BY SECTION
[4] SHOW ALL RECORD
Input Number: 2
Enter Subject: JAVA
Attendance List by SUBJECT: JAVA
SUBJECT NAME STUDENT NO. TIME & DATE
No Record of JAVA Subject
No Record of JAVA Subject
No Record of JAVA Subject
JAVA Saludaga Joshua 19-01297 Tue, 12/03/2019 18:02
JAVA Pulano Hardhie 19-00900 Tue, 12/03/2019 18:02
JAVA Tatoy Cherrylyn 19-00751 Tue, 12/03/2019 18:02
for(int x=0;x<SUBJECT.size();x++) {
if(SB.equals(SUBJECT.get(x))) {
System.out.println(SUBJECT.get(x)+"\t\t"+NAME.get(x)+"\t\t"+STUDNO.get(x)+"\t\t"+TIME.get(x));
} else
System.out.println("No Record of "+SB+" Subject");
}
上面应该写成.
boolean found = false;
for(int x=0;x<SUBJECT.size();x++) {
if(SB.equals(SUBJECT.get(x))) {
System.out.println(SUBJECT.get(x)+"\t\t"+NAME.get(x)+"\t\t"+STUDNO.get(x)+"\t\t"+TIME.get(x));
found =true;
}
}
if(!found)
System.out.println("No Record of "+SB+" Subject");
希望这能解决您的问题。