Results 1 to 7 of 7
Thread: Help with java math
 10252010, 01:09 PM #1Member
 Join Date
 Oct 2010
 Posts
 3
 Rep Power
 0
Help with java math... SOLVED
Java Code:import java.util.Scanner; public class Sphere { public static void main(String [] a) { System.out.println("This program prints surface area and volume"); System.out.println("of spheres for radii ranging from 10 to N,"); System.out.print("in increments of 10. Please input the value of N: "); Scanner userInput = new Scanner(System.in); int valueOfN = userinput.nextInt(); double pi = 3.14; System.out.println(""); System.out.println("\t\tSurface"); System.out.println("r\tArea\tVolume"); System.out.println(""); for(int r = 10; r <= valueOfN; r += 10) { System.out.print(r + "\t" + (4*pi*(r*r)) + "\t" + (4/3*pi*(r*r*r))); } } }
Last edited by gabrielfirestorm; 10252010 at 01:25 PM. Reason: solved
 10252010, 01:13 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,311
 Blog Entries
 7
 Rep Power
 24
4/3 == 1 because both 4 and 3 are integers so integer division is used. Use 4.0/3.0 instead; oh, b.t.w. the Math class has a better approximation for pi.
kind regards,
Jos
 10252010, 01:14 PM #3
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 22
Seems your question is not a valid one, it's 0.75 x pi x r^{3} or other way?
 10252010, 01:14 PM #4Senior Member
 Join Date
 Jun 2008
 Posts
 2,571
 Rep Power
 11
do "4/3.0" or "4/(double) 3" or something similar. bare numbers in code without decimals are integers and integer math can only produce integers, thereby making 4/3 = 1.
Edit: Man, am I slow.
 10252010, 01:18 PM #5
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 22
 10252010, 01:22 PM #6Member
 Join Date
 Oct 2010
 Posts
 3
 Rep Power
 0
thank you very much that explains everything :)
 10252010, 01:54 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,311
 Blog Entries
 7
 Rep Power
 24
Similar Threads

hi guys help me in math.random () in java
By funkygarzon in forum New To JavaReplies: 5Last Post: 08092010, 04:48 AM 
Help need on math java program
By zidangus in forum New To JavaReplies: 27Last Post: 07072010, 02:48 AM 
Java Math
By levent in forum Java TutorialReplies: 1Last Post: 05122008, 09:03 AM 
java.lang.Math
By eva in forum New To JavaReplies: 2Last Post: 01312008, 05:17 PM 
Help with math in java
By fernando in forum New To JavaReplies: 1Last Post: 08062007, 06:05 AM
Bookmarks