
-----------------------------------
aqazwsx1
Sat Feb 07, 2009 5:34 pm

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.




import java.util.Scanner;

public class Percent {

public static void main (String

Mod Edit: Remember to use syntax tags! Thanks :) [syntax="java"]Code Here[/syntax]

-----------------------------------
SIXAXIS
Sat Feb 07, 2009 5:55 pm

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 (double)num1 / (double)Total * 100


Mod Edit: Remember to use syntax tags! Thanks :) [syntax="java"]Code Here[/syntax]

-----------------------------------
andrew.
Sat Feb 07, 2009 6:00 pm

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. percent1 = (int)((double)num1 / (double)Total * 100);

-----------------------------------
aqazwsx1
Sat Feb 07, 2009 6:17 pm

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
Sat Feb 07, 2009 6:45 pm

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
Sat Feb 07, 2009 6:49 pm

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
Sat Feb 07, 2009 11:29 pm

RE:Output always 0.0
-----------------------------------
Let's try making this code look a little bit nicer too.

import java.util.Scanner;

public class Percent {
    public static void main(String

And, how about we break out the prompt logic.

import java.util.Scanner;

public class Percent {
    public static void main(String
