我正在尝试在我的类Address
中创建扫描仪方法,因此我不必在需要用户输入的每个方法中重复扫描仪代码。我遇到的问题是,当我在 Address
类实例的测试程序中运行这两个 set 方法时,此方法似乎只返回我的实例变量值,而不是用户的扫描仪输入。
我该怎么做?我不想在每个方法中重复 scr.nextInt()
等。
//Scanner method for integer inputs from user.
public int scanInt(){
int userInt = scr.nextInt();
scr.nextLine();
return userInt;
}
//Scanner method for string inputs from user
public String scanLine(){
String userString = scr.nextLine();
return userString;
}
//in these methods below (and others) I want to use the above scanner methods so I don't have to repeat the code for scanner inputs in every single method.
public void setStreet(){
System.out.println("Skriv in gata: ");
scanLine();
}
public void setStreetNr(){
System.out.println("Skriv in gatunummer: ");
scanInt();
}
最佳答案
public void setStreet(){
System.out.println("Skriv in gata: ");
street = scanLine();
}
关于java - 对类中的多个设置方法使用扫描仪方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27903603/