0

I am Andrew, I just started learning Java and made a code. It took me about two hour but it works well. It basically allows you to input your gender, and then your age, it then tells you if you are old or not, with a bunch of different set messages. It's kinda basic and useless but its my first project :p Here it is.

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...");}
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

            }
        }
    }
}
  • 2
    sounds for more like https://codereview.stackexchange.com/ – dumbPotato21 Apr 30 '17 at 06:42
  • @Shashwat agreed, but still getting anything to work as a first project is good work, keep it up!! –  Apr 30 '17 at 06:43
  • Thanks, those were really quick responses – TheSilverSpade Apr 30 '17 at 06:44
  • 1
    Congrats, but this is not a question for stackoverflow, – Erich Kitzmueller Apr 30 '17 at 06:44
  • The formatting could be improved to make it better readable. – Henry Apr 30 '17 at 06:49
  • This looks very much like the program I made when I started programming. If you like to get some basic programming skills I'd recommend you start with an interpreted language like Python or Ruby - they'll save you from some of the stuff that goes around languages like Java eg. like compiling, VMs etc. – Michael Banzon Apr 30 '17 at 06:50
  • @Henry Sorry, It was the format I could handle. I am new to java and like doing things a certain way, that format just worked with me – TheSilverSpade Apr 30 '17 at 06:50
  • @mbanzon Java is the operating system of what i want to develop for, Python and Ruby might help with skills, but don't intrest me in the long run, i might consider them in the future, But I think i will press through Java, Compiling is simple enough, VMs are Virtual Machines right? Dont quite know what they have to do with Java – TheSilverSpade Apr 30 '17 at 06:52
  • @thesilverspade this is poorly formatted. I would recommend starting with even simpler programs, like a basic addition-subtraction-multiplication-division calculator, or adventure based programs, to get you used to using methods. –  Apr 30 '17 at 07:07
  • @theProgrammer101 when i said it was my first program i kinda lied, i made a calculator.. Very basic, when i first started, this is my first bigger project – TheSilverSpade Apr 30 '17 at 08:44
  • @thesilverspade HAHA i thought it was a little bit advanced for a first-timer. –  Apr 30 '17 at 08:45
  • @theProgrammer101 Its my first time without Tutorials or Guides, or any internet help at all... Calculator was a tutorial that i added a teeny bit on – TheSilverSpade Apr 30 '17 at 08:57
  • @TheSilverSpade Java programs are typically run in the Java Virtual Machine - it's perfectly fine if you know that is the route you'll take - it's great to have focus! My main point is that setting up a Java development environment is a hurdle, one that you might've passed already, bit still. In Java everything is a class, and you need to be very verbose to do simple (even) simple things. I started out learning Java in class - I whish the university had started with python eg. - at the moment I'm teaching my daughter (9yo) Python which she loves. You seem to be a bit futher - happy coding! – Michael Banzon Apr 30 '17 at 09:12

1 Answers1

2

good but for your next project:

1- you can declare Scanner in class instead of method. you need declare it once not every time in while loop.

2- the variable gender could be declare as boolean or int

3- you can use if(gender == 2) instead of if(gender>1 && gender<3)

4- you can use else if{ instead of else{ if{

5- you didn't need to declare age variable two times. you can do it once.

6- code indent is very useful (try to use it)

for example

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

and lots of things that you can learn in clean code robert cecil martin

Vahid Forghani
  • 343
  • 1
  • 2
  • 14
  • Thanks, fogot about else if. also forgot to do == instead of = When i took out the second age it stoped working and said "Variable might not have been intiated" or something like tht, I tried to make it without looking online at all because i wanted to learn it, without copying someone. Also with the format, The format i used is one that worked with my brain, I could look at it and know what i was looking for, It just looked good to me, sorry if that isn't good. – TheSilverSpade Apr 30 '17 at 08:42