【发布时间】:2016-11-09 13:38:39
【问题描述】:
我的程序有问题。
我想存储在 while 循环中看到的鸟的数量,然后在程序终止时打印最常见的鸟。
我对 if 语句有疑问。
任何帮助将不胜感激。
import java.util.*;
class gardenbird
{
public static void main(String[] main)
{
askbird();
System.exit(0);
}// END MAIN METHOD
public static void askbird()
{
final int sentinel = -1;
int mostseen = 0;
int howmany = 0;
print("When you want to end the program type in "+sentinel);
while(howmany != sentinel)
{ Scanner scanner = new Scanner(System.in);
print("Which bird have you seen?");
String bird = scanner.nextLine();
howmany = input("How many where in your garden at once?");
if(howmany>mostseen)
{
howmany = mostseen;
}
print("You saw " + howmany+ " " + bird +"\n It was the most common bird in your garden.");
}
}
public static String print(String message)
{
System.out.println(message);
return message;
}
public static int input(String count)
{
Scanner scanner = new Scanner(System.in);
print(count);
String number1=scanner.nextLine();
int number = Integer.parseInt(number1);
return number;
}
}
【问题讨论】:
-
howmany = mostseen应该是mostseen = howmany。 -
您也可以重新考虑过度使用新的 Scanner 实例。
标签: java if-statement methods while-loop procedural-programming