java help #2
•
26 Nov 2008, 21:21
•
Journals
i did a little test now but it doesn't quite work..i can run it but i always get all 3 System.out.println 's
task:
years in firm less than 1 : no money
atleast 1 year but less than 5 : 80% of the earnings
5 years or more in firm: 80% of earnings + 500€
yes i'm pretty much the biggest noob >:d
i hope anybody can heelp
task:
years in firm less than 1 : no money
atleast 1 year but less than 5 : 80% of the earnings
5 years or more in firm: 80% of earnings + 500€
yes i'm pretty much the biggest noob >:d
i hope anybody can heelp
and also at the end of the if structure put a }
import java.util.Scanner;
public class test {
public static void main (String args[]){
double money = 0;
Scanner sc = new Scanner (System.in);
double x= sc.nextDouble();
System.out.println("Years: " + x);
double y= sc.nextDouble();
System.out.println("Earnings: " + y);
if (1>x){
System.out.println("No extra money!");}
if (1<=x && x<5){
money=y*0.8;
System.out.println("You will get " +money+ " euro");
}
if (x>=5){
money=y*0.8+500;
System.out.println("You will get " +money+ " euro");
}
}
}
Cant help that much
x < 1 instead of 1 > x
x>= 1 instead of 1 <=x
confusing to read
public class Test
{
public static void main (String args[])
{
Scanner input = new Scanner (System.in);
int year;
int earnings=1000;
int bonus=0;
System.out.print("Enter the number of years you have been working: ");
year = input.nextInt();
if (year<1)
System.out.println("You receive no bonus");
else if (year>=1 && year<5)
bonus=(earnings/100)*80;
else
bonus=(earnings/100)*80+500;
System.out.printf("You will earn %d extra.\n", bonus);
}
}
-------
still, don't really understand what you mean by "earnings"..
System.out.print("Enter the number of years you have been working..")
ain't gonna compile ;)
if (1>x)
else if (1<=x && x<5)
else if (x>=5)
that way the program doesnt have to go through all the branches, so less cpu time
edit with the braces as above thou : ]
if (x != y) { do smth }
else if (x == y) { do smth else }
I don't think you need brackets after an if statement if the code after the if statement is only 1 line.