我目前正在尝试完成此程序,但是当我测试我的switch语句时,它直接进入我的默认情况并说我输入的信息无效。
我的任务
我必须从用户那里收到一个月并将其发送到我的案例陈述,以便为特定案件执行我的代码。您可能会注意到,每个案例中都有一个密钥,此密钥用于个人目的。请不要理会。 p>
我的问题
case语句直接进入我的default语句,该语句向用户发出无效的信息消息。
我的进步
这将是我的完整程序,除了我的case语句将每个月识别为无效输入外,一切似乎都正常工作
// Import Libraries
import javax.swing.*;
import java.util.*;
import java.io.*;
// This is my program.
public class DateCalc
{
public static void main (String[] args)
{
String month;
String day;
String inputYear;
Scanner keyboard = new Scanner(System.in);
// receiving input for my age variable
System.out.print( "Please Enter The Month Of The Date :");
month = keyboard.nextLine();
// receiving input for my weight variable
System.out.print( "Please Enter The Day Of The Date : ");
day = keyboard.nextLine();
// receiving input for my height variable
System.out.print( "Please Enter The Year OF The Date : ");
inputYear = keyboard.nextLine();
String stringYear = ""+ inputYear.charAt(inputYear.length()-2) + inputYear.charAt(inputYear.length()-1);
int year = Integer.parseInt(stringYear);
int intDay = Integer.parseInt(day);
switch(month)
{
// I tried to test my program by using my first case " January ", However it goes right through every case directly for my default case.
case "January || january" :
int janKey = 1;
int janQuarter = year / 4;
int janSum = year + janQuarter + intDay + janKey;
System.out.print( " Date Entered Was : " + month + ","+ day + "" + inputYear);
System.out.print( " Last Two Digits Of The Year Were : " + year);
System.out.print( " One Quarter Of Last Two Digits : " + janQuarter);
System.out.print( " The Given Day Of The Month Entered : " + day);
System.out.print( " The Index Key This Moth is : " + janKey);
System.out.print( " The Sum Of All The number Above is : " + janSum);
System.out.print( " \n \n The Day Of The Week Was : ");
int weekDay = dayLookUp(janSum);
System.out.print( " \n \n The Day Of The Week Was : " + weekDay);
break;
case "February || february":
int febKey = 4;
break;
case "March || march":
int marKey = 4;
break;
case "April || april":
int aprKey = 0;
break;
case "May || may":
int maykey = 2;
break;
case "June || june":
int junKey = 5;
break;
case "July || july":
int julKey = 0;
break;
case "August || august":
int augKey = 3;
break;
case "September || september":
int septKey = 6;
break;
case "October || october":
int octKey = 1;
break;
case "November || november":
int novKey = 4;
break;
case "December || december":
int decKey = 4;
break;
// IN MY DEFUALT CASE " inputValidation " WILL BE EXECUTED
default:
JOptionPane.showMessageDialog(null," Invalid Entry Please Try Again " );
}
}
public static int dayLookUp ( int janSum )
{
int sum = janSum;
int day = 14 % 7;
return day;
}
}
答案 0 :(得分:6)
现在你正在这样做,它正在寻找整个字符串,从字面上看,不是将||
解释为任何形式的or
。
您可以将开关中的元素设置为大写或小写:
switch(month.toLowerCase()) {
case "january" :
...
break;
case "february":
...
...
}
或者你必须加倍案例元素:
switch (month) {
case "January":
case "january":
...
break;
case "February":
case "february":
...
...
}
答案 1 :(得分:1)
错误发生在"January || january"
这是一个String
使用case "January": case "january":
答案 2 :(得分:1)
您无法以这种方式测试替代品,case "January || january":
无效。您可以使用多个case
s
switch (month) {
case "january":
case "January":
int janKey = 1;
没有干预break
。当输入january
时,这会导致第二种情况下降。当然,其他几个月也一样。