【发布时间】:2018-04-21 00:59:41
【问题描述】:
String[] months={"A","B","C","D","E","F","G","H","I","J","K","L"};
int valueMonth;
Scanner yasin=new Scanner(System.in);
System.out.println("Please input your month number: ");
valueMonth=(yasin.nextInt());
switch (valueMonth) {
case 1: months[valueMonth];
break;
}
我在开关盒和字符串数组方面出了点问题。如果不使用if-else.. 我怎么能解决这个问题。谢谢你..
ps:如果我在网站上犯了语法错误,那就不是了。
我就这样解决了这个练习;
String[] months={"a", "b" ,"c","d","e","f","g","h","i","j","k","l"};
int valueMonth;
Scanner tara=new Scanner(System.in);
System.out.println("Input Number : ");
valueMonth=tara.nextInt();
if(valueMonth>0 && valueMonth<=12){
System.out.println(months[ valueMonth- 1]);
}else{
System.err.println("ERROR");
}
并且我想进行更多锻炼并尝试使用Switch-Case 语句来做。但它不起作用。也许我错过了什么或者它根本不工作 – Yasin Atagün 7 分钟前
【问题讨论】:
-
你想用这个做什么?你得到什么错误? case 1,意味着它将执行冒号和中断之间的所有内容;如果月份 == 1。但看起来月份是一个数组......也许你的意思是把 switch(valueMonths).这是一个 int,比如 1。
-
我的练习是从用户那里获得价值,并且通过这个输入月份在屏幕上。就像他说的4,程序会在屏幕上输入April。我只能使用 switch case 语句来做到这一点。但我在 java 的数组主题上。老师用if else做到了。我很轻松地做到了。但是如果可能的话,现在我试着用 switch case 来做。
标签: java arrays string switch-statement