I'm having an issue with my program.
I want to store the number of birds seen in a while loop and then print the most seen bird when the program is terminated.
Im having issues with the if statement.
Any help will be appreciated.
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;
}
}
The contents of your if statement are backwards, try this:
if(howmany > mostseen)
{
mostseen = howmany;
}
Also,
print("You saw " + mostseen + " " + bird +"\n It was the most common bird in your garden.");
should probably go outside of the while? That way you're only informing the user on termination, instead of every time they make a new entry. You don't really have a design that allows you to break out of the loop, but that's what your question stated...Or, you could put it inside of the if statement so it is only printed out when the condition is true