Author |
Message |
aqazwsx1
|
Posted: Sat Feb 07, 2009 5:34 pm Post subject: Output always 0.0 |
|
|
Hi, this program is suppose to get how much percent the first numbers is divided by the total. No matter what you enter the percent is always 0.0 Why is this happening?
Thanks in advance.
Java: | import java.util.Scanner;
public class Percent {
public static void main (String[] args ) {
Scanner input= new Scanner (System. in);
int num1;
int num2;
int Total;
int percent1,percent2;
System. out. println ("Enter");
num1=input. nextInt();
System. out. println ("Enter again");
num2=input. nextInt();
Total=num1+num2;
percent1=num1/Total* 100;
System. out. println (percent1 );
}
} |
Mod Edit: Remember to use syntax tags! Thanks code: | [syntax="java"]Code Here[/syntax] |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
SIXAXIS
|
Posted: Sat Feb 07, 2009 5:55 pm Post subject: RE:Output always 0.0 |
|
|
I think it's because it rounds it because it's an integer. Try casting it as a double like this Java: | (double)num1 / (double)Total * 100 |
Mod Edit: Remember to use syntax tags! Thanks code: | [syntax="java"]Code Here[/syntax] |
|
|
|
|
|
|
andrew.
|
Posted: Sat Feb 07, 2009 6:00 pm Post subject: RE:Output always 0.0 |
|
|
Remember when putting the value into the percent1 variable, you have to cast it all back to an integer.
e.g. Java: | percent1 = (int)((double)num1 / (double)Total * 100); |
|
|
|
|
|
|
aqazwsx1
|
Posted: Sat Feb 07, 2009 6:17 pm Post subject: Re: Output always 0.0 |
|
|
I still get an output of 0.0 Does this happen when you guys run it? Maybe theres a problem with my java? |
|
|
|
|
|
aqazwsx1
|
Posted: Sat Feb 07, 2009 6:45 pm Post subject: Re: Output always 0.0 |
|
|
I still get an output of 0.0 Does this happen when you guys run it? Maybe theres a problem with my java? |
|
|
|
|
|
syntax_error
|
Posted: Sat Feb 07, 2009 6:49 pm Post subject: RE:Output always 0.0 |
|
|
Read up on data types, which is the reason why you get that out put. Try change all the data types to float, or double depends how many decimals you want it to go up to |
|
|
|
|
|
wtd
|
Posted: Sat Feb 07, 2009 11:29 pm Post subject: RE:Output always 0.0 |
|
|
Let's try making this code look a little bit nicer too.
Java: | import java.util.Scanner;
public class Percent {
public static void main (String[] args ) {
Scanner input = new Scanner (System. in);
System. out. println("Enter");
int num1 = input. nextInt();
System. out. println("Enter again");
int num2 = input. nextInt();
int total = num1 + num2;
int percent1 = num1 / total * 100;
System. out. println (percent1 );
}
} |
And, how about we break out the prompt logic.
Java: | import java.util.Scanner;
public class Percent {
public static void main (String[] args ) {
Scanner input = new Scanner (System. in);
int num1 = getIntWithPrompt ("Enter", input );
int num2 = getIntWithPrompt ("Enter again", input );
int total = num1 + num2;
int percent1 = num1 / total * 100;
System. out. println(percent1 );
}
static int getIntWithPrompt (String prompt, Scanner s ) {
System. out. println(prompt );
return s. nextInt();
}
} |
|
|
|
|
|
|
|