Results 1 to 4 of 4
Thread: Need some help
- 06-11-2012, 11:28 PM #1
New To Java
- Join Date
- Jun 2012
- Posts
- 3
- Rep Power
- 0
Need some help
Hey there, I am extremely new to java and was wanting some help on this bug I am getting.
My code is as follows:
But whenever I run the program, no matter what I do, I get my average is 0.0Java Code:import java.util.Scanner; public class bavg{ public static void main (String args[]){ Scanner keyboard=new Scanner(System.in); System.out.println("Enter the amount of at-bats."); int ab=keyboard.nextInt(); System.out.println("Now, enter the amount of hits."); int hits=keyboard.nextInt(); double avg=(hits/ab); System.out.println("Your batting average is "+avg+"."); } }
Anyone have any suggestions?
Again I am very new to java so I apologize in advance for any silly mistakes.
- 06-12-2012, 01:07 AM #2
New To Java
- Join Date
- Jun 2012
- Posts
- 3
- Rep Power
- 0
Re: Need some help
I tried turning the int "avg" into a double to see if that did anything, but it didn't help.
- 06-12-2012, 02:02 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 24
- Rep Power
- 0
Re: Need some help
You need to type cast the int variables to double. If "ave" is a double, "ab" and "hits" need to be converted into doubles as well.
Or this, basically the same thing:Java Code:double avg= (double)hits/(double)ab;
Alternatively, you could just change those int variables into double.Java Code:double avg= ((double)hits)/ab;
Last edited by atac57; 06-12-2012 at 02:04 AM.
- 06-12-2012, 02:11 AM #4
New To Java
- Join Date
- Jun 2012
- Posts
- 3
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks