尝试使用setVaraible()
使字符串"Hometown"
具有相同的用户输入,以便稍后可以回调它。之前的其他人曾帮助过我这种性质的东西,但它被用于循环中,所以我无法确切地看到它是如何工作的。
private static String InputName;
private static String Sex;
private static String Age;
private static String input;
private static String Hometown;
看起来像这样:
while (sc.hasNext()) {
input = sc.next();
System.out.println("");
setVariable(a, input);
if(input.equalsIgnoreCase("no")){
sc.close();
break;
}
else if(a>questions.length -1)
{
a = 0;
}
else{
a++;
}
if(a>questions.length -1){
System.out.println("Fine, " + InputName
+ ", so, you are " + Age + " years old and " + Sex + "." );
System.out.println("");
sleep();
System.out.println("Do you need to change any information?\n");
}
if(!input.equalsIgnoreCase("no") && a<questions.length){
System.out.println(questions[a]);
}
而且,我只需要它(我知道这段代码错了):
static public void Intro() {
int a = 0;
setVariable(3, Hometown);
String[] questions = {"What do you want to name your hometown?"};
System.out.println(questions);
input = sc.next();
}
以下是setVariable()
方法:
static void setVariable(int a, String Field) {
switch (a) {
case 0:
InputName = Field;
return;
case 1:
Age = Field;
return;
case 2:
Sex = Field;
return;
case 3:
Hometown = Field;
return;
}
}
答案 0 :(得分:1)
基本上,你可以通过传递两个值来调用setVariable
,第一个是int
值,表示要设置的字段,第二个是要应用的值,例如
setVariable(0, "This is the input name");
将InputName
变量设置为“这是输入名称”
使用1
可以更改Age
变量,2
Sex
变量和3
Hometown
变量,所以基本上,你需要尝试的是...... ...
setVariable(3, "New home town value");
例如......
目前您的代码看起来像......
static public void Intro() {
int a = 0;
setVariable(3, Hometown);
String[] questions = {"What do you want to name your hometown?"};
System.out.println(questions);
input = sc.next();
}
这基本上将Hometown
的当前值分配给自己......可能是null
,然后您尝试从Scanner
反过来试试,例如......
static public void Intro() {
System.out.println("What do you want to name your hometown?");
input = sc.nextLine();
setVariable(3, input);
}
确保将input
值传递给方法,以便将值分配给正确的字段...
就个人而言,我认为这不是有用或直观的。当然你可以使用enum
甚至定义static final
变量来确定你想要更改哪个字段更容易,但Java是面向对象的语言,我们不妨利用它...
我建议您定义一个Person
对象(为了论证),您可以使用它来设置/获取对象的各个属性,例如......
public class Person {
private String inputName;
private String sex;
private String age;
private String hometown;
public void setName(String name) {
this.inputName = name;
}
public String getName() {
return inputName;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getSex() {
return sex;
}
public void setAge(String age) {
this.age = age;
}
public String getAge() {
return age;
}
public void setHometown(String hometown) {
this.hometown = hometown;
}
public String getHometown() {
return hometown;
}
}
然后您只需创建Person
Person person = new Person();
并填写...
person.setName("Ruphet");
person.setSex("Male");
person.setAge("18");
person.setHometown("Murembugie");
当你需要展示或“获得”一些价值......
System.out.println("Hello " + person.getName() + " from " + person.getHometown());
ps-
您可能希望通过Code Conventions for the Java Programming Language阅读,这会让其他人更容易阅读您的代码(以及您阅读其他代码)...
答案 1 :(得分:0)
尝试这样的事情。
// This method should not be static, but your Hometown is.
public static void setHometown(String input) {
// This should not be a static variable, also it shouldn't be capitalized.
if (input == null) {
Hometown = "";
} else {
Hometown = input;
}
}