Java return 一个方法中的多个字符串
Java return multiple strings in one method
我正在尝试编写一个程序,询问用户他们的宠物名字是什么,种类,找出口渴程度并给出相应的响应。
如果有人能帮助我解决我遇到的问题,我将不胜感激,在 askpetname 和 thirstlevel 两种方法中的每一种中,我都希望在不使用全局变量的情况下在整个 class 中访问 2 个字符串。
谁能告诉我我哪里做错了或指出正确的方向。
另外,我知道我过度使用方法来完成繁琐的任务是不好的做法,但它有助于记忆语法。
谢谢。
class dinoo
{
public static void main(String[] p)
{
explain();
output();
System.exit(0);
}
public static void explain()
{
print("The following program demonstrates use of user input by asking for pet name.");
return;
}
public static String askpetname()
{
Scanner scanner = new Scanner(System.in);
print("Name your dinosaur pet!");
String petname = scanner.nextLine();
print("Awesome, cool dinosaur name, what species is " + petname+ " ?");
String petspecies = scanner.nextLine();
return petname, petspecies;
}
public static int thirstlevel()
{
Random ran = new Random();
int thirst = ran.nextInt(11);
int hunger = ran.nextInt(11);
return thirst,hunger;
}
public static String anger(int thirst, int hunger)
{
double angerscore = (thirst+hunger)/2;
String temper;
if(angerscore<=2)
{
temper = "Serene";
}
else if(3<=angerscore<=6)
{
temper= "Grouchy";
}
else if(6<angerscore)
{
temper = "DANGEROUS";
}
return temper;
}
public static String warning()
{
if (temper.equals("Serene"))
{
print("He's looking happy!");
}
else if(temper.equals("Grouchy"))
{
print("Ahhh hes a bit "+temper+", you better start feeding him before he gets mad!");
}
else if(temper.equals("DANGEROUS"))
{
print("GET OUT OF THERE, HES " + temper+"!!!. He will have to be put down for everyones safety.");
}
}
public static void output()
{
print(askpetname() + "'s, thirst level is "+thirstlevel()+"/10");
return;
}
public static String print(String message)
{
System.out.println(message);
return message;
}
}
该代码无法编译,因为您不能:
return string1, string2;
或
else if(3<=angerscore<=6)
与其尝试 return 多个字符串,最好的办法是创建一个 class,比如说 Pet
,其中包含宠物名称的字符串字段,一个 Species其物种的领域,以及饥饿,口渴的任何其他领域......最好封装构成一个逻辑"pet"的所有数据以及getAnger()
等方法return 是一个取决于宠物状态的愤怒值。然后,您可以通过创建方法创建并 return 一个可行的 Pet 对象。
此外,您的代码有很多编译错误,建议您改进创建代码的方式。永远不要尝试将新代码添加到 "bad" 代码中,添加到无法编译的代码中。如果可能,请使用 NetBeans、Eclipse 或 IntelliJ 等 IDE 来帮助您创建程序。如果您的任何代码包含编译错误,IDE 将标记您,然后关键是:在您首先修复现有编译错误之前不要添加新代码.如果您不能使用 IDE,那么您必须尽早并经常编译,并做同样的事情——在添加新的之前修复所有错误。
首先,我建议在尝试之前先完成教程,完成所有涵盖作用域、对象、数组和函数的 hello world。熟悉面向对象的风格,尽管那甚至不是过程式编程……没什么 returns 2 个对象……总是 1 个(它可能是一个包含许多对象的数组,但数组是单个对象)
继续,虽然这是糟糕的编码实践,但对于初学者来说还可以,因为你的函数都是静态的,在每个函数中创建一个私有静态变量并创建 getter 个函数
//convert
String petname = scanner.nextLine();
// To this
private static String petname = scanner.nextLine();
// Then add this below it
public static String getPetName()
{
return petname;
}
你需要的每条数据都一样。
现在从所有函数中删除 return 语句并将 return 类型声明为 void
然后从 Main 调用所有函数,
askpetname();
thirstlevel();
然后打印最终输出(在调用函数之后)
System.out.println("Petname: " + getPetname + " ThirstLevel: " + getThirstLevel() + " HungerLevel: " + getHungerLevel);
我正在尝试编写一个程序,询问用户他们的宠物名字是什么,种类,找出口渴程度并给出相应的响应。
如果有人能帮助我解决我遇到的问题,我将不胜感激,在 askpetname 和 thirstlevel 两种方法中的每一种中,我都希望在不使用全局变量的情况下在整个 class 中访问 2 个字符串。
谁能告诉我我哪里做错了或指出正确的方向。
另外,我知道我过度使用方法来完成繁琐的任务是不好的做法,但它有助于记忆语法。
谢谢。
class dinoo
{
public static void main(String[] p)
{
explain();
output();
System.exit(0);
}
public static void explain()
{
print("The following program demonstrates use of user input by asking for pet name.");
return;
}
public static String askpetname()
{
Scanner scanner = new Scanner(System.in);
print("Name your dinosaur pet!");
String petname = scanner.nextLine();
print("Awesome, cool dinosaur name, what species is " + petname+ " ?");
String petspecies = scanner.nextLine();
return petname, petspecies;
}
public static int thirstlevel()
{
Random ran = new Random();
int thirst = ran.nextInt(11);
int hunger = ran.nextInt(11);
return thirst,hunger;
}
public static String anger(int thirst, int hunger)
{
double angerscore = (thirst+hunger)/2;
String temper;
if(angerscore<=2)
{
temper = "Serene";
}
else if(3<=angerscore<=6)
{
temper= "Grouchy";
}
else if(6<angerscore)
{
temper = "DANGEROUS";
}
return temper;
}
public static String warning()
{
if (temper.equals("Serene"))
{
print("He's looking happy!");
}
else if(temper.equals("Grouchy"))
{
print("Ahhh hes a bit "+temper+", you better start feeding him before he gets mad!");
}
else if(temper.equals("DANGEROUS"))
{
print("GET OUT OF THERE, HES " + temper+"!!!. He will have to be put down for everyones safety.");
}
}
public static void output()
{
print(askpetname() + "'s, thirst level is "+thirstlevel()+"/10");
return;
}
public static String print(String message)
{
System.out.println(message);
return message;
}
}
该代码无法编译,因为您不能:
return string1, string2;
或
else if(3<=angerscore<=6)
与其尝试 return 多个字符串,最好的办法是创建一个 class,比如说 Pet
,其中包含宠物名称的字符串字段,一个 Species其物种的领域,以及饥饿,口渴的任何其他领域......最好封装构成一个逻辑"pet"的所有数据以及getAnger()
等方法return 是一个取决于宠物状态的愤怒值。然后,您可以通过创建方法创建并 return 一个可行的 Pet 对象。
此外,您的代码有很多编译错误,建议您改进创建代码的方式。永远不要尝试将新代码添加到 "bad" 代码中,添加到无法编译的代码中。如果可能,请使用 NetBeans、Eclipse 或 IntelliJ 等 IDE 来帮助您创建程序。如果您的任何代码包含编译错误,IDE 将标记您,然后关键是:在您首先修复现有编译错误之前不要添加新代码.如果您不能使用 IDE,那么您必须尽早并经常编译,并做同样的事情——在添加新的之前修复所有错误。
首先,我建议在尝试之前先完成教程,完成所有涵盖作用域、对象、数组和函数的 hello world。熟悉面向对象的风格,尽管那甚至不是过程式编程……没什么 returns 2 个对象……总是 1 个(它可能是一个包含许多对象的数组,但数组是单个对象)
继续,虽然这是糟糕的编码实践,但对于初学者来说还可以,因为你的函数都是静态的,在每个函数中创建一个私有静态变量并创建 getter 个函数
//convert
String petname = scanner.nextLine();
// To this
private static String petname = scanner.nextLine();
// Then add this below it
public static String getPetName()
{
return petname;
}
你需要的每条数据都一样。
现在从所有函数中删除 return 语句并将 return 类型声明为 void
然后从 Main 调用所有函数,
askpetname();
thirstlevel();
然后打印最终输出(在调用函数之后)
System.out.println("Petname: " + getPetname + " ThirstLevel: " + getThirstLevel() + " HungerLevel: " + getHungerLevel);