如果用户输入无效h

时间:2018-05-17 05:55:27

标签: java

我是一名新的程序员,我的班级已完成。我的问题是,如果用户输入无效并且执行了 else 语句,如何在执行else语句 之后生成System.out.println("Your Sign is " + sign);,无需在每个 if / else if 语句中使用 System.out.print

因此,如果用户输入无效,则输出应仅为:"无效输入。请运行程序再试一次。"

import java.util.Scanner;

public class Sign {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int month, day;

        System.out.println("What's your sign?");
        System.out.print("Please enter the month of your birth (1-12): ");
        month = input.nextInt();
        System.out.print("Please enter the day of your birth (1-31): ");
        day = input.nextInt();
        String sign = "";

        if ((month == 3 && (day >= 21 && day <= 31)) || (month == 4 && (day >= 1 && day <= 19))) {
            sign = "Aries";
        }
        else if ((month == 4 && (day >= 20 && day <= 30)) || (month == 5 && (day >= 1 && day <= 20))) {
            sign = "Taurus";
        }
        else if ((month == 5 && (day >= 21 && day <= 31)) || (month == 6 && (day >= 1 && day <= 21))) {
            sign = "Gemini";
        }
        else if ((month == 6 && (day >= 22 && day <= 30)) ||(month == 7 && (day >= 1 && day <= 22))) {
            sign = "Cancer";
        }
        else if ((month == 7 && (day >= 23 && day <= 31)) || (month == 8 && (day >= 1 && day <= 22))) {
            sign = "Leo";
        }
        else if ((month == 8 && (day >= 23 && day <= 31)) || (month == 9 && (day >= 1 && day <= 22))) {
            sign = "Virgo";
        }
        else if ((month == 9 && (day >= 23 && day <= 30)) || (month == 10 && (day >= 1 && day <= 22))) {
            sign = "Libra";
        }
        else if ((month == 10 && (day >= 23 && day <= 31)) || (month == 11 && (day >= 1 && day <= 21))) {
            sign = "Scorpio";
        }
        else if ((month == 11 && (day >= 22 && day <= 30)) || (month == 12 && (day >=1 && day <= 21))) {
            sign = "Sagittarius";
        }
        else if ((month == 12 && (day >= 22 && day <= 31)) || (month == 1 && (day >= 1 && day <= 19))) {
            sign="Capricon";
        }
        else if ((month == 1 && (day >= 20 && day <= 31)) || (month == 2 && (day >= 1 && day <= 18))) {
            sign="Aquarius";
        }
        else if((month == 2 && (day >= 19 && day <= 29)) || (month == 3 && (day >= 1 && day <= 20))) {
            sign="Pieces";
        }
       else {
           System.out.println("Invalid Entry. Please run the program to try again.");
        }

        System.out.println("Your Sign is " + sign);
        input.close();

    }
}

5 个答案:

答案 0 :(得分:5)

您可以删除else子句,并使用sign的值作为有效输入的指示。

String sign = null;
if ((month == 3 && (day >= 21 && day <= 31)) || (month == 4 && (day >= 1 && day <= 19))) {
     sign = "Aries";
} else if ...
    ...
} else if ((month == 2 && (day >= 19 && day <= 29)) || (month == 3 && (day >= 1 && day <= 20))) {
    sign="Pieces";
}

if (sign != null) {
    System.out.println("Your Sign is " + sign);
} else {
    System.out.println("Invalid Entry. Please run the program to try again.");
}

答案 1 :(得分:1)

你必须删除你的else子句,并检查sign的值是否为null或如下所示为空。

import java.util.Scanner;

    public class Sign {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            int month, day;

            System.out.println("What's your sign?");
            System.out.print("Please enter the month of your birth (1-12): ");
            month = input.nextInt();
            System.out.print("Please enter the day of your birth (1-31): ");
            day = input.nextInt();
            String sign = "";

            if ((month == 3 && (day >= 21 && day <= 31)) || (month == 4 && (day >= 1 && day <= 19))) {
                sign = "Aries";
            }
            else if ((month == 4 && (day >= 20 && day <= 30)) || (month == 5 && (day >= 1 && day <= 20))) {
                sign = "Taurus";
            }
            else if ((month == 5 && (day >= 21 && day <= 31)) || (month == 6 && (day >= 1 && day <= 21))) {
                sign = "Gemini";
            }
            else if ((month == 6 && (day >= 22 && day <= 30)) ||(month == 7 && (day >= 1 && day <= 22))) {
                sign = "Cancer";
            }
            else if ((month == 7 && (day >= 23 && day <= 31)) || (month == 8 && (day >= 1 && day <= 22))) {
                sign = "Leo";
            }
            else if ((month == 8 && (day >= 23 && day <= 31)) || (month == 9 && (day >= 1 && day <= 22))) {
                sign = "Virgo";
            }
            else if ((month == 9 && (day >= 23 && day <= 30)) || (month == 10 && (day >= 1 && day <= 22))) {
                sign = "Libra";
            }
            else if ((month == 10 && (day >= 23 && day <= 31)) || (month == 11 && (day >= 1 && day <= 21))) {
                sign = "Scorpio";
            }
            else if ((month == 11 && (day >= 22 && day <= 30)) || (month == 12 && (day >=1 && day <= 21))) {
                sign = "Sagittarius";
            }
            else if ((month == 12 && (day >= 22 && day <= 31)) || (month == 1 && (day >= 1 && day <= 19))) {
                sign="Capricon";
            }
            else if ((month == 1 && (day >= 20 && day <= 31)) || (month == 2 && (day >= 1 && day <= 18))) {
                sign="Aquarius";
            }
            else if((month == 2 && (day >= 19 && day <= 29)) || (month == 3 && (day >= 1 && day <= 20))) {
                sign="Pieces";
            }
            if(!sign.equals("")){
               System.out.println("Your Sign is " + sign);
            }else{
                System.out.println("Invalid Entry. Please run the program to try again.");
            }
            input.close();

        }
    }

答案 2 :(得分:0)

将此代码放在try..catch块中,在执行else阻塞后的SOP时似乎异常。

答案 3 :(得分:0)

使用try catch。 https://beginnersbook.com/2013/04/try-catch-in-java/

将您想要执行的代码放入try中,并使用错误消息捕获异常。

答案 4 :(得分:0)

只需使用变量isEntryValid添加一个支票(我已经标记了更改&#34; //已添加&#34;评论)。

希望你的问题得到解决:

import java.util.Scanner;

public class Sign {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int month, day, isEntryValid = 1 //Added;

    System.out.println("What's your sign?");
    System.out.print("Please enter the month of your birth (1-12): ");
    month = input.nextInt();
    System.out.print("Please enter the day of your birth (1-31): ");
    day = input.nextInt();
    String sign = "";

    if ((month == 3 && (day >= 21 && day <= 31)) || (month == 4 && (day >= 1 && day <= 19))) {
        sign = "Aries";
    }
    else if ((month == 4 && (day >= 20 && day <= 30)) || (month == 5 && (day >= 1 && day <= 20))) {
        sign = "Taurus";
    }
    else if ((month == 5 && (day >= 21 && day <= 31)) || (month == 6 && (day >= 1 && day <= 21))) {
        sign = "Gemini";
    }
    else if ((month == 6 && (day >= 22 && day <= 30)) ||(month == 7 && (day >= 1 && day <= 22))) {
        sign = "Cancer";
    }
    else if ((month == 7 && (day >= 23 && day <= 31)) || (month == 8 && (day >= 1 && day <= 22))) {
        sign = "Leo";
    }
    else if ((month == 8 && (day >= 23 && day <= 31)) || (month == 9 && (day >= 1 && day <= 22))) {
        sign = "Virgo";
    }
    else if ((month == 9 && (day >= 23 && day <= 30)) || (month == 10 && (day >= 1 && day <= 22))) {
        sign = "Libra";
    }
    else if ((month == 10 && (day >= 23 && day <= 31)) || (month == 11 && (day >= 1 && day <= 21))) {
        sign = "Scorpio";
    }
    else if ((month == 11 && (day >= 22 && day <= 30)) || (month == 12 && (day >=1 && day <= 21))) {
        sign = "Sagittarius";
    }
    else if ((month == 12 && (day >= 22 && day <= 31)) || (month == 1 && (day >= 1 && day <= 19))) {
        sign="Capricon";
    }
    else if ((month == 1 && (day >= 20 && day <= 31)) || (month == 2 && (day >= 1 && day <= 18))) {
        sign="Aquarius";
    }
    else if((month == 2 && (day >= 19 && day <= 29)) || (month == 3 && (day >= 1 && day <= 20))) {
        sign="Pieces";
    }
   else {

       System.out.println("Invalid Entry. Please run the program to try again.");
       isEntryValid = 0; //Added

        }

       if(isEntryValid ){ //Added

         System.out.println("Your Sign is " + sign);

       }//Added

    input.close();