7 Replies - 2027 Views - Last Post: 13 May 2012 - 03:41 PM Rate Topic: -----

#1 Kimmyizm   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-April 12

Celsius to Fahrenheit table, missing something

Posted 12 May 2012 - 08:12 PM

Hi. Yes this is a homework assignment. I have read through other posts and not been able to apply what I have read to this. I am not asking anyone to do my homework for me, I would like some help in locating what I am missing or what I have incorrect.

I have tried c and f both as double and int. Thanks for your input!

Task: Display table of Celsius temp 0 - 20 and Fahrenheit equivalent

public class CelsiusToFahrenheit
{
	public static void main(String[] args)
	{
		int c = 0;
		int f;

		System.out.println("Celsius   Fahrenheit");
		System.out.println("--------------------");

		while (c <= 20; c ++);						//c = 0 or 1?
		{
		f = 9.0*c/5+32;
		System.out.println("     "  +c+  "     "+f);

		}
	}
	//this is how it is in chpt 5, but doesn't appear to do anything different
	static int f (int c)
		{
			int f;
			f = (9.0/5.0)*c+32;
			return f;
		}
}




The results are below. Where's the rest???

/*
C F
-----------------------
21.0 69.8
Press any key to continue . . .
*/

Is This A Good Question/Topic? 0
  • +

Replies To: Celsius to Fahrenheit table, missing something

#2 collegekid901   User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 41
  • Joined: 09-March 12

Re: Celsius to Fahrenheit table, missing something

Posted 12 May 2012 - 08:16 PM

Explain the problem you having. What is the expected results. What are your results
Was This Post Helpful? 0
  • +
  • -

#3 Kimmyizm   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-April 12

Re: Celsius to Fahrenheit table, missing something

Posted 12 May 2012 - 08:34 PM

Display table of Celsius temp 0 - 20 and Fahrenheit equivalent is what I am attempting to do.
The results that I am having are:
C F
-----------------------
21.0 69.8
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Celsius to Fahrenheit table, missing something

Posted 12 May 2012 - 08:51 PM

Where'd you learn to make a while statement like this:

while (c <= 20; c ++);

Check that.

And if you want floating point results, you should use floating point variables.
Was This Post Helpful? 1
  • +
  • -

#5 collegekid901   User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 41
  • Joined: 09-March 12

Re: Celsius to Fahrenheit table, missing something

Posted 12 May 2012 - 08:52 PM

Pretty much what Greg said, fix your while loop and it should work out fine.
Was This Post Helpful? 0
  • +
  • -

#6 Kimmyizm   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-April 12

Re: Celsius to Fahrenheit table, missing something

Posted 12 May 2012 - 08:58 PM

	while (c > 1; c <= 20; c ++)	



Getting 3 errors. Maybe I'll try some sleep and look at it tomorrow. Happy Mother's Day to me, haha.
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Celsius to Fahrenheit table, missing something

Posted 13 May 2012 - 03:35 AM

A while loop should not be a for loop. What happens in your while loop is it gets to while (c > 1; and drops out of the loop, as inside a while loop, that's what a ; means. So then it only executes once. You can fix this one of two ways.

1) Fix the while loop. Use a while loop syntax:
		while (c <= 20) // <-- NO SEMICOLONS		//c = 0 or 1?
		{
		    f = 9.0*c/5+32;
		    System.out.println("     "  +c+  "     "+f);
		    c++;
		}



2) Use a for loop, which requires exactly 2 semicolonsL
		for (c = 1; c <= 20; c ++) 
		{
		    f = 9.0*c/5+32;
		    System.out.println("     "  +c+  "     "+f);

		}



So it looks like you simply do not know your loop structures.
http://coweb.cc.gate...va_Tutorial.pdf
Was This Post Helpful? 1
  • +
  • -

#8 Kimmyizm   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-April 12

Re: Celsius to Fahrenheit table, missing something

Posted 13 May 2012 - 03:41 PM

Problem solved! Finally! Thanks for your help everyone, I "think" I finally know the difference with "while" and "for" use... Feel pretty stupid, but am trying to "get it".
public class CelsiusToFahrenheit
{
	public static void main(String[] args)
	{
		int c = 0;
		int f;

		System.out.println("Celsius   Fahrenheit");
		System.out.println("--------------------");

		//while (c <= 20)
		for (c = 1; c <= 20; c++)
		{
			f = 9*c/5+32;
			//tried f = 9*c/5+32;
			System.out.println("     "  +c+  "     "+f);
		//c++;

		}
	}
}
/*
Celsius   Fahrenheit
--------------------
     1     33
     2     35
     3     37
     4     39
     5     41
     6     42
     7     44
     8     46
     9     48
     10     50
     11     51
     12     53
     13     55
     14     57
     15     59
     16     60
     17     62
     18     64
     19     66
     20     68
Press any key to continue . . .
*/


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1