# Using the ? operator inside of a for loop

• September 23rd, 2013, 09:11 PM
///M Dood
Using the ? operator inside of a for loop
Code then description of problems:

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++) // 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.

Code :

```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:

Code :

``` 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!
• September 23rd, 2013, 09:14 PM
Junky
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?
• September 23rd, 2013, 09:36 PM
///M Dood
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:

Code :

```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:

Code :

```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.

Code :

```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);```

Code :

``` 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
• September 23rd, 2013, 09:46 PM
Junky
Re: Using the ? operator inside of a for loop
I cannot see your entire code.
• September 23rd, 2013, 09:57 PM
///M Dood
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:

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++) { 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:

Code :

```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```
• September 23rd, 2013, 10:05 PM
Junky
Re: Using the ? operator inside of a for loop
Look at exactly what is being printed inside the for loop.
• September 23rd, 2013, 10:17 PM
///M Dood
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?
• September 23rd, 2013, 10:21 PM
Junky
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.
• September 23rd, 2013, 10:29 PM
///M Dood
Re: Using the ? operator inside of a for loop
The type cast of temp is desired, the 0 at the is not however.
• September 23rd, 2013, 10:32 PM
Junky
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.
• September 23rd, 2013, 10:47 PM
///M Dood
Re: Using the ? operator inside of a for loop
hhrrmm... OK, I'll try to make some changes, thanks for the input.
• September 25th, 2013, 06:16 PM
///M Dood
Re: Using the ? operator inside of a for loop
OK, so i figured out what was funky inside of the for loop.

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; // 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:
Code :

`"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.

Code :

```------------------------ 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.
• September 25th, 2013, 08:19 PM
///M Dood
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:

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)

Code :

```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?????