Eri*_*ick 0 java switch-statement
我目前正在尝试完成此程序,但是当我测试我的switch语句时,它直接进入我的默认情况并说我输入了无效信息.
我的任务
我必须收到用户一个月的时间并将其发送到我的case语句,以便为特定情况执行我的代码.您可能会注意到,每个案例中都有一个密钥,此密钥用于个人目的.请忽略.
我的问题
case语句直接进入我的默认语句,该语句向用户发出无效的信息消息.
我的进步
这将是我的完整程序,除了我的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;
}
}
Run Code Online (Sandbox Code Playgroud)
你现在正在这样做,它正在寻找整个字符串,从字面上看,不解释||任何形式的字符串or.
您可以将开关中的元素设置为大写或小写:
switch(month.toLowerCase()) {
case "january" :
...
break;
case "february":
...
...
}
Run Code Online (Sandbox Code Playgroud)
或者你必须加倍案例元素:
switch (month) {
case "January":
case "january":
...
break;
case "February":
case "february":
...
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
709 次 |
| 最近记录: |