对象的循环不仅仅适用于方法-java
Thr程序似乎编译并运行良好唯一的问题是AvailabilityBooks(字符串标题)方法,该方法甚至没有循环通过列表“mathites”,并且不能正常工作。我不明白为什么会出现此问题。我是编程新手,希望能得到任何帮助。谢谢你的帮助。:) 主要类别:对象的循环不仅仅适用于方法-java,java,loops,object,Java,Loops,Object,Thr程序似乎编译并运行良好唯一的问题是AvailabilityBooks(字符串标题)方法,该方法甚至没有循环通过列表“mathites”,并且不能正常工作。我不明白为什么会出现此问题。我是编程新手,希望能得到任何帮助。谢谢你的帮助。:) 主要类别: import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.*; import java.lang.String; p
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.*;
import java.lang.String;
public class MainLibrary {
static Scanner scan=new Scanner(System.in);
public List <Student> mathites=new ArrayList<Student>();
public List <Professor> daskaloi=new ArrayList<Professor>();
public Student Scurrent;//apothikeuoun to antikeimeno pou brethike apo AnazitisiName
public Professor Pcurrent;
String biblia[]=new String[]{"2666","All About Love","Desert Solitaire","Disgrace","Geek Love","Gilead","A Good Man Is Hard to Find","The Handmaid's Tale",
"The Hitchhiker's Guide to the Galaxy","Infinite Jest","The Left Hand of Darkness","Lolita"};
public void firstMenu(){
boolean ch=true;
int answer=0;
while (ch){
System.out.println("=======MENU========\n1.Make account.\n2.Login\n3.Exit programm.");
answer=ChooseOption(1,3);
switch(answer){
case 1:
MakeAcc();
break;
case 2:
LoginSystem();
break;
case 3:
return;
default:
System.out.println("Wrong input!!");
}
}
}
public int PorS(){
boolean check=true;
int an=0;
while(check){ // check if student or Professor
check=false;
System.out.println("Are you a Student or Professor? ~~1=Student || 2=Professor");
try{
an=scan.nextInt(); //an= account property
scan.nextLine();
if(an!=1 && an!=2){
throw new InputMismatchException();
}
}
catch(InputMismatchException imex){
scan.nextLine();
System.out.println("Invalid Input");
check=true;
}
}
return an;
}
public void LoginSystem(){
boolean check;
int an,i,j;
String name="";
an=PorS();
check=true;
while (check){
check=false;
System.out.print("Enter username:");
name=scan.nextLine();
if(name.equals("exit")){
return;
}
if(AnazitisiName(name,an)){
System.out.println("Username: '"+name+"' does not exist.Type 'exit' if you want to quit ");
check=true;
}
}
if(an==1){
Scurrent.login();
Scurrent.LoginMenu();
}
else{
Pcurrent.login();
Pcurrent.LoginMenu();
}
}
public int ChooseOption(int a,int b){ //basic diabasma epilogon apo a mexri b
int an;
while(true){
try{
System.out.print("Option: ");
an=scan.nextInt();
System.out.println();
if (an<a || an>b)
throw new InputMismatchException();
return an;
}
catch(InputMismatchException aex){
System.out.println("Invalid Input!!");
scan.nextLine();
}
}
}
public void displayBooks (){ //kanei display ta biblia k dixnei poia einai available
int i=1;
System.out.println("---------Library Books---------");
for(String t : biblia){
System.out.println(i+++"."+t);
System.out.print(AvailabilityBooks(t)?" |Available":" |Not Available");
System.out.println();
}
System.out.println("13.exit");
System.out.println("-------------------------------");
}
public boolean AvailabilityBooks (String tittle){ //true if it's available
for (Student s : mathites){
System.out.println("AAA");
for (int i=0; i<s.books; i++){
if (s.GetTittle(i).equals(tittle))
return false;
}
}
for (Professor s : daskaloi){
for (int j=0; j<s.books; j++){
if (s.GetTittle(j).equals(tittle))
return false;
}
}
return true;
}
public void MakeAcc(){
String name="empty";
int password;
int temp,clas=0;
boolean check=true,upper;
clas=PorS();
check=true;
while(check){ //username input
System.out.println("Enter Username |5-12 characters used |at least 1 number and no more than 2 |at least 1 Uppercase letter !!!type 'exit' if you dont want to create account.");
System.out.print("Username: ");
name=scan.nextLine();
if (name.equals("exit")){
return;
}
System.out.println();
check=false;
upper=false;//check if there is one upper letter
if(!AnazitisiName(name,clas)){ //check if it's already exists
check=true;
System.out.println("This username already exists");
}
else if (name.length()<5 || name.length()>12){ //check username leangth
System.out.println("Invalid length!");
check=true;
}
else{
temp=0; //count characters
for (char p: name.toCharArray()){ //check if there are more than 2 digits
if( Character.isDigit(p) ){
temp++;
}
if (Character.isUpperCase(p)){
upper=true;
}
}
if (temp>2){
System.out.println("Only 2 numbers are allowed");
check=true;
}
else if (temp==0){
System.out.println("Username must contain at least 1 number!");
check=true;
}
if (!upper){
System.out.println("Username must contain at least 1 Uppercase letter ");
check=true;
}
}
}
System.out.println("New account ||| "+ name);
if(clas==1)
mathites.add(new Student(name));
else if (clas==2)
daskaloi.add(new Professor(name));
}
public boolean AnazitisiName(String n,int b){//true if name is available || b=1 then Student
int a;
if(b==1){
if (mathites.isEmpty()){
return true;
}
for (Student s : mathites){
System.out.println("AA");
if(s.GetName().equals(n)){
Scurrent=s;
return false;
}
}
return true;
}
else{
if (daskaloi.isEmpty()){
return true;
}
for (Professor s : daskaloi){
if(s.GetName().equals(n)){
Pcurrent=s;
return false;
}
}
return true;
}
}
public void printmath(){
for(Student p : mathites)
System.out.println(p.toString());
}
public static void main(String[] args) {
MainLibrary l1=new MainLibrary();
l1.firstMenu();
}
}
问题
在运行时,当您在AvailabilityBooks中时,它会引用您当前的对象。您可以在执行System.out.println(this)
在您的可用性手册中
这就是为什么学生列表在这里是空的,为什么它不会进入for循环
解决方案
要解决这个问题,您可以使列表独立于MainClass对象,因此只需将其设置为静态
publicstaticlist mathites=newarraylist()代码>
还有一个逻辑问题
假设第一个书名不相等,它将立即返回false并退出循环,而不检查其他书籍。
因此,您可以比较标题,您最初认为它不可用,然后当标题相等时,这意味着它可用,因此您可以立即返回true
如果学生和教授中没有相等的头衔,则返回false
public boolean AvailabilityBooks (String tittle){ //true if it's available
for (Student s : mathites){
System.out.println("AAA");
for (int i=0; i<s.books; i++){
if (s.GetTittle(i).equals(tittle))
return true;
}
}
for (Professor s : daskaloi){
for (int j=0; j<s.books; j++){
if (s.GetTittle(j).equals(tittle))
return true;
}
}
return false;
}
问题
在运行时,当您在AvailabilityBooks中时,它会引用您当前的对象。您可以在执行System.out.println(this)
在您的可用性手册中
这就是为什么学生列表在这里是空的,为什么它不会进入for循环
解决方案
要解决这个问题,您可以使列表独立于MainClass对象,因此只需将其设置为静态
publicstaticlist mathites=newarraylist()代码>
还有一个逻辑问题
假设第一个书名不相等,它将立即返回false并退出循环,而不检查其他书籍。
因此,您可以比较标题,您最初认为它不可用,然后当标题相等时,这意味着它可用,因此您可以立即返回true
如果学生和教授中没有相等的头衔,则返回false
public boolean AvailabilityBooks (String tittle){ //true if it's available
for (Student s : mathites){
System.out.println("AAA");
for (int i=0; i<s.books; i++){
if (s.GetTittle(i).equals(tittle))
return true;
}
}
for (Professor s : daskaloi){
for (int j=0; j<s.books; j++){
if (s.GetTittle(j).equals(tittle))
return true;
}
}
return false;
}
问题似乎是for循环中的return
语句。这将退出该方法,并且不会进行进一步的循环迭代。这发生在AvailabilityBooks
方法中。我想你应该反复阅读那里所有的教授、学生和书籍。另外,你应该重新检查你的逻辑。退出checkStudentOrProfessor的唯一方法是输入无效输入。那是不可能的sense@Sebastian我知道,这正是我想要的,但它似乎甚至没有循环通过if语句…你的可用性书籍的逻辑是什么,让它可供学生和教授使用?如果书名相等,为什么返回false?@Alex It正在检查这本书是否已被借用。如果有人(教授或学生)借用了这本书,它应该不可用。问题似乎是for循环中的return
语句。这将退出该方法,并且不会进行进一步的循环迭代。这发生在AvailabilityBooks
方法中。我想你应该反复阅读那里所有的教授、学生和书籍。另外,你应该重新检查你的逻辑。退出checkStudentOrProfessor的唯一方法是输入无效输入。那是不可能的sense@Sebastian我知道,这正是我想要的,但它似乎甚至没有循环通过if语句…你的可用性书籍的逻辑是什么,让它可供学生和教授使用?如果书名相等,为什么返回false?@Alex它正在检查这本书是否已经被借走。如果有人(教授或学生)借了这本书,它应该不可用。作为参数给出的标题是图书馆中的一本书,如果其他人有,它将被检查。对于你所说的,如果我想检查所有的书,我将通过一个循环来检查图书馆拥有的每一本书。(检查displayBooks方法)。想象一下,如果我运行这个程序,命令System.out.println(“AAA”)甚至不会被打印。好的,我会检查这一点,但很明显,您的列表中没有学生。或者您是否收到任何错误?没有收到任何错误。如果您创建帐户,则正在创建一个学生并进入列表。并且您不能在没有帐户的情况下登录,以便访问方法可用性手册,列表中必须至少有一个学生对象。在其他方法相同的循环工作正常。(例如printmath())。作为参数给出的标题是图书馆中的一本书,如果其他人有它,它将被检查。对于你所说的,如果我想检查所有的书,我将通过一个循环来检查图书馆拥有的每一本书。(检查displayBooks方法)。想象一下,如果我运行这个程序,命令System.out.println(“AAA”)甚至不会被打印。好的,我会检查这一点,但很明显,您的列表中没有学生。或者您是否收到任何错误?没有收到任何错误。如果您创建帐户,则正在创建一个学生并进入列表。并且您不能在没有帐户的情况下登录,以便访问方法可用性手册,列表中必须至少有一个学生对象。在相同循环的其他方法工作正常。(例如printmath()。
public interface LibraryAccess{
public void returnBook(String tittle);
public void borrowBook(String tittle);
}
public interface NetworkAccess{
public void login();
public void logoff();
}
public boolean AvailabilityBooks (String tittle){ //true if it's available
for (Student s : mathites){
System.out.println("AAA");
for (int i=0; i<s.books; i++){
if (s.GetTittle(i).equals(tittle))
return true;
}
}
for (Professor s : daskaloi){
for (int j=0; j<s.books; j++){
if (s.GetTittle(j).equals(tittle))
return true;
}
}
return false;
}
---------Library Books---------
1.2666
AAA
|Available
2.All About Love
AAA
|Available
3.Desert Solitaire
AAA
|Available
4.Disgrace
AAA
|Available
5.Geek Love
AAA
|Available
6.Gilead
AAA
|Available
7.A Good Man Is Hard to Find
AAA
|Available
8.The Handmaid's Tale
AAA
|Available
9.The Hitchhiker's Guide to the Galaxy
AAA
|Available
10.Infinite Jest
AAA
|Not Available
11.The Left Hand of Darkness
AAA
|Available
12.Lolita
AAA
|Not Available
13.exit
-------------------------------
Option: