如何让Java闰年程序循环运行直至输入0退出?
Java闰年程序循环功能修复指导
问题描述
我正在完成编程入门课程的期末作业,无法实现让Java闰年程序循环运行直至用户输入0退出的功能。老师要求必须使用指定代码格式,不允许简化代码。整个16周课程中仅学习了约4周Java,对Java的使用和理解较为生疏。
目前程序可接收年份输入、判断是否为闰年、输出对应结果后直接退出。checkValidYear方法会输出提示语“如果想退出请输入0。”,但无法接收后续输入。需要指导修改程序,使其循环运行直至用户输入0才停止。
当前代码
主文件代码(FinalProject.java)
/** * Main class of the Java program. */ import java.util.Scanner; public class FinalProject { public static void main(String[] args) { int usersYear; //set up the scanner Scanner inputReader = new Scanner(System.in); //Create leap year instance LeapYear inputYear = new LeapYear(); do { System.out.println("Enter a year which I will test if it is a leap year. Input 0 to stop"); int i = inputReader.nextInt(); inputYear.setYear(i); } while(inputYear.checkValidYear().equals("invalid")); inputReader.close(); System.out.println(inputYear.getOutput()); // create a loop to which sees if the user wants to stop // in the loop: // use the setYear() method to set the year inside the object // use the CheckforLeapYear() method to do all the testing. DO NOT SEND YEAR because it was already set using setYear() // use the getOutput() to get the message you print - is it a leap year or not. // get the next year from the user } //end main method } //end Main class
LeapYear类代码(LeapYear.java)
/** LeapYear class determines if a year is a leap year **/ class LeapYear { /**Declarations**/ /* * 1. Create a private year variable that is an integer * 2. Create a getter and setter method for the field variable * 3. Create a default constructor.Do not set any variables **/ private int year; public LeapYear(){ } // Getter public int getYear() { return year; } // Setter public void setYear(int newYear) { year = newYear; } /* * This method should call all the checkIfYearDivisibleby4(), checkIfYearNotDivisibleby100(), and checkIfYearDivisibleby400() * @return int year **/ public boolean checkForLeapYear(){ boolean result = false; //add code here result= checkIfYearDivisibleby4()|| checkIfYearNotDivisibleby100()|| checkIfYearDivisibleby400(); return result; } /* * This method should return true if year is divisible by 4 * @return int year **/ private boolean checkIfYearDivisibleby4(){ boolean result = false; //add code here result = year%4==0; return result; } /* * This method should return true if year is NOT divisible by 100 * @return int year **/ private boolean checkIfYearNotDivisibleby100(){ boolean result = false; //add code here result = year%100==0; return result; } /* * This method should return true if year is divisible by 400 * @return int year **/ private boolean checkIfYearDivisibleby400(){ boolean result = false; //add code here result = year%400==0; return result; } /* * This method calls the checkForLeapYear() * if the checkForLeapYear() returns true return a string that states it is a leap year * otherwise, return a string that states it is not a leap year * @return int year **/ public String getOutput(){ String output= ""; if (checkForLeapYear()) { output = "This is a leap year"; } else { output = "This is not a leap year"; } return output; } public String checkValidYear(){ System.out.println("Enter 0 if you'd like to exit."); String result = "valid"; if(year==0) result = "invalid"; return result; } }
当前程序输出
Enter a year which I will test if it is a leap year. Input 0 to stop 2023 Enter 0 if you'd like to exit. This is not a leap year
修复步骤及修改后代码
1. 修正LeapYear类中的逻辑错误
你的闰年判断逻辑存在两处核心错误:
checkIfYearNotDivisibleby100方法:方法名要求返回“年份不能被100整除”的结果,但当前代码返回year%100==0(即能被100整除),逻辑完全相反,需改为year%100 != 0。checkForLeapYear方法:闰年的正确规则是能被4整除,且(不能被100整除 或 能被400整除),当前用||串联所有条件的逻辑错误,需调整为checkIfYearDivisibleby4() && (checkIfYearNotDivisibleby100() || checkIfYearDivisibleby400())。
修改后的LeapYear类关键方法:
// 修正checkIfYearNotDivisibleby100方法 private boolean checkIfYearNotDivisibleby100(){ boolean result = false; result = year%100 != 0; // 改为不等于0 return result; } // 修正checkForLeapYear方法 public boolean checkForLeapYear(){ boolean result = false; result = checkIfYearDivisibleby4() && (checkIfYearNotDivisibleby100() || checkIfYearDivisibleby400()); return result; } // 提示语改为中文(可选) public String checkValidYear(){ System.out.println("如果想退出请输入0。"); String result = "valid"; if(year==0) result = "invalid"; return result; }
2. 修改主程序的循环结构
当前主程序的do-while循环条件颠倒,且输出仅在循环外执行一次。需要将输入、判断、输出全部放入循环内,直到用户输入0才退出:
修改后的FinalProject.java主方法:
public static void main(String[] args) { int usersYear; //set up the scanner Scanner inputReader = new Scanner(System.in); //Create leap year instance LeapYear inputYear = new LeapYear(); do { System.out.println("请输入一个年份,我会判断它是否为闰年。输入0退出"); int i = inputReader.nextInt(); inputYear.setYear(i); // 如果输入的是0,直接退出循环 if(i == 0){ System.out.println("程序已退出"); break; } // 输出闰年判断结果 System.out.println(inputYear.getOutput()); } while(true); // 无限循环,直到输入0时break inputReader.close(); }
3. 调整输出提示语为中文(可选)
若需要全中文输出,可修改getOutput方法:
public String getOutput(){ String output= ""; if (checkForLeapYear()) { output = "这是闰年"; } else { output = "这不是闰年"; } return output; }
修复后程序运行示例
请输入一个年份,我会判断它是否为闰年。输入0退出 2020 这是闰年 请输入一个年份,我会判断它是否为闰年。输入0退出 2023 这不是闰年 请输入一个年份,我会判断它是否为闰年。输入0退出 0 程序已退出
内容的提问来源于stack exchange,提问作者imadethisaccountformyfinal
相关产品推荐
相关产品推荐

