简体   繁体   中英

Java While Loops Issue(new)

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

As others pointed out your if block replacement was backwards.

Creating a utility method to perform System.out.println() is excessive encapsulation.

Creating Objects over and over is wasteful of system resources and makes the code less readable, however you are on the right track.

Compare and contrast this.

import java.util.Scanner;

public class GardenBird
{
  public static void main(String[] main)
  {
    askbird();
    System.exit(0);
  }// END MAIN METHOD

  public static void askbird()
  {
    Scanner scanner = new Scanner(System.in);
    final int sentinel = -1;
    int mostseen = 0;
    int howmany = 0;
    String mostSeenBird = "";
    String currentBird = "";

    System.out.println("When you want to end the program type in " + sentinel);

    while (howmany != sentinel)
    {
      System.out.println("Which bird have you seen?");
      currentBird = scanner.nextLine();
      System.out.println("How many where in your garden at once?");
      howmany = Integer.parseInt(scanner.nextLine());

      if (howmany > mostseen)
      {
        mostseen = howmany;
        mostSeenBird = currentBird;
      }
    }
    System.out.println("You saw " + howmany + " " + mostSeenBird
        + "\n It was the most common bird in your garden.");
    scanner.close();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM