gpt4 book ai didi

java - 如何获取用户输入的年份的最后两位数

转载 作者:行者123 更新时间:2023-12-02 05:52:14 24 4
gpt4 key购买 nike

我已经玩这个有一段时间了,我似乎不知道如何完成这项任务。

我的任务:

我需要获取用户输入的年份的最后两位数字。

示例:

用户输入2014年7月2日;我需要获取 2014 年的最后两位数字“14”并将其除以 4。这将得到 3.5;但是我会忽略“.5”并保留 3。

研究:

我一直在阅读我的教科书,并看到了一种我可以使用的方法,其中包括字符串生成器类。然而,我的书有一个非常简短的描述,并且没有显示我可以建设性地用来完成我的任务的示例。

进展:

这就是我到目前为止所拥有的,它基本上是我想要的程序的模板;我只需要一些帮助来获取一年的最后 2 位数字。

public class DateCalc

{
public static void main (String[] args)
{
String month;
int day;
int year;
Scanner keyboard = new Scanner(System.in);

// receiving input
System.out.print( "Please Enter The Month Of The Date : ");
month = keyboard.nextLine();
// receiving input
System.out.print( "Please Enter The Day Of The Date : ");
day = keyboard.nextInt();
// receiving input
System.out.print( "Please Enter The Year OF The Date : ");
year = keyboard.nextInt();

switch(month)
{

// keys are numbered indexes that I need for this, please disregard

case "January" :
int janKey = 1;
break;


case "February":
int febKey = 4;
break;


case "March":
int marKey = 4;
break;


case "April":
int aprKey = 0;
break;

case "May":
int maykey = 2;
break;

case "June":
int junKey = 5;
break;

case "July":
int julKey = 0;
break;

case "August":
int augKey = 3;
break;

case "September":
int septKey = 6;
break;

case "October":
int octKey = 1;
break;

case "November":
int novKey = 4;
break;

case "December":
int decKey = 4;
break;

// IN MY DEFUALT CASE " inputValidation " WILL BE EXECUTED
default:
JOptionPane.showMessageDialog(null," Invalid Entry Please Try Again " );

}
}
}

最佳答案

您可以执行类似 StringyearString = Integer.toString(year).substring(2) 的操作,它首先将整数转换为字符串,然后获取由第二个之后的所有内容组成的子字符串特点。然后要将其转回整数,请尝试 intyearInt = Integer.parseInt(yearString)

关于java - 如何获取用户输入的年份的最后两位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23440244/

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