while循环和scanner java出现问题



编程非常新。我知道这个问题对于经验丰富的程序员来说很容易回答。我想继续运行这个程序,它可以找到闰年,直到用户输入"n"。该程序在能够输入y/n之前终止。非常感谢您的帮助。。。非常感谢。

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        String another = "y";
        Scanner scan = new Scanner(System.in);
        System.out.print("Please enter a year ");
        int year = scan.nextInt();
        while (another.equals("y")) {
            if (year < 1582) {
                System.out.println("Not an established Gregorian year.");
            } else if (year % 4 != 0 || (year % 100 == 0 && year % 400 != 0)) {
                System.out.println("Not a leap year");
            } else {
                System.out.println("Leap year!");
            }
            System.out.println();
            System.out.print("Test another year (y/n)?");
            another = scan.nextLine();
        }
    }
}

当人员在开始时输入年份号时的输入不会用完。它存储在"缓冲区"中,当程序到达您询问他们是否想再做一年的点时,它会读取缓冲区中的输入。要解决此问题,您只需要再次扫描。nextLine();在重要的一个之前。

最新更新