【问题标题】:Java While Loops Issue(new)Java While 循环问题(新)
【发布时间】: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


【解决方案1】:

你的 if 语句的内容是向后的,试试这个:

if(howmany > mostseen)
{
   mostseen = howmany;
}

还有,

print("You saw " + mostseen + " " + bird +"\n It was the most common bird in your garden.");

可能应该在一段时间之外?这样,您只会在终止时通知用户,而不是每次他们输入新条目时。您并没有真正的设计可以让您跳出循环,但这就是您的问题所述...或者,您可以将其放在 if 语句中,以便仅在条件为真时打印出来

【讨论】:

    【解决方案2】:

    正如其他人指出的那样,您的 if 块替换是倒退的。

    创建一个实用方法来执行 System.out.println() 是过度封装。

    一遍又一遍地创建对象会浪费系统资源并降低代码的可读性,但是您走在正确的轨道上。

    比较和对比这个。

    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();
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-01-03
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      • 2011-06-19
      • 2014-05-27
      • 1970-01-01
      相关资源
      最近更新 更多