简体   繁体   English

退出Java程序

[英]Exiting a program in Java

I have an assignment to write Health Analyzer Application program in Java. 我分配了一个用Java编写Health Analyzer Application程序的任务。 This is the first step: 这是第一步:

The user has to enter Man (Upper case or lower), or Woman (Upper case or lower) for gender, or Exit (Upper case or lower) for exiting from the program. 用户必须输入“男”(大写或小写或小写)或“女”(大写或小写)作为性别,或输入“退出”(大写或小写)以退出程序。 If the user inputs a word that is not recognized (NOT Man, Woman or Exit) for gender, then the program displays “Wrong Selection of gender! 如果用户输入的性别无法识别的单词(“不,男人,女人或退出”),则程序显示“错误选择性别! ”, and exits. ”,然后退出。

This is what I've been working on. 这就是我一直在努力的。 The only thing I'm missing is that it reads "Exit" as "Wrong selection of gender", when it should say "Thank you for using this Application" 我唯一缺少的是,它应该显示“谢谢您使用此应用程序”时,将“退出”读为“性别选择错误”

System.out.println("------------------------------------------------------------\n" +
                   "        Welcome to Health Analyzer\n" +
                   "------------------------------------------------------------");

// SELECTING GENDER
       Scanner scan = new Scanner(System.in);
System.out.println("\t Select Gender\n" +
"\t Enter \"Man\" to calclate average calorie requirements for man\n" +
"\t Enter \"Woman\" to calclate average calorie requirements for Woman\n" +
"\t Enter \"Exit\" for Exiting the Program\n" +
"Enter the Choice: ");

String gender;  
gender = scan.nextLine().toLowerCase();

String man = "Man"; 
String woman = "Woman"; 
String exit = "Exit"; 

if (! (gender.equals(man)) || (gender.equals(woman))) {
    System.out.println("\t Wrong Selection of gender!");
    System.exit(0); }
else if (gender.equals(exit)) {
    System.out.println("\t\t *** Thank you for using this Application ***");
    System.exit(0);
}

Your logic is a bit reversed here. 您的逻辑在这里有点相反。

As you enter exit - the first if ( 输入出口时-第一个(

if (! (gender.equals(man)) || (gender.equals(woman))) {

) will evalute to true, as it is not woman nor man. )将评估为真,因为它既不是女人也不是男人。

You should probably do it with truthy evaluations. 您可能应该进行真实的评估。

if (man.equals(gender) || woman.equals(gender)) {
doSomeStuffHere
}
else if (exit.equals(gender)) {
exitProgramHere
}
else { 
printWrongGenderHere
}

Also instead of using equals, use equalsIgnoreCase 另外,不要使用equals,而是使用equalsIgnoreCase

Since you are using toLowerCase on input string to convert into lowercase. 由于您在输入字符串上使用toLowerCase转换为小写。 You should compare the string with lowercase only. 您只能将字符串与小写字母进行比较。

gender = scan.nextLine().toLowerCase();
String man = "man"; 
String woman = "woman"; 
String exit = "exit"; 

Otherwise, you could user equalsIgnoreCase for ignoring case-sensitivity. 否则,您可以使用equalsIgnoreCase忽略大小写。

if (gender.equals(exit)) {
    System.out.println("\t\t *** Thank you for using this Application ***");
    System.exit(0);
}
else if (! (gender.equals(man) || gender.equals(woman)) ) {
    System.out.println("\t Wrong Selection of gender!");
    System.exit(0); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM