我正在嘗試編寫一個程序,詢問用戶他們的寵物名稱是什麼,物種,找到口渴級別並相應地給出響應。Java以一種方式返回多個字符串
我將不勝感激,如果有人可以幫助我有一個問題,IM,在每個2種方法askpetname和thirstlevel有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;
}
}
你能澄清你正在談論哪些字符串嗎?看着'thirstlevel()'裏面沒有字符串?你還會在哪裏使用這些字符串?全球變量並不是世界上最糟糕的事情。 – user123
我的壞我的意思是int在thirstlevel。 – coding95