Java 非常基本的程序使用while循环和if语句编译,但不起作用-我认为这一切都正确吗?

Java 非常基本的程序使用while循环和if语句编译,但不起作用-我认为这一切都正确吗?,java,if-statement,while-loop,Java,If Statement,While Loop,程序应反复询问用户他/她见过的鸟中的哪只和多少只,直到他们说“结束”,程序应存储看到的鸟最多,然后输出看到的鸟最多的鸟 运行时,程序会询问问题,然后在键入end时,输出总是“你看到了‘x’;这是你花园中曾经看到的最常见的鸟。”输出不会显示哪种鸟最受欢迎为什么 import java.util.Scanner; class birds { public static void main(String[] args) { questions(); }// end

程序应反复询问用户他/她见过的鸟中的哪只和多少只,直到他们说“结束”,程序应存储看到的鸟最多,然后输出看到的鸟最多的鸟

运行时,程序会询问问题,然后在键入end时,输出总是“你看到了‘x’;这是你花园中曾经看到的最常见的鸟。”输出不会显示哪种鸟最受欢迎为什么

import java.util.Scanner;

class birds {
    public static void main(String[] args) {

        questions();

    }// end main method

    public static void questions() {
        int largest = 0;
        String popularBird = "";
        while (true) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("Which bird have you seen?");
            String answerBird = scanner.nextLine();

            if (answerBird.equalsIgnoreCase("end")) {
                System.out.println("You saw " + largest + " " + popularBird);
                System.out.println("It was the most common bird seen at one time in your garden.");
            break;
        }//end if statement

    System.out.println("How many were in your graden at once?");            
    int answerNumber = scanner.nextInt();

        if(largest < answerNumber) {
            largest = answerNumber;
            answerBird = popularBird;
        }



        }//end while loop

    return;
    }// end method questions

}// end class bird
import java.util.Scanner;
鸟类分类{
公共静态void main(字符串[]args){
问题();
}//端主方法
公共静态无效问题(){
int最大=0;
字符串popularBird=“”;
while(true){
扫描仪=新的扫描仪(System.in);
System.out.println(“你见过哪只鸟?”);
字符串answerBird=scanner.nextLine();
if(应答鸟等信号情况(“结束”)){
System.out.println(“您看到了”+maxist+“”+popularBird);
println(“这是你花园里曾经见过的最常见的鸟。”);
打破
}//end if语句
System.out.println(“你的成绩一次有多少?”;
int answerNumber=scanner.nextInt();
if(最大值<应答号){
最大=应答器编号;
answerBird=popularBird;
}
}//边结束边循环
返回;
}//结束方法问题
}//末级鸟

您应该在
之外声明
扫描器
最大的
popularBird
变量,而
循环与当前代码一样,它们在每次迭代中都会被覆盖/替换

您可以使用内联注释引用以下代码:

//declare the variables outside the loop
int largest = 0;
String popularBird = "";
Scanner scanner = new Scanner(System.in);//use the same scanner

try{
    while (true) {
      System.out.println("Which bird have you seen?");
      String answerBird = scanner.nextLine();

     if (answerBird.equalsIgnoreCase("end")) {
                  System.out.println("You saw " + largest + " " + popularBird);
                   System.out.println("It was the most 
                         common bird seen at one time in your garden.");
                       break;
      }//end if statement

     System.out.println("How many were in your graden at once?");            
     int answerNumber = Integer.parseInt(scanner.nextLine());

     if(largest < answerNumber) {
                        largest = answerNumber;
                        popularBird  = answerBird;
              }
            }
     } finally {
            if(scanner != null)
               scanner.close();//close the scanner
         }
     }
//在循环外部声明变量
int最大=0;
字符串popularBird=“”;
扫描仪=新的扫描仪(System.in)//使用相同的扫描仪
试一试{
while(true){
System.out.println(“你见过哪只鸟?”);
字符串answerBird=scanner.nextLine();
if(应答鸟等信号情况(“结束”)){
System.out.println(“您看到了”+maxist+“”+popularBird);
System.out.println(“它是
曾经在你的花园里见过的普通鸟。”);
打破
}//end if语句
System.out.println(“你的成绩一次有多少?”;
int answerNumber=Integer.parseInt(scanner.nextLine());
if(最大值<应答号){
最大=应答器编号;
popularBird=应答鸟;
}
}
}最后{
如果(扫描器!=null)
scanner.close();//关闭扫描仪
}
}

int最大值=0总是在循环启动时调用,因为它在循环内部。也许你想把它放在外面?你不应该在人们开始回答之后再改变这个问题。这不公平地使他们的努力无效。如果你有新问题,请提出新问题。