Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Simple Java Code Help--- temperature conversion using while loop

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple Java Code Help--- temperature conversion using while loop

    Hello, extreme Java idiot here.

    For lab, we had to have the program output to the console screen the conversion of 98.6 degrees Fahrenheit to celsius. I did that.
    Then we had to have it print the conversion of Fahrenheit 0.0, 5.0, 10.0.... 40.0 using a for loop. Below, I think I did it.

    public class Lab3 {
      public static void main (String[] args){
          float fahrenheit=98.6f, celsius;
          celsius=(5.0f/9)*(fahrenheit-32);
          System.out.println(fahrenheit + " degrees Fahrenheit is " + celsius + " degrees Celsius.");
     
          System.out.println();
          for (fahrenheit=0f; fahrenheit <=40; fahrenheit ++){
              celsius=(5.0f/9)*(fahrenheit-32);
              if (fahrenheit%5==0)
                  System.out.println(fahrenheit + " degrees Fahrenheit is " + celsius + " degrees Celsius.");
          }
      }
    }


    After that, we had to use a while loop instead. Here is where i'm confused:
    With while loop replacing for loop:

    All I did was replace the if statement with a while one.

    public class Lab3 {
      public static void main (String[] args){
          float fahrenheit=98.6f, celsius;
          celsius=(5.0f/9)*(fahrenheit-32);
          System.out.println(fahrenheit + " degrees Fahrenheit is " + celsius + " degrees Celsius.");
     
          System.out.println();
          for (fahrenheit=0f; fahrenheit <=40; fahrenheit ++){
              celsius=(5.0f/9)*(fahrenheit-32);
              while (fahrenheit%5==0)
                  System.out.println(fahrenheit + " degrees Fahrenheit is " + celsius + " degrees Celsius.");
          }
      }
    }

    However, my program begins to run infinitely and does not stop when I do this.

    But actually what the assignment says is that I have to replace the for loop with a while loop? So am I supposed to change the 'for
    to a 'while?'

    Thanks in advance for having patience with me.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Simple Java Code Help--- temperature conversion using while loop

    Quote Originally Posted by Idiot_willing_to_learn View Post
    But actually what the assignment says is that I have to replace the for loop with a while loop? So am I supposed to change the 'for
    to a 'while?'
    What happened when you tried exactly that?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple Java Code Help--- temperature conversion using while loop

    Well if I just replace the 'for' with a 'while' it definitely wouldn't work.

    Lab3.java:8: error: ')' expected
    while (fahrenheit=0f; fahrenheit <=40; fahrenheit ++){
    ^
    Lab3.java:8: error: not a statement
    while (fahrenheit=0f; fahrenheit <=40; fahrenheit ++){
    ^
    Lab3.java:8: error: ';' expected
    while (fahrenheit=0f; fahrenheit <=40; fahrenheit ++){
    ^
    3 errors
    If I want it to print out the temperatures 0.0, 5.0., 10.0, 15.0, ... 40.0, I'm guessing I definitely have to say while (fahrenheit%5==0). I'm not sure how to get the while loop to go up to 40 though.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Simple Java Code Help--- temperature conversion using while loop

    Recommended reading: The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. how do i loop this simple java code?
    By legend101z in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 27th, 2013, 06:46 PM
  2. Question about methods, temperature conversion program
    By aivory616 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 23rd, 2012, 01:27 PM
  3. Temperature Conversion Chart
    By cb12991 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 8th, 2012, 12:11 AM
  4. Replies: 9
    Last Post: August 30th, 2012, 03:25 PM
  5. conversion d'un fichier doc en un fichier access (code java)
    By Chourouk2012 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 7th, 2012, 01:51 PM