gpt4 book ai didi

java - 方法错误,Class不能应用于不同类型?

转载 作者:行者123 更新时间:2023-12-02 07:08:51 26 4
gpt4 key购买 nike

我想将主方法中的字符串转换为另一个方法中的整数,但出现错误。

`    public static void main(String[] args) 
{

System.out.println("Enter a date (use the format -> (MM/DD/YYYY)");

//declare Scanner
Scanner in = new Scanner (System.in);

System.out.println("Enter a month (MM): ");
String month = in.nextLine();

System.out.println("Enter a day (DD): ");
String day = in.nextLine();

System.out.println("Enter a year (YYYY): ");
String year = in.nextLine();

String enteredDate = month + "/" + day + "/" + year;

if (main.isValidDate(enteredDate))
{
main.leapYearCheck();
}
}

private boolean isValidDate(String enteredDate)
{
//logic
parsedDate = null;// if it's valid set the parsed Calendar object up.
return true;
}

// other code

private void leapYearCheck(String year)
{
//leapyear
int theYear = Integer.parseInt(year);

if (theYear < 100)
{
if (theYear > 40)
{
theYear = theYear + 1900;
}
else
{
theYear = theYear + 2000;
}
}

if (theYear % 4 == 0)
{
if (theYear % 100 != 0)
{
System.out.println(theYear + " is a leap year.");
}
else if (theYear % 400 == 0)
{
System.out.println(theYear + " is a leap year.");
}
else
{
System.out.println(theYear + " is not a leap year.");
}
}
else
{
System.out.println(theYear + " is not a leap year.");
}
}//end of leap year

//other code }`

我收到错误: Date.java:31: 错误:Date类中的方法leapYearCheck无法应用于给定类型;
main.leapYearCheck();
^
必需:字符串
发现:没有参数
原因:实际参数列表和正式参数列表的长度不同
1 个错误

我不明白这个错误。说我需要一个字符串,并且由于该方法使用整数(我认为)我需要返回一个字符串?我该如何解决这个问题?

最佳答案

您需要根据定义将年份以字符串形式传递给 leapYearCheck 方法。

关于java - 方法错误,Class不能应用于不同类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15777844/

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