2016-11-27 159 views
2

我是Java初学者,我正在学习if语句没有else,请问社区检查我的代码是否改进?使用if语句

问题陈述: 在这段代码中,我们只使用if语句来练习。在这里你需要输入你的年龄才能进入酒吧与热辣女孩,如果你的年龄在18岁以上,你可以进入。 如果它低于18你不能进入。

import java.util.Scanner; 

public class Main { 
    public static void main(String[] args){ 

     Scanner input = new Scanner(System.in); 

     int age; 

     // Ask the age 
     System.out.println("Please write your age to enter to the bar:"); 
     age = input.nextInt(); 

     // Equal to 
     if(age == 18){ 
      System.out.println("Your age " + age + " it's equal to 18, you can enter"); 
     } 

     // Not equal to 
     if(age != 18){ 
      System.out.println("Your age " + age + " it's not equal to 18"); 
     } 

     // Greater than 
     if(age > 18){ 
      System.out.println("Your age " + age + " " + "is greater than 18, you can enter"); 
     } 

     // Less than 
     if(age < 18){ 
      System.out.println("Your age " + age + " " + "is less than 18, you can't enter"); 
     } 

     // Greater than or equal to 
     if(age >= 18){ 
      System.out.println("Your age " + age + " is greater than or equal to 18, you can enter"); 
     } 
     // Less than or equal to 
     if(age <= 18){ 
      System.out.println("Your age " + age + " is less than or equal to 18, you can't enter"); 
     } 
    } 
} 
+0

有什么好检查的时候,它只是一个与不同的运营商他们的名单?这当然不是这样做的最好方式,但通过说*“没有别的”*你似乎无论如何都知道。 –

+0

看起来不错。有一点,最后两个陈述是无用的,因为前四个陈述耗尽了所有可能性,所以它永远不会得到它们。为了将来的参考,我相信你会在学习的时候加以说明,一个switch语句对于这些问题是完美的。学习一件有趣的事情! –

+0

@AyoubFalah我没有downvote,但它是一个低质量的问题,因为它询问'if'的用法,没有指出他们的实际问题是什么,所以我可以看到为什么两个人做了。 –

回答

0

你的代码是,除了最后的精if语句应该比不仅仅是少,不等于以下,所有的如果最后两个前语句是多余的,所以你可以将它们删除。你只需要考虑两种可能性:18岁以上的人,或者18岁以下的人。

0

你的代码将会'工作',因为它会打印每个年龄输入的注释。但是,因为您不使用其他或嵌套,它总是会产生多个注释。例如:如果输入17,则将触发对于<> 18,< 18和< = 18的评论。对于18的特定输入,其中一个打印的评论将与其他评论相冲突,因为= 18和> = 18条评论会让你进入,而< = 18条评论会拒绝你的输入。

0

如果您只需要使用'if',它在代码中的使用是正确的,但是您的条件正在重复。 提示:如果一个人的年龄应该是20,那么您的代码的第二,第三和第五个条件将为真,并且所有这些都将输出。正如你在问题中所说的,你需要检查年龄是大于还是小于18,并且如果你想检查它是否等于18,那么从所有这些条件中,你只需要两个

1

从技术上讲,你刚才案件:

  • age < 18
  • age == 18
  • age > 18

所以,你可以简化你的代码

... 
if (age < 18) { 
    System.out.println("Your age " + age + " it's not equal to 18"); 
    System.out.println("Your age " + age + " is less than 18, you can't enter"); 
    System.out.println("Your age " + age + " is less than or equal to 18, you can't enter"); 
} 

// "else if" instead of "if" will make the code more readable 
if (age == 18) { 
    System.out.println("Your age " + age + " it's equal to 18, you can enter"); 
    System.out.println("Your age " + age + " is greater than or equal to 18, you can enter"); 
    System.out.println("Your age " + age + " is less than or equal to 18, you can't enter"); 
} 

// "else" instead of "if" will make the code more readable 
if (age > 18) { 
    System.out.println("Your age " + age + " it's not equal to 18"); 
    System.out.println("Your age " + age + " is greater than 18, you can enter"); 
    System.out.println("Your age " + age + " is greater than or equal to 18, you can enter"); 
} 

如果你想确保age >= 18

if (age >= 18) { 
    System.out.println("Your age " + age + " is greater than or equal to 18, you can enter"); 
} 

// "else" is a better choice here 
if (age < 18) { 
    System.out.println("Your age " + age + " is less than 18, you can't enter"); 
} 
+0

他提到不需要使用'其他' – user5434084

+0

@ user5434084:我明白了,谢谢!我已经编辑了这个问题,并且把*没有*'else'以突出显示这个不寻常的情况。 –