使用if语句

时间:2016-11-27 21:18:55

标签: java if-statement

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

问题陈述: 在这段代码中,我们只使用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");
        }
    }
}

5 个答案:

答案 0 :(得分: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");  
} 

答案 1 :(得分:0)

你的代码很好,除了最后一个if语句应该小于,不等于或小于,并且最后两个之前的所有if语句都是无关的,所以你可以删除它们。你只需要考虑两种可能性:这个人是18岁以上,或者是18岁以下的人。

答案 2 :(得分:0)

您的代码将“正常工作”,因为它将为每个年龄输入打印注释。但是因为你不使用else或嵌套,它总会产生多个注释。例如:如果输入17,则它将触发对&lt;&gt; 18的评论,&lt;对于18的特定输入,其中一个打印的注释将与其他注释冲突,因为= 18和&gt; = 18注释将允许您进入,而&lt; = 18注释会拒绝你入境。

答案 3 :(得分:0)

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

答案 4 :(得分:0)

这里有一个方法,用于计算用户是否成人尊重now()

public Boolean isMaggiorenne(Date dataGenerica) {
Boolean isMagg = false;
if (dataGenerica != null && dataNascita != null) {

GregorianCalendar dataDiNascita = new GregorianCalendar();
dataDiNascita.setTime(dataNascita);

GregorianCalendar dataMagg = new GregorianCalendar();
dataMagg.setTime(dataGenerica);

int anno = dataMagg.get(GregorianCalendar.YEAR) – dataDiNascita.get(GregorianCalendar.YEAR);
int mese = dataMagg.get(GregorianCalendar.MONTH) – dataDiNascita.get(GregorianCalendar.MONTH);
int giorno = dataMagg.get(GregorianCalendar.DAY_OF_MONTH) – dataDiNascita.get(GregorianCalendar.DAY_OF_MONTH);

isMagg = anno>18 || (anno==18 && (mese>0 || (mese==0 && giorno>=0))) ;
}
return isMagg;
}

这里有最高精度!