即使我没有任何循环,它也会让我循环



我不知道哪里出了问题但它让我一直在循环即使我按下或输入1我想它调用子类代理但它没有但是当我按下2时它调用了子类这里出了什么问题我已经在这里呆了5-7个小时了我的眼睛都乱了

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
class Person 
{
protected String agentId = "20860132";
protected String password = "20020729" ;
protected String address;
public Person(String agentId,String password, String address) 
{
this.agentId = agentId;
this.password = password;
this.address = address;
Scanner input = new Scanner(System.in);
System.out.println("[1]AGENT");
System.out.println("[2]CUSTOMER");
int choice = input.nextInt();

//here is where the code loops it just keeps repeating this lines 
"System.out.println("[1]AGENT"); System.out.println("[2]CUSTOMER");"
if(choice == 1) 
{
Agent agent = new Agent("Niel", "diko alam", "umay");
}   
else if(choice == 2)
{
System.out.println("POTANGINA");
}   
}
}

上面的代码块应该调用这个类

class Agent extends Person 
{
public Agent(String agentId, String password, String address) 
{
super(agentId, password, address);
Scanner input2 = new Scanner(System.in);
System.out.println("[LOGIN]");
System.out.print("ENTER AGENT ID:");
int id = input2.nextInt();
System.out.print("ENTER PASSWORD:");
int pass = input2.nextInt();
if(id == 20860132 && pass == 20020729)
{
Scanner input = new Scanner(System.in);
System.out.println("[1]ADD CAR");
System.out.println("[2]SCHEDULE");
System.out.println("[3]RECORDS");
int choice2 = input.nextInt();
if(choice2 == 1)
{   
boolean stopFlag = false;
do 
{
List<String>cars = new ArrayList<String>();
System.out.println("[CARS]");
cars.add("Tayota");
cars.add("Hillux");
cars.add("Bugatti");
System.out.println(cars);
System.out.println("Enter Car:");
String car = input.nextLine();
cars.add(car);
System.out.println("Would you like to add more?");
System.out.println("[1]YES");
System.out.println("[2]NO");
String choice3 = input.nextLine();
addCar(cars);
if(!choice3.equals(1))
stopFlag = true;
}while(!stopFlag);
}
}
else 
{
System.out.println("INCORRECT PLEASE TRY AGAIN.");
}
}
public void addCar(List<String> cars) 
{
try 
{
FileWriter fw = new FileWriter("cars.txt", true);
PrintWriter pw = new PrintWriter(fw);
pw.println(cars);
pw.close();
}
catch (IOException e) 
{
e.printStackTrace();
}
}
public void schedule(String schedule) 
{
try 
{
FileWriter fw = new FileWriter("schedule.txt", true);
PrintWriter pw = new PrintWriter(fw);
pw.println(schedule);
pw.close();
} 
catch (IOException e) 
{
e.printStackTrace();
}
}
public void records(String record) 
{
try
{
FileWriter fw = new FileWriter("records.txt", true);
PrintWriter pw = new PrintWriter(fw);
pw.println(record);
pw.close();
} 
catch (IOException e) 
{
e.printStackTrace();
}
}
}


class Customer extends Person 
{
private String customerId;
public Customer(String agentId, String password, String address, String customerId) 
{
super(agentId, password, address);
this.customerId = customerId;
}
public void setCustomerId(String customerId) 
{
this.customerId = customerId;
}
public String getCustomerId() 
{
return customerId;
}
public void rentCar(String car) 
{
try 
{
FileWriter fw = new FileWriter("cars.txt", true);
PrintWriter pw = new PrintWriter(fw);
pw.println(car);
pw.close();
} 
catch (IOException e) 
{
e.printStackTrace();
}
}
public void viewSchedule(String schedule) 
{
try 
{
FileWriter fw = new FileWriter("schedule.txt", true);
PrintWriter pw = new PrintWriter(fw);
pw.println(schedule);
pw.close();
} 
catch (IOException e) 
{
e.printStackTrace();
}
}
public void extend(String record) 
{
try 
{
FileWriter fw = new FileWriter("records.txt", true);
PrintWriter pw = new PrintWriter(fw);
pw.println(record);
pw.close();
} 
catch (IOException e) 
{
e.printStackTrace();
}
}
}

这里是主方法

public class Finals 
{
public static void main(String[] args) 
{
Person person = new Person("20860132", "h208f32", "San luis");
Agent agent = new Agent("20860132", "h208f32", "San luis");
}
}

Agent构造函数中的第一行调用super, super是Person的构造函数,将再次请求输入1和2。这就是你的"循环"。当选择1时,它将开始创建另一个Agent对象,该对象将再次执行相同的操作。如果你一直选择"1",你将永远无法通过Agent中的super call。

将打印/输入逻辑放在其他地方,比如在main方法中。构造函数应该简单。例如,您的Person构造函数中的逻辑,将其移动到final类(public static void createPerson)中的静态方法中,其参数与构造函数现在具有的参数相同,然后从您的主方法而不是new Person调用该方法。除此之外,还有很多需要改进的地方,但这可能会修复你的"循环"。

最新更新