Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By Norm

Thread: while continue and for looping help

  1. #1
    divput is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default while continue and for looping help

    class Weather
    {
    public static void main(String[] args)
    {
    int i=1;
    double temp=0.0;
    final double ANNUAL_INCREASE=1.0;
    double uncertainty=0.0;
    String result= "\"";

    System.out.println("Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");

    while (true)
    {
    i=1;
    if (i ==3) continue;
    if (i<=5) break;
    i++;
    }

    switch (i)
    {
    case 1:
    uncertainty=2.1;
    break;
    case 2:
    uncertainty=4.3;
    break;
    case 4:
    uncertainty=7.4;
    break;
    case 5:
    uncertainty=8.6;
    break;
    }


    for (int j=0; j <= 12;j++)
    {
    temp = j>2 && j<10 ? 5*j+7: j+3;
    temp += (i-1)*ANNUAL_INCREASE;
    temp *= (1+ uncertainty/100);
    result += " " + (int) temp;
    System.out.println(i +result);
    }
    }
    }

    My while true statement is not correctly working which I believe in turn is affecting my for statement.
    My output needs to be in

    year
    1 result result result result result result result result result result result result
    2 result result result result result result result result result result result result
    4 result result result result result result result result result result result result
    5 result result result result result result result result result result result result

    as opposed to
    year
    1 result
    1 result result
    1 result result result
    1 result result result result

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: while continue and for looping help

    My while true statement is not correctly working
    What do you intend it to do?

    Java Code:
    while (true)
    {
        i=1; 
        if (i ==3) continue;
        if (i<=5) break;
        i++;
    }
    As written this loop correctly just assigns 1 to i. Ie, it is exactly equivalent to:

    Java Code:
    i = 1;

  3. #3
    divput is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Re: while continue and for looping help

    i=year

    I need it to loop through years 1-5 skipping year 3.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: while continue and for looping help

    Can you write a loop that prints out the loop index?
    Can you write a if test that tests if the index has a value of 3?

    If you can do those two things then you should be able to do what you want.
    j_arif123 likes this.

  5. #5
    j_arif123 is offline Member
    Join Date
    Jan 2012
    Location
    India
    Posts
    34
    Rep Power
    0

    Default Re: while continue and for looping help

    When i run part of your code:
    Java Code:
    class whi	{
    	public static void main(String args[])	{
    		int i=1;
    		while (true)
    		{
    			i=1; 
    			if (i ==3) continue;
    			if (i<=5) break;
    			System.out.println(" " + i);  // it prints nothing
    			i++;
    		}
    	}
    }
    Output :
    E:\java>javac whi.java

    E:\java>java whi

    E:\java>

    I tested the loop with a doubt that it might be infinite loop. But it was not. So, if you explain what you want to do with this, we can think about it.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: while continue and for looping help

    might be infinite loop
    Statements 6 and 8 stop that.

  7. #7
    divput is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Re: while continue and for looping help

    I have to use a while(true) statement and i has to =1. I need it to loop through 1-5 except 3. I had it working while(i<=5) and i=0 but thats not what is asked. I maybe need to add one more i++ line along with the continue (as a compound statement in curly braces) not really sure

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: while continue and for looping help

    Play computer with your code. Do each statement, write down the values of the variables as they change and see what happens when the statements are executed.
    Are statements 9 & 10 executed?

  9. #9
    divput is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Re: while continue and for looping help

    i can get it to loop 1-5 using:

    int i=1
    while (true)
    {
    //if (i==3) continue;
    if (i>5)break;
    System.out.println(i);
    i++;
    }

    but when I add in the continue, it just prints out 1 2

    Thanks for you help and suggestions.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: while continue and for looping help

    but when I add in the continue, it just prints out 1 2
    And then what does the program do? Does it exit or what?

    Think about where the value of i is changed vs where the test for == 3

Similar Threads

  1. use Continue with label
    By er1c550n20 in forum New To Java
    Replies: 2
    Last Post: 04-04-2010, 01:24 PM
  2. Continue or not?
    By kliane in forum New To Java
    Replies: 3
    Last Post: 01-19-2010, 02:42 PM
  3. press any key to continue
    By dotnet007 in forum New To Java
    Replies: 3
    Last Post: 05-11-2008, 05:19 AM
  4. How to use Continue
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •