java将数字输入转换为一年
我的目标是通过输入一个整数来询问用户年份。例如,如果用户输入“05”或“87”或“2017”,则输出将为“2005”或“2087”或仅为“2017”。该程序只意味着在输入中占2和4个空格。例如,如果用户输入“123”,则输出为“123是无效年份”或“12345”,输出为“12345不是年份”。我在代码中遇到问题,不知道如何更正它
import java.util.Scanner;
public class Y2k
{
public static void main( String [ ] args )
{
Scanner scan = new Scanner ( System.in );
// establish string and number to be apply to input
String year;
int sand = 20;
// ask for the year
System.out.println( "Enter a year: " );
year = scan.next( );
char ohTwo = year.charAt( 1 );
// perform the year and print the result
switch ( year )
{
case "05":
System.out.println( sand + year ); //output year and input
break;
}
}
}
对于这件事上的任何帮助,提前谢谢你
# 1 楼答案
要使用switch语句实际实现这一点,请执行以下操作:
# 2 楼答案
如果要使用switch,请执行以下操作,而不是if-else语句:
# 3 楼答案
这是使用“if”语句实现的程序:
此解决方案使用“if”语句。您可以将此作为使用case方法创建自己的代码的指南