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 13 of 13

Thread: Using the ? operator inside of a for loop

  1. #1
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Using the ? operator inside of a for loop

    Code then description of problems:

    class Weather 
    {
    	public static void main(String[] args) 
    	{
    	// declare variables
    	int i = 1; // represents year
    	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)
    	{
    	if (i > 5) break;
            if (i == 3)
            {
                   i++;
                  continue;
             }
     
    	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;
    		default:
    			uncertainty = 0;
    			break;
    	} // end of switch statement
     
    	for (int j = 1 ; j <= 12 ; j++)                    // Start of for loop, works 1-12
    	{                                                          // 
    	temp = j <= 10 && j > 2 ? j + 7 : j + 3;     // Assign temp, based on j
    	temp += uncertainty;                              // Apply uncertainty to temp
    	}                                                          //
    	System.out.println(i + result + (int)temp);  // Test results for now
    	i++;
    	}// end of while loop
     
    	}
    }

    Just for reference, here is the chunk of my problem. I am not asking for a hand out, i just want to make my questions as clear as i can.

    Next (still inside the while loop), use the following formula to compute the temperature of each of the 12 months of years 1, 2, 4 and 5.  Use a for loop to loop over the months in a given year.
    ---------------
    Year i = 1:
    ---------------
    Month j = 1, 2, ... 12
    Note: (A) and (B) below, taken together will be called "Equation 1".
    (A) Winter Months: j = 1, 2, 11 and 12:  Temperature = j + 3
    (B) Other Months: j = 3, 4, 5, .., 9, 10:  Temperature = 5j + 7 // i don't know if the teacher typo'd here or not???
     
    Note: Use a logical expression to differentiate between Winter and Other Months, in conjunction with the ?: operator, all in one single statement to compute the temperature for a month j using Equation 1.

    OK, so the problem lies inside of the for loop. I need to use the ? operator to determine what the value of temp should be. The ways it's written, to me at least, it should work. Yet when i try to test my results inside of a print statement, somewhere in the code it (meaning me) seems to forget that there are 12 months per year. It's printing out the years correctly, but only giving my variable temp one value per year. Giving me an output of:

     Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
    1 17
    2 19
    4 22
    5 23

    PS those values aren't even right!!! I am not very good at structuring up a well written question on here, so if i am missing any info let me know. I'm here all night!


    Thanks!


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Using the ? operator inside of a for loop

    If your aim is to print out 12 values (one per month) for fives years then you will need to print 12 values per line and 5 lines. How many loops do you think are required?
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Using the ? operator inside of a for loop

    Welp, I have my years variable in the while loop if "(i > 5) break;" , that one seems to be taken care of. Next, i need to display the values of my "temp" value, 12 times per 4 total lines, which should be taken care of with my for loop (inside of my while loop). So, if i were to put my print statement inside the for loop, like this:

    for (int j = 1 ; j <= 12 ; j++)                  // Start of for loop, works 1-12
    	{                                            // 
    	temp = j <= 10 && j > 2 ? j + 7 : j + 3;     // Assign temp, based on j
    	temp += uncertainty;                              // apply uncertainty
    	System.out.println(i + result + (int)temp);  // NEW PRINT TEST
    	}

    It gives me the result like this:

    1 6
    1 7
    1 12
    1 13
    1 14
    1 15
    1 16
    1 17
    1 18
    1 19
    1 16
    1 17
    2 8
    2 9 
    etc
    etc
    you get it

    YET, when i try to test my for loop only, I do get 12 values, code then results.

    for (int j = 1 ; j <= 12 ; j++)              
    	{                                            
    	System.out.print(result + j + result);
    	//temp = j <= 10 && j > 2 ? j + 7 : j + 3;     
    	//temp += uncertainty;                         
    	}                                            
    	System.out.println(result + (int)temp);

     Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
     1  2  3  4  5  6  7  8  9  10  11  12  0
     1  2  3  4  5  6  7  8  9  10  11  12  0
     1  2  3  4  5  6  7  8  9  10  11  12  0
     1  2  3  4  5  6  7  8  9  10  11  12  0

    I mean, i feel like I'm kind of close but to answer your specific question, these two loops should have the assignment covered.

    --- Update ---

    PS i have no clue where the 0 comes from

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Using the ? operator inside of a for loop

    I cannot see your entire code.
    Improving the world one idiot at a time!

  5. #5
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Using the ? operator inside of a for loop

    Sorry, i just thought you might want to see the little chunk that i altered. Here ya go:

    class Weather 
    {
    	public static void main(String[] args) 
    	{
    	// declare variables
    	int i = 1; // represents year
    	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)
    	{
    	if (i > 5) break;
            if (i == 3)
            {
                   i++;
                  continue;
             }
     
    	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;
    		default:
    			uncertainty = 0;
    			break;
    	} // end of switch statement
     
    	for (int j = 1 ; j <= 12 ; j++)              
    	{                                            
    	System.out.print(result + j + result);
    	//temp = j <= 10 && j > 2 ? j + 7 : j + 3;     
    	//temp += uncertainty;                         
    	}                                            
    	System.out.println(i + result + (int)temp);                                           
     
    	i++;
    	}// end of while loop
     
    	}

    Output:

    Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
     1  2  3  4  5  6  7  8  9  10  11  12 1 0
     1  2  3  4  5  6  7  8  9  10  11  12 2 0
     1  2  3  4  5  6  7  8  9  10  11  12 4 0
     1  2  3  4  5  6  7  8  9  10  11  12 5 0

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Using the ? operator inside of a for loop

    Look at exactly what is being printed inside the for loop.
    Improving the world one idiot at a time!

  7. #7
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Using the ? operator inside of a for loop

    So 1 - 12, then my variable i (year), my year variable (i) and a mysterious 0, repeat?

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Using the ? operator inside of a for loop

    You are casting temp to an int which is resulting in a 0 being appended to the end of each line. If this is what you want then fine. If it is not what you want then you need to change what your print statements are printing.
    Improving the world one idiot at a time!

  9. #9
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Using the ? operator inside of a for loop

    The type cast of temp is desired, the 0 at the is not however.

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Using the ? operator inside of a for loop

    Once again I direct your attention to what is happening (or not happening) inside the loop.
    Improving the world one idiot at a time!

  11. #11
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Using the ? operator inside of a for loop

    hhrrmm... OK, I'll try to make some changes, thanks for the input.

  12. #12
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Using the ? operator inside of a for loop

    OK, so i figured out what was funky inside of the for loop.

    class Weather 
    {
    	public static void main(String[] args) 
    	{
    	// declare variables
    	int i = 1; // represents year
    	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)
    	{
    	if (i > 5) break;
            if (i == 3)
            {
                   i++;
                  continue;
             }
     
    	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;
    		default:
    			uncertainty = 0;
    			break;
    	} // end of switch statement
    	for (int j = 1 ; j <= 12 ; j++)              
    	{                                            
    	temp = j <= 10 && j > 2 ? 5*j + 7 : j + 3; // determine temp if j (month) is winter or other month
    	temp *= (int)(1 + uncertainty/100.0); 
    	System.out.println(uncertainty + result + temp);  // just a test.              
    	}
    	temp *= (1 + uncertainty/100.0);                                                                                     
    	//System.out.println(result+ result + i + result + result + result + temp);  // also a test
    	i++;
    	}// end of while loop
     
    	}
    }

    Next part of the assignment is:
    "1st year, so ANNUAL_INCREASE doesn't apply, but uncertainty does.  In the next line, increase Temperature by Uncertainty percent to display the final temperature, for each month."

    I was able to apply uncertainty, but wouldn't that apply to every month and year? And if so, that's where this section of the problem gets confusing for me.

    ------------------------
    Years: i = 2, 4, 5
    ------------------------
    First calculate Temperature using Equation 1.  Next, find the increases to apply to that value.  Temperature increase per year: ANNUAL_INCREASE (degrees) - same increase applies to each month of a year. Use the Year i and ANNUAL_INCREASE in an expression to figure the exact increase that would apply for that year.
     
    Uncertainty percent applies to the value obtained after the ANNUAL_INCREASE.

    This is all supposed to be done without exiting my for loop, and no if-else is instructed to determine what year the loop is on. Any translation would be greatly appreciated. Let me know if i left anything out.

  13. #13
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Using the ? operator inside of a for loop

    Nevermind, i got all of it...

    Last thing I'd like assistance with if anybody is willing is with my print statement. I am supposed to print my variable i (years) vertically, followed by the values of the temp for all 12 months inside my for loop. I can't figure out how that would happen now, at least without printing my i variable 12 times, which isn't ideal. Revised code:

    class Weather 
    {
    	public static void main(String[] args) 
    	{
    	// declare variables
    	int i = 1; // represents year
    	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)
    	{
    	if (i > 5) break;
            if (i == 3)
            {
                   i++;
                  continue;
             }
     
    	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;
    		default:
    			uncertainty = 0;
    			break;
    	} // end of switch statement
    	for (int j = 1 ; j <= 12 ; j++)              
    	{                                            
    	temp = j <= 10 && j > 2 ? 5*j + 7 : j + 3;
    	temp += (i-1) * ANNUAL_INCREASE;
    	temp *= (1 + uncertainty/100.0);
    	System.out.print(result + i + result + (int)temp + result); // Test results for now
    	}
    	//System.out.println(i);
    	i++;
    	}// end of while loop
     
    	}
    }

    And the output i get is expected (but not desired)

    Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
     1 4 
     1 5 
     1 22 
     1 27 
     1 32 
     1 37 
     1 42 
     1 47 
     1 53 
     1 58 
     1 14 
     1 15 
     2 5 
     2 6 
     2 23 
     2 29 
     2 34 
     2 39 
     etc
     etc

    So, it is printing the year, followed by temperature just like I asked it to, but obviously i want year/month and temp to line up. Is there a way to get around this inside of the for loop? I think it sounds crazy myself, but these are my teachers words exactly: "Inside the 'for loop', concatenate temperature values for all 12 months with spaces in between, assign it to 'result' and display in one line along with the Year i. Repeat for years 1, 2, 4 and 5. Try to align the output with the Month headers by manipulating the blank space in between."

    Anybody out there?????

Similar Threads

  1. Dividing inside a for loop. Lolwtf?
    By samy109 in forum Loops & Control Statements
    Replies: 6
    Last Post: July 23rd, 2013, 09:04 PM
  2. how to call a string inside a while loop
    By shen_punkz21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 25th, 2013, 07:25 AM
  3. New to Java, need help with arrays inside a loop please!
    By Darkaudicate in forum Loops & Control Statements
    Replies: 1
    Last Post: November 21st, 2012, 06:32 PM
  4. Can I have an if statement inside a while loop? or something similar?
    By ColeTrain in forum Java Theory & Questions
    Replies: 3
    Last Post: October 3rd, 2012, 05:01 PM
  5. Timer inside loop
    By dove in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 26th, 2012, 09:57 AM