2 Replies - 444 Views - Last Post: 19 June 2013 - 04:45 AM Rate Topic: -----

#1 Zach786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 07-June 13

Incorrect output for Celsius/Fahrenheit Table output

Posted 19 June 2013 - 12:20 AM

I trying to create celsius/fahrenheit table using java. I am not sure what is wrong with my code.Any feedback would be appreciated.Thanks in Advance!
// Celsius/Fahrenheit Table
public class W_Loop_Celsius_FahrenheitDegree {
	public static void main (String args[]){
		double CelsiusDegree,FahrenheitDegree;
		FahrenheitDegree = 0.0;
		System.out.println("The table list Fahrenehit Degree and there relative value in Celsius Degree");
		System.out.println("Fahrenheit  Celsius\n");
		while (FahrenheitDegree <=300){
		CelsiusDegree = (5.0/9.0)*(FahrenheitDegree-32);
		System.out.printf("%d,%d",(int)FahrenheitDegree,(int)CelsiusDegree);
		FahrenheitDegree = FahrenheitDegree +20;
		}
	}
	
}


Output
The table list Fahrenehit Degree and there relative value in Celsius Degree
Fahrenheit  Celsius

0,-1720,-640,460,1580,26100,37120,48140,60160,71180,82200,93220,104240,115260,126280,137300,148



Is This A Good Question/Topic? 0
  • +

Replies To: Incorrect output for Celsius/Fahrenheit Table output

#2 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Incorrect output for Celsius/Fahrenheit Table output

Posted 19 June 2013 - 02:15 AM

I added a line feed ("\n") on line 10 and fixed a grammar mistake on line 5. :)

It outputs correctly on my machine.

Spoiler


I can't confirm that the values are correct, but they certainly differ from yours.

Quote

The table list Fahrenehit Degree and their relative value in Celsius Degree
Fahrenheit Celsius

0,-17
20,-6
40,4
60,15
80,26
100,37
120,48
140,60
160,71
180,82
200,93
220,104
240,115
260,126
280,137
300,148

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: Incorrect output for Celsius/Fahrenheit Table output

Posted 19 June 2013 - 04:45 AM

Yep, you're just missing a line feed.

I would consider a for loop:
System.out.println("Fahrenheit  Celsius");
for(int f=0; f<=300; f+=20) {
	int CelsiusDegree = (int)((5.0/9.0)*(f-32));
	System.out.printf("%d,%d\n",f,CelsiusDegree);
}



Also, rather than a comma, you can use size for the format. Then you could line up the numbers with your headers.

e.g.
System.out.printf("%12d %12d\n",f,CelsiusDegree);

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1