【发布时间】:2025-11-24 07:10:01
【问题描述】:
好的,我对 java 和编码很陌生,我有这个任务,我必须接受用户输入,这将是月、日和年,然后将月乘以日并将其与一年中的最后两位数字来检查它是否是一个神奇的日子。如果用户只使用数字作为输入,我完成了这个,我想尝试做另一个程序,用户输入月份(例如四月(不是 4)),我想看看程序是否可以检查枚举,检查 April 及其值,并将其值分配给 String 月份,然后我将其转换为 int。对不起,如果我的解释很混乱,但我已尽我所能解释了,如果您对某些事情感到困惑,请随时询问。
到目前为止,这是我的代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package magicadvanced;
import java.util.Scanner;
/**
*
* @author yfernandez
*/
public class MagicAdvanced {
public enum Months{
January(1),February(2),March(3),April(4),May(5),June(6),July(7),August(8),September(9),October(10),November(11),December(12);
private int value;
Months(int value){
this.value = value;
}
public int valueInt(){
return value;
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner ls=new Scanner(System.in);
String month,year,yrTwo;
int day,yearInt;
System.out.println("Please enter a month.");
month = ls.next();
System.out.println("Please enter a day.");
day = ls.nextInt();
System.out.println("Please enter the year.");
year = ls.next();
yrTwo = year.substring(2,4); //getting last two characters of the string year
yearInt = Integer.valueOf(yrTwo);// converting last two character of the string year into an integer
System.out.println(yearInt*2);//this is a just a test code to check if my conversion to integer works, will remove when program is done
}
}
【问题讨论】: