gpt4 book ai didi

java - 对类中的多个设置方法使用扫描仪方法

转载 作者:太空宇宙 更新时间:2023-11-04 06:17:28 25 4
gpt4 key购买 nike

我正在尝试在我的类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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com