对于我的低 Java 技能来说,这是一个很好的第一个项目吗? *年龄检测器*

Is this a good first project for my low Java skill? *Age Detector*

我是安德鲁,刚开始学习Java,做了个代码。我花了大约两个小时,但效果很好。它基本上允许你输入你的性别,然后是你的年龄,然后它会告诉你你是否老了,还有一堆不同的设置信息。它有点基础而且没用,但它是我的第一个项目 :p 这是它。

import java.util.Scanner;
class main
{
public static void main(String[] args)
{   
    while(true){

Scanner User = new Scanner(System.in);
double gender;
System.out.println("Enter Your Gender. 1 = Men 2= Women");
gender = User.nextDouble();
if(gender<1 || gender>2 || gender<2 && gender>1)
{   
System.out.println("That's a gender... Enter Your Gender. 1 = Men 2= 
Women");
gender = User.nextDouble(); 
}

{
if(gender>1 && gender<3)
{System.out.println("You are a Women");
double age;
System.out.println("Enter Your Age.");
age = User.nextDouble();

    if(age<0){System.out.println("You Arn't Even Born...");}
    else{
    if(age>0 && age<13){System.out.println("You are so young you shouldn't 
be doing this!");}
    else{
    if(age>12 && age<21){System.out.println("You're really young!");}
    else{
    if(age>20 && age<25){System.out.println("These are the best years!");}
    else{
    if(age>24 && age<46){System.out.println("There is still a lot more in 
store for you!");}
    else{
    if(age>45 && age<61){System.out.println("Enjoy your life, while you 
still have many years left");}
    else{
    if(age>60 && age<71){System.out.println("Life is starting to fade away, 
Live it to the fullest!");}
    else{
    if(age>70 && age<81){System.out.println("I would start preparing for the 
worst...");}
    else{
    if(age>80 && age<101){System.out.println("You might want to say your 
final goodbyes...");}
    else{
    if(age>100){System.out.println("How are you not dead yet? WHAT IS YOUR 
SECRET!!!");}
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
if(gender<2 && gender>0)
{System.out.println("You are a Man");
double agetwo;
System.out.println("Enter Your Age.");
agetwo = User.nextDouble();

    if(agetwo<0){System.out.println("You Arn't Even Born...");}
    else{
    if(agetwo>0 && agetwo<8) {System.out.println("You are so young you 
shouldn't be doing this!");}
    else{
    if(agetwo>7 && agetwo<21) {System.out.println("You're really young!");}
    else{
    if(agetwo>20 && agetwo<25){System.out.println("You are still really 
young!");}
    else{
    if(agetwo>24 && agetwo<31){System.out.println("Enjoy these golden 
days!");}
    else{
    if(agetwo>30 && agetwo<46){System.out.println("Life is still burning as 
bright as ever!");}
    else{
    if(agetwo>45 && agetwo<66){System.out.println("There is still gas in the 
can!");}
    else{
    if(agetwo>65 && agetwo<71){System.out.println("Live every day to the 
fullest, you still have many left!");}
    else{
    if(agetwo>70 && agetwo<85){System.out.println("There are preperations 
you should make");}
    else{
    if(agetwo>84 && agetwo<95){System.out.println("Life may end any day, Be 
ready for it");}    
    else{
    if(agetwo>94){System.out.println("You shouldn't be alive.... Tell me 
your secret...");}
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

            }
        }
    }
}

很好,但对于你的下一个项目:

1- 您可以在 class 中声明 Scanner 而不是方法。您需要在 while 循环中声明一次而不是每次都声明它。

2- 变量 gender 可以声明为 booleanint

3- 您可以使用 if(gender == 2) 而不是 if(gender>1 && gender<3)

4- 您可以使用 else if{ 而不是 else{ if{

5- 您不需要声明 age 变量两次。你可以做一次。

6-代码缩进很有用(尝试使用它)

例如

public void test(){
    if(condition){
        //some code here 
    } else {
        //some code here
    }
}

以及您可以在 clean code robert cecil martin

中学到的许多东西