Java 我的Switch语句未按预期工作
我目前正试图完成这个程序,但是当我测试我的switch语句时,它直接进入我的默认情况,并说我输入了无效信息 我的任务 我必须从用户那里收到一个月的消息,并将其发送到我的case语句中,以便执行特定案例的代码。您可能会注意到,每个案例中都有一把钥匙,这把钥匙是用于个人目的的。请不要理会 我的问题 case语句直接进入我的默认语句,它向用户发出无效的信息消息 我的进步 这将是我的完整程序,除了我的case语句将每个月都识别为无效输入之外,其他一切似乎都正常工作Java 我的Switch语句未按预期工作,java,switch-statement,Java,Switch Statement,我目前正试图完成这个程序,但是当我测试我的switch语句时,它直接进入我的默认情况,并说我输入了无效信息 我的任务 我必须从用户那里收到一个月的消息,并将其发送到我的case语句中,以便执行特定案例的代码。您可能会注意到,每个案例中都有一把钥匙,这把钥匙是用于个人目的的。请不要理会 我的问题 case语句直接进入我的默认语句,它向用户发出无效的信息消息 我的进步 这将是我的完整程序,除了我的case语句将每个月都识别为无效输入之外,其他一切似乎都正常工作 // Import Libraries
// 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;
}
}
你现在这样做,它是在寻找整个字符串,而不是把| |解释为任何形式的or 您可以将开关中的元素设置为大写或小写使用:
switch(month.toLowerCase()) {
case "january" :
...
break;
case "february":
...
...
}
或者您必须将大小写元素加倍:
switch (month) {
case "January":
case "january":
...
break;
case "February":
case "february":
...
...
}
错误是在一月| |一月这是一个字符串用例一月:一月:你不能用这种方式测试替代方案,一月| |一月:不起作用。您可以提供多个案例的备选方案
没有中间的休息。这导致进入1月份时,第二种情况出现下降。当然,其他月份也是如此。case-January:case-January:你希望你的字符串中有一个| |如何神奇地工作?你只是有一个奇怪的字符串文字。是的,你是对的,我想我试着把每一个案例都变成一个条件语句……我真是太傻了
switch (month) {
case "january":
case "January":
int janKey = 1;