我的教授已经给我一个任务来创建一个带有多个构造函数的Monthnum类来获取所有参数,因为我们只是学习面向对象编程。我需要创建一个新的构造函数,它接受用户输入作为int值和另一个构造函数,它接受它作为一年中的一个月的字符串值。例如:1 = 1月和1月= 1.我知道我可以在我的主方法中创建扫描仪,但我不确定如何接受并打印出这个数字。正确的直接步骤非常有用!
import java.util.Scanner;
public class learningObjectsAndClasses {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int monthNumber = input.nextInt();
String monthName = input.nextLine();
Monthnum inputMonthNumber = new Monthnum(monthNumber);
Monthnum inputMonthName = new Monthnum(monthName);
System.out.println("Please enter the month name or number: "
+ inputMonthNumber);
}
}
class Monthnum{
int Monthnum;
String monthName;
Monthnum(){
Monthnum = 1;
}
Monthnum(int whichMonth){
Monthnum = whichMonth;
if (whichMonth == 1){
System.out.println("January");
}
else if (whichMonth == 2){
System.out.println("February");
}
else if (whichMonth == 3){
System.out.println("March");
}
else if (whichMonth == 4){
System.out.println("April");
}
else if (whichMonth == 5){
System.out.println("May");
}
else if (whichMonth == 6){
System.out.println("June");
}
else if (whichMonth == 7){
System.out.println("July");
}
else if (whichMonth == 8){
System.out.println("August");
}
else if (whichMonth == 9){
System.out.println("September");
}
else if (whichMonth == 10){
System.out.println("October");
}
else if (whichMonth == 11){
System.out.println("November");
}
else if (whichMonth == 12){
System.out.println("December");
}
else
System.out.println("Invalid input");
}
Monthnum(String whichMonth){
if (whichMonth == "January"){
Monthnum = 1;
}
else if (whichMonth == "February"){
Monthnum = 2;
}
else if (whichMonth == "March"){
Monthnum = 3;
}
else if (whichMonth == "April"){
Monthnum = 3;
}
else if (whichMonth == "May"){
Monthnum = 4;
}
else if (whichMonth == "June"){
Monthnum = 5;
}
else if (whichMonth == "July"){
Monthnum = 6;
}
else if (whichMonth == "August"){
Monthnum = 7;
}
else if (whichMonth == "September"){
Monthnum = 8;
}
else if (whichMonth == "October"){
Monthnum = 9;
}
else if (whichMonth == "November"){
Monthnum = 10;
}
else if (whichMonth == "December"){
Monthnum = 11;
}
else if (whichMonth == "March"){
Monthnum = 12;
}
else
System.out.println("Invalid input");
}
}
答案 0 :(得分:1)
因此,在您设置扫描仪和所有内容之后。您想要读取用户输入的值。您可以使用Scanner's .nextInt()
方法阅读该内容。
Scanner input = new Scanner(System.in);
int num;
num = input.nextInt();
然后在读取它并将其保存在变量num中之后,您可以使用从用户获得的给定参数来实例化Monthnum的对象。
Monthnum m = new Monthnum(num);
如果你想从用户那里读取字符串,你可以像这样使用Scanner's nextLine()
方法:
Scanner input = new Scanner(System.in); //Same scanner from above
//no need to initialize it again
String s;
s = input.nextLine();
现在您将使用String
参数声明并初始化Monthnum的对象(您尚未制作)
Monthnum p = new Monthnum(s);
现在这里是制作该构造函数的提示。如果你已有的话,你可以使用它们。只需将它们更改为:
if (whichMonth.equals("January"){
System.out.println("a");
}
....
如果你不明白,请问我问题!感谢
编辑:所以现在你的代码应该是这样的:
package tst;
import java.util.Scanner;
public class learningObjectsAndClasses {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.println("Enter Month Name ");
String monthName = input.next();
Monthnum inputMonthNumber = new Monthnum(monthName);
System.out.println("Enter Number ");
int monthNumber = input.nextInt();
Monthnum inputMonthName = new Monthnum(monthNumber);
}
public static class Monthnum{
public int Monthnum;
public String monthName;
public Monthnum(){
Monthnum = 1;
monthName = "January";
}
public Monthnum(int whichMonth){
Monthnum = whichMonth;
if (whichMonth == 1){
monthName = "January";
System.out.println("January");
}
else if (whichMonth == 2){
monthName = "February";
System.out.println("February");
}
else if (whichMonth == 3){
monthName = "March";
System.out.println("March");
}
else if (whichMonth == 4){
monthName = "April";
System.out.println("April");
}
else if (whichMonth == 5){
monthName = "May";
System.out.println("May");
}
else if (whichMonth == 6){
monthName = "June";
System.out.println("June");
}
else if (whichMonth == 7){
monthName = "July";
System.out.println("July");
}
else if (whichMonth == 8){
monthName = "August";
System.out.println("August");
}
else if (whichMonth == 9){
monthName = "September";
System.out.println("September");
}
else if (whichMonth == 10){
monthName = "October";
System.out.println("October");
}
else if (whichMonth == 11){
monthName = "November";
System.out.println("November");
}
else if (whichMonth == 12){
monthName = "December";
System.out.println("December");
}
else {
System.out.println("Invalid input from int cons");
}
} //end of cons
public Monthnum(String whichMonth){
if (whichMonth.equals("January")){
Monthnum = 1;
System.out.println(Monthnum);
}
else if (whichMonth.equals("February")){
Monthnum = 2;
System.out.println(Monthnum);
}
else if (whichMonth.equals("March")){
Monthnum = 3;
System.out.println(Monthnum);
}
else if (whichMonth.equals("April")){
Monthnum = 4;
System.out.println(Monthnum);
}
else if (whichMonth.equals("May")){
Monthnum = 5;
System.out.println(Monthnum);
}
else if (whichMonth.equals("June")){
Monthnum = 6;
System.out.println(Monthnum);
}
else if (whichMonth.equals("July")){
Monthnum = 7;
System.out.println(Monthnum);
}
else if (whichMonth.equals("August")){
Monthnum = 8;
System.out.println(Monthnum);
}
else if (whichMonth.equals("September")){
Monthnum = 9;
System.out.println(Monthnum);
}
else if (whichMonth.equals("October")){
Monthnum = 10;
System.out.println(Monthnum);
}
else if (whichMonth.equals("November")){
Monthnum = 11;
System.out.println(Monthnum);
}
else if (whichMonth.equals("December")){
Monthnum = 12;
System.out.println(Monthnum );
}
else
System.out.println("Invalid input");
}
}
}