gpt4 book ai didi

java - 闰年程序问题——似乎无法让它正常运行

转载 作者:行者123 更新时间:2023-12-01 05:38:38 25 4
gpt4 key购买 nike

我应该设计一个程序,要求用户输入年份,然后使用 for 方法确定该年是否是闰年,但我似乎无法让它工作。我第一次遵守时它就运行了,但它没有按照我想要的方式工作。现在我做了一些更改,但它根本没有运行。我究竟做错了什么?我必须有四种方法。

显示说明

isLeap

获取年份

显示结果

import java.util.Scanner;
public class LeapYearr
{
public static void main(String[] args)
{


//Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

int Year;
displayInstructions();
Year = getYear();
boolean leapYear;
leapYear = isLeap (Year);


}

public static void displayInstructions()
{
System.out.println("This program allows a users to enter a year then" +
" the program determines weather or not the year entered" +
"is leap Year or is not. ");
}

public static int getYear()

{
Scanner keyboard = new Scanner(System.in);

int Year;
System.out.print("Enter a Year of your choice: ");
System.out.println("");
System.out.println("");
Year = keyboard.nextInt();

while(Year<1000||Year>9999)
{

System.out.println("Invalid entry. Year has to be a 4 Digit Number!!");
System.out.println("");
System.out.print("Enter a Year of your choice: ");
System.out.println("");
Year = keyboard.nextInt;
}



return Year;
}

public static boolean isLeap (int Year)
{
if (Year % 4 != 0)
{
return false;
}
else if (Year % 400 == 0)
{
return true;
}
else if (Year % 100 == 0)
{
return false;
}
else
{
return true;
}
}
public static void displayResults( boolean leapYear, int Year)
{
if (isLeapYear(Year))
{
System.out.println(Year + "is a leap Year.");
}
else
{
System.out.println(Year + "is not a leap Year.");
}
}
}

最佳答案

您的代码当前无法编译。

getYear() 的 while 循环中修复此问题:

while(Year<1000||Year>9999)
{
System.out.println("Invalid entry. Year has to be a 4 Digit Number!!");
System.out.println("");
System.out.print("Enter a Year of your choice: ");
System.out.println("");
Year = keyboard.nextInt(); // you were missing '()' your parenthesis!
}

关于java - 闰年程序问题——似乎无法让它正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7720406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com