gpt4 book ai didi

java - 在Java中发现驱动类中没有参数

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

我已经尝试了很多方法,并在这个问题上花费了无数时间,但没有任何运气。我所拥有的是一个类,需要获取用户输入的月份作为 int 并使用 switch 语句转换为字符串(相应的月份)。我已经尝试了很多方法,但由于某种原因,我收到了所需参数和找到的参数长度不同的错误。(我尝试了很多方法,例如简单地使用 S.O.P、toString 等,但到目前为止没有任何效果)

总结:通过switch语句将整数转为字符串。

这是我的驱动程序类:

import java.util.Scanner;
/**
* Write a description of class TestMonthDays here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class TestMonthDays
{
public static void main(String[] args)
{
//Creates scanner
Scanner keyboard = new Scanner(System.in);

//Brings the MonthDays class into the test class
MonthDays date;
date = new MonthDays();

//Asks user for number 1-12 and if its not between that range then asks the user again
System.out.print ("Enter the number of a month [1-12]: ");
int numberMonth = keyboard.nextInt();

while (!(numberMonth >= 1 && numberMonth <= 12))
{
System.out.print ("Please enter a number of a month between 1 and 12: ");
numberMonth = keyboard.nextInt();
}

//Gets the year that the user inputs
System.out.print ("\nEnter the number of a year, greater than 0: ");
int year = keyboard.nextInt();

while (year < 0)
{
System.out.print ("Please enter the number of a year that is greater than 0: ");
year = keyboard.nextInt();
}

System.out.println ("\nThe number of days in " + date.getMonth() + ", " + year + " is: ");
}
}

这是我正在努力解决的另一个类中的具体方法和构造函数:


/**
* Write a description of class MonthDays here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MonthDays
{
// instance variables
int month;
int year;
String thisMonth;
public void date() //Default Constructor
{
month = 0;
year = 0;
thisMonth = "";
}


public String getMonth(int month)
{
switch (month)
{
case 1:
thisMonth = ("January");
break;
case 2:
thisMonth = ("February");
break;
case 3:
thisMonth = ("March");
break;
case 4:
thisMonth = ("April");
break;
case 5:
thisMonth = ("May");
break;
case 6:
thisMonth = ("June");
break;
case 7:
thisMonth = ("July");
break;
case 8:
thisMonth = ("August");
break;
case 9:
thisMonth = ("September");
break;
case 10:
thisMonth = ("October");
break;
case 11:
thisMonth = ("November");
break;
case 12:
thisMonth = ("December");
break;
}
return thisMonth;
}
}

最佳答案

第一

public void date() //Default Constructor

错误。构造函数名称必须与类名称匹配,并且 void

public MonthDays() // <-- No-arg constructor, if you provide any constructor
// there is no default constructor.

其次,您应该将确定月份名称的逻辑与设置值分开。我更喜欢一个 static 方法和 Map 。就像,

private static Map<Integer, String> monthMap = new HashMap<>();
static {
String[] names = { "January", "February", "March", "April", //
"June", "July", "August", "September", "October", //
"November", "December" };
for (int i = 0; i < names.length; i++) {
monthMap.put(i + 1, names[i]);
}
}

// Could be public, just a utility method
private static String getMonthName(int month) {
return monthMap.getOrDefault(month, "Unknown");
}

然后,使用类似的东西

public String getMonth() {
return getMonthName(this.month);
}

关于java - 在Java中发现驱动类中没有参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60426519/

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