我试图将 int 的用户输入与 String 数组进行比较。import java.util.Arrays;import java.util.Scanner;public class Array{ public static void main(String[]args) { Scanner input = new Scanner(System.in); int number; String [] month = new String[13]; month[0] = "Null"; month[1] = "January"; month[2] = "February"; month[3] = "March"; month[4] = "April"; month[5] = "May"; month[6] = "June"; month[7] = "July"; month[8] = "August"; month[9] = "September"; month[10] = "October"; month[11] = "November"; month[12] = "December"; System.out.println("Enter a number"); number = input.nextInt(); }}
2 回答
qq_笑_17
TA贡献1818条经验 获得超7个赞
只需使用 input 作为索引,假设他们输入 1 为一月,那么您可以调用索引:
System.out.println("You've selected the month of: " + month[number]);
将为 输出“一月”,当然month[number]
,您还必须检查是否超出范围。
一只斗牛犬
TA贡献1784条经验 获得超2个赞
试试这个
System.out.println("Please enter a number between 0 and 13");
Integer userNmber = input.nextInt();
if(userNmber > month.length-1){
System.out.println("Please enter a number between 0 and 13");
userNmber = input.nextInt();
}
if(month[userNmber] != null)
System.out.println("Input from user " + userNmber + " match with month : "+ month[userNmber]);
答:请输入一个 0 到 13 之间的数字
13
请输入 0 到 13 之间的数字
12
来自用户 12 的输入与月份匹配:十二月
添加回答
举报
0/150
提交
取消