Some help in java? :)

Delta517

Its okay...Im a ninja ;)
OP
Member
Joined
Nov 25, 2008
Messages
1,329
Trophies
0
Age
29
Website
Visit site
XP
1,180
Country
Norway
Hi!
Cheesegrin.gif
Im just learning to code and Im following Buckys tutorial on YouTube
smile1.gif
Im just up to part 18 so I really can't much, but I figured I'd try out something myself
smile1.gif


So basically I wanted to make a code where you wrote in your age and it would give you three different answers based on what you putted in
smile1.gif


Here's the code:

import java.util.Scanner;

class apples{
public static void main(String args[]){
Scanner ageInput = new Scanner(System.in);
int age;

System.out.println("Please enter your age here: ");
age = ageInput.nextInt();

if (ageInput.nextInt() >= 60)
System.out.println("You are a senior citizien");
else if (ageInput.nextInt() >=50)
System.out.println("You are in your 50s");
else
System.out.println("You are a young buck");
}
}

So the problem is that I have to insert three numbers before it will return any of the three answers that it should give
s.gif
Help, please? And please keep in mind that I'm a newbie at this xP
 

Dermy

Well-Known Member
Member
Joined
Jul 1, 2007
Messages
314
Trophies
0
XP
334
Country
United States
Your if statements should have 'age' in them, not 'ageInput.nextInt()'. Right now you are telling the scanner to keep getting a new value from the user when you enter each conditional statment. Since you already got the value and stored it in the variable called age, you should use that as the comparison in your conditional statements.

if (age >= 60)

else if (age >=50)

On a side note, you could change System.out.println("Please enter your age here: "); to System.out.print("Please enter your age here: "); so that when you enter the age it will stay on the same line as the message.
 

Delta517

Its okay...Im a ninja ;)
OP
Member
Joined
Nov 25, 2008
Messages
1,329
Trophies
0
Age
29
Website
Visit site
XP
1,180
Country
Norway
Your if statements should have 'age' in them, not 'ageInput.nextInt()'. Right now you are telling the scanner to keep getting a new value from the user when you enter each conditional statment. Since you already got the value and stored it in the variable called age, you should use that as the comparison in your conditional statements.

if (age >= 60)

else if (age >=50)

On a side note, you could change System.out.println("Please enter your age here: "); to System.out.print("Please enter your age here: "); so that when you enter the age it will stay on the same line as the message.

There! :D Thank you very much ^^ And also thanks for the tip! :D I see I still got a long way ahead of me, but atleast Im on the right path ^^
 

Delta517

Its okay...Im a ninja ;)
OP
Member
Joined
Nov 25, 2008
Messages
1,329
Trophies
0
Age
29
Website
Visit site
XP
1,180
Country
Norway
Another question :) I learned how to make a very basic calculator in the turoials, but I wanted it to show what numbers you entered in the equation(right word? xP).

I tried this:

import java.util.Scanner;

class apples{
public static void main(String args[]){
Scanner inputNum = new Scanner(System.in);
double fnum, snum, answer;

System.out.print("Enter first number: ");
fnum = inputNum.nextDouble();

System.out.print("Enter second number: ");
snum = inputNum.nextDouble();

answer = fnum + snum;
System.out.print(fnum "+" snum "=" answer);
}
}

But it didn't work, si I was wondering if any of you could tell med how to make it show something like "5,7 + 3,4 = 9,1" at the end? :)
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    HiradeGirl @ HiradeGirl: I met you in my dreams.