java help #2

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€

Quote
package test;

import java.util.Scanner;

public class test {
public static void main (String args[]){

Scanner sc = new Scanner (System.in);
System.out.println("Years:");
double x= sc.nextDouble();
System.out.println("Earnings:");
double y= sc.nextDouble();

if (1>x);
System.out.println("No extra money!");

if (1<=x && x<5);
double money;
money=y*0.8;
System.out.println("You will get " +money+ " euro");

if (x>=5);
double money1;
money1=y*0.8+500;
System.out.println("You will get " +money1+ " euro");
}
}

yes i'm pretty much the biggest noob >:d
i hope anybody can heelp
Comments
19
remove the ";" behind if(..) and replace it with a {

and also at the end of the if structure put a }
ahhh yeah that was it, thank you :D
Parent
you've made some other mistakes which will compile but are crappy code, check my comment below
Parent
package test;

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");
}
}
}
Im not sure about java, but at c# -> if you need to introduce more that 1 argument inside a cicle, you need to use { }, so add them at if cicles. And we dont use ; in front of if ... its a cicle... dunno about java :f

Cant help that much
c# is almost the same as java except for the interface implementations and so forth (syntaxwise)
Parent
ye, but i only started learning c# 1.5 months ago :D dont know that much yet...
Parent
why not java? dotnet is retarded :s
Parent
well.. dunno.. we had to choose between c# and c++, but teacher said that c# its easier (garbage collector... memory threads etc)... I kinda like the language, specially because its very similar with c++ and java (syntaxwise ye)... the bad (or good) thing is that its a microsoft language (.net)... but almost everyone uses microsoft... and a good coder can easly code at c#, java or c++.. language doesnt matter since its kinda easy and fast to learn with manuals :)
Parent
do

x < 1 instead of 1 > x

x>= 1 instead of 1 <=x

confusing to read
import java.util.Scanner;

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"..
income = earnings + bonus

System.out.print("Enter the number of years you have been working..")

ain't gonna compile ;)
Parent
thx for the help guys helps me to improve
lol, read better online books.
Parent
im learning at school but still criticism from better ppl helps me
Parent
redo the test expressions like so

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 }
remove the ; behind the if statements

I don't think you need brackets after an if statement if the code after the if statement is only 1 line.
Back to top