Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否有其他方式获得输入,如日期格式?_Java_Switch Statement_Try Catch - Fatal编程技术网

Java 是否有其他方式获得输入,如日期格式?

Java 是否有其他方式获得输入,如日期格式?,java,switch-statement,try-catch,Java,Switch Statement,Try Catch,我希望并寻找一些其他的想法或方法来做到这一点。如您所见,我要求用户输入mmddyyyy(05022001=2001年5月2日),并使用substring以获取索引中的特定输入,然后对其进行解析。现在,我想完成的是找到另一种方法,例如使用正式的日期格式化程序,在我的例子中,我没有使用。我认为date formatter比这个好得多,但我并没有从中得到灵感 这是我到目前为止得到的。提前谢谢 import java.util.*; public class ZodiaChineseBirthSign

我希望并寻找一些其他的想法或方法来做到这一点。如您所见,我要求用户输入mmddyyyy(05022001=2001年5月2日),并使用substring以获取索引中的特定输入,然后对其进行解析。现在,我想完成的是找到另一种方法,例如使用正式的日期格式化程序,在我的例子中,我没有使用。我认为date formatter比这个好得多,但我并没有从中得到灵感

这是我到目前为止得到的。提前谢谢

import java.util.*;

public class ZodiaChineseBirthSign {
    public static void main(String [] args) {
       Scanner reader = new Scanner(System.in);
       String mm,dd,yyyy;
       int mm1,dd1,yyyy1;
       
       /**
        * To keep asking for the input from the user I have to use looping
        * which is the while loop.
        * 
        */
       while(true) {
       System.out.print("Enter your birthdate: ");
       String birth = reader.next();
       
       //Substring is to get the specific input of the user.
       mm = birth.substring(0, 2);
       dd = birth.substring(2, 4);
       yyyy = birth.substring(4);
        
       //ParseInt which is used to convert String into Integer in order for me to be able to determine the Animal Sign and Zodiac.
       mm1 = Integer.parseInt(mm);
       dd1 = Integer.parseInt(dd);
       yyyy1 = Integer.parseInt(yyyy);
       
       try {
           if (mm1 < 1 || mm1 > 12 || dd1 < 1 || dd1 > 31) {
               throw new ArrayIndexOutOfBoundsException();
           }
       
       
       switch (mm1) {
            case 1: System.out.println("You were born on January " + dd1 + ", " + yyyy1 ); break;
            case 2: System.out.println("You were born on February " + dd1 + ", " + yyyy1 ); break;
            case 3: System.out.println("You were born on March " + dd1 + ", " + yyyy1 ); break;
            case 4: System.out.println("You were born on April " + dd1 + ", " + yyyy1 ); break;
            case 5: System.out.println("You were born on May " + dd1 + ", " + yyyy1 ); break;
            case 6: System.out.println("You were born on June " + dd1 + ", " + yyyy1 ); break;
            case 7: System.out.println("You were born on July " + dd1 + ", " + yyyy1 ); break;
            case 8: System.out.println("You were born on August " + dd1 + ", " + yyyy1 ); break;
            case 9: System.out.println("You were born on September " + dd1 + ", " + yyyy1 ); break;
            case 10: System.out.println("You were born on October " + dd1 + ", " + yyyy1 ); break;
            case 11: System.out.println("You were born on November " + dd1 + ", " + yyyy1 ); break;
            case 12: System.out.println("You were born on December " + dd1 + ", " + yyyy1 ); break;
       }
       
       switch (yyyy1 % 12) {
            case 0: System.out.println("in the Year of the Monkey"); break;
            case 1: System.out.println("in the Year of the Rooster"); break;
            case 2: System.out.println("in the Year of the Dog"); break;
            case 3: System.out.println("in the Year of the Pig"); break;
            case 4: System.out.println("in the Year of the Rat"); break;
            case 5: System.out.println("in the Year of the Ox"); break;
            case 6: System.out.println("in the Year of the Tiger"); break;
            case 7: System.out.println("in the Year of the Rabbit"); break;
            case 8: System.out.println("in the Year of the Dragon"); break;
            case 9: System.out.println("in the Year of the Snake"); break;
            case 10: System.out.println("in the Year of the Horse"); break;
            case 11: System.out.println("in the Year of the Sheep"); break;
       }
       
       if ((dd1>21 && mm1 == 12) || (dd1<=19 && mm1== 1)){
           System.out.println("under the sign of Capricorn");
       }
       if ((dd1>20 && mm1 ==1) || (dd1<=18 && mm1 == 2)){
           System.out.println("under the sign of Aquarius");
       }
       if ((dd1>19 && mm1 == 2) || (dd1 <=20 && mm1 == 3)){
           System.out.println("under the sign of Pisces");
       }
       if ((dd1>21 && mm1 == 3) || (dd1 <=20 && mm1 == 4)){
           System.out.println("under the sign of Aries");
       }
       if ((dd1>21 && mm1 == 4) || (dd1 <=20 && mm1 == 5)){
           System.out.println("under the sign of Taurus");
       }
       if ((dd1>21 && mm1 == 5) || (dd1<=20 && mm1 == 6)){
           System.out.println("under the sign of Gemini");
       }
       if ((dd1>21 && mm1 == 6) || (dd1<=20 && mm1 == 7)){
           System.out.println("under the sign of Cancer");
       }
       if ((dd1>21 && mm1  == 7) || (dd1<=20 && mm1== 8)){
           System.out.println("under the sign of Leo");
       }
       if ((dd1>21 && mm1 ==8) || (dd1<=22 && mm1 == 9)){
           System.out.println("under the sign of Virgo");
       }
       if ((dd1>23 && mm1 == 9) || (dd1<=20 && mm1 ==10)){
           System.out.println("under the sign of Libra");
       }
       if ((dd1>21 && mm1 == 10) || (dd1<=22 && mm1 == 11)){
           System.out.println("under the sign of Scorpio");
       }
       if ((dd1>23 && mm1 == 11) || (dd1<=20 && mm1 == 12)){
           System.out.println("under the sign of Sagittarius");
       }
       }
       catch (ArrayIndexOutOfBoundsException e) { //Catch-Block which will print(enter correct birthdate) whenever it throws exception error.
           System.out.println("Please enter the correct Birthdate");
       } 
       System.out.println("");
       }
       
       /**
        * Thanks to some of my classmate for giving me some snippets of codes and ideas ^_^
        */
    }  
}
import java.util.*;
公共级十二生肖{
公共静态void main(字符串[]args){
扫描仪阅读器=新扫描仪(System.in);
字符串mm,dd,yyyy;
int mm1,dd1,yyyy1;
/**
*为了不断请求用户的输入,我必须使用循环
*这是while循环。
* 
*/
while(true){
System.out.print(“输入您的生日:”);
字符串birth=reader.next();
//子字符串用于获取用户的特定输入。
mm=出生子串(0,2);
dd=出生子串(2,4);
yyyy=出生子串(4);
//ParseInt,用于将字符串转换为整数,以便我能够确定动物星座和生肖。
mm1=整数.parseInt(mm);
dd1=整数.parseInt(dd);
yyyy1=整数.parseInt(yyyy);
试一试{
如果(mm1<1 | | mm1>12 | | dd1<1 | | dd1>31){
将新的ArrayIndexOutOfBoundsException()抛出;
}
开关(mm1){
案例1:System.out.println(“您出生于1月份”+dd1+,“+yyyy1”);break;
案例2:System.out.println(“您出生于二月”+dd1+,“+yyyy1”);break;
案例3:System.out.println(“您出生于3月份”+dd1+,“+yyyy1”);break;
案例4:System.out.println(“您出生于四月”+dd1+,“+yyyy1”);break;
案例5:System.out.println(“您出生于五月”+dd1+,“+yyyy1”);break;
案例6:System.out.println(“您出生于六月”+dd1+,“+yyyy1”);break;
案例7:System.out.println(“您出生于7月份”+dd1+,“+yyyy1”);break;
案例8:System.out.println(“您出生于八月”+dd1+,“+yyyy1”);break;
案例9:System.out.println(“您出生于九月”+dd1+,“+yyyy1”);break;
案例10:System.out.println(“您出生于10月”+dd1+,“+yyyy1”);break;
案例11:System.out.println(“您出生于11月”+dd1+,“+yyyy1”);break;
案例12:System.out.println(“您出生于12月”+dd1+,“+yyyy1”);break;
}
交换机(yyyy1%12){
案例0:System.out.println(“在猴年”);中断;
案例1:System.out.println(“鸡年”);中断;
案例2:System.out.println(“在狗年”);中断;
案例3:System.out.println(“在猪年”);中断;
案例4:System.out.println(“鼠年”);中断;
案例5:System.out.println(“在牛年”);中断;
案例6:System.out.println(“在虎年”);中断;
案例7:System.out.println(“在兔年”);中断;
案例8:System.out.println(“在龙年”);中断;
案例9:System.out.println(“蛇年”);中断;
案例10:System.out.println(“在马年”);中断;
案例11:System.out.println(“羊年”);中断;
}

如果(dd1>21和和mm1=21以及以及以及mm1=1=12)和(dd1>21和21以及以及以及以及以及以及以及mm1=1=2)如果(dd1>21和和和mm1=21以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及mm1=1=1=1=1=1)12)124\124;(dd119和(dd119和以及以及以及以及以及以及以及以及以及以及以及以及以及mm1=1=1=2=1=1=2)2)2)2)2)124\124;124;(dd1和(dd1和(dd1以及以及以及以及以及以及以及以及以及以及(dd1以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及以及1=1=1=1=1=1=1=1=1=1=1=2=2=2=| |(dd123&&mm1==11)|(dd1您应该告知用户您希望使用的格式

获取输入字符串并进行解析尝试。在输入错误的情况下捕获异常。无需检查月和日的范围。
LocalDate.parse
为您进行这些数据输入验证检查

DateTimeFormatter f=DateTimeFormatter.of(“mmdduuu”);
尝试
{
LocalDate ld=LocalDate.parse(输入);
} 
捕获(DateTimeParse异常)
{
…处理错误输入
}
不需要打开月份。使用
month#getDisplayName
生成月份的本地化名称

String monthName=ld.getMonth().getLocalizedDisplayName(TextStyle.FULL\u STANDALONE,Locale.CANADA\u FRENCH);

所有这些都已经在堆栈溢出上讨论过很多次了。搜索以了解更多信息。

我如何使用DateFormatter?它应该是这样的吗?
System.out.print(“输入生日(mmddyyyy):”);
f=reader.next();
?然后在我的try块上是LocalDate ld=LocalDate.parse(f)?但是我如何才能接受特定的输入,例如月份和年份,这将是能够确定符号的因素。@1337正如我所说的,您正在从控制台获取一个输入字符串。您的工作是解析该字符串输入。我明白了。我会尝试的out@1337你必须学会阅读文档。我给了你关键:使用
LocalDate
class。我甚至给你展示了
getMonth
方法,所以你可能会认为也有一个
getYear
方法。你的第一反应应该是打开Javadoc让那门课学习它的方法,而不是寻求更多的帮助。我们不是来为你做作业的。