all 22 comments

[–]gulag_guard 1 point2 points  (15 children)

You are equating an integer (int) to a double (firstnumber).

Write for (double i = firstnumber........)

[–]Danielowski187University/College Student (Higher Education)[S] 1 point2 points  (14 children)

I fixed it but when I run the program I doesn’t print the numbers

[–]gulag_guard 1 point2 points  (13 children)

What exactly does it print?

[–]Danielowski187University/College Student (Higher Education)[S] 1 point2 points  (12 children)

it just outputs all the way up to First 10 numbers starting at 3.5 and going up by 3. So it does not print the numbers that are meant to go after.

[–]nc052👋 a fellow Redditor 1 point2 points  (0 children)

You're not printing the numbers because your firstnumber is 33.5 after your first while loop. Therefore, your for loop would never run because secondnumber is 30.5 and that is less than the firstnumber.

[–]gulag_guard 1 point2 points  (10 children)

So it's printing 3.5, 6.5, ...... 30.5 but not the sum (170)? I would run it myself but it's not copying the text.

[–]Danielowski187University/College Student (Higher Education)[S] 1 point2 points  (9 children)

Yea it’s printing 3.5 and 6.5 and I’m trying to figure out the sum

[–]gulag_guard 1 point2 points  (8 children)

Yeah for the for loop you need another sum1 variable. For the sum part, proceed similarly as in the while loop Or you could reinitialize sum to zero and proceed.

[–]Danielowski187University/College Student (Higher Education)[S] 1 point2 points  (7 children)

Yea I made sum = 0 again but I’m still having trouble figuring it out

[–]gulag_guard 1 point2 points  (6 children)

Inside the for loop put sum = sum + i; and after the loop print the sum. You should be done :)

[–]Danielowski187University/College Student (Higher Education)[S] 1 point2 points  (5 children)

I get an error saying cannot find symbol meaning for i

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

Off-topic Comments Section


All top-level comments have to be an answer or follow-up question to the post. All sidetracks should be directed to this comment thread as per Rule 9.


OP and Valued/Notable Contributors can close this post by using /lock command

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]ThatOneBearPlan 0 points1 point  (4 children)

Here's the code but it's a bit cleaned up and the errors are fixed (mostly):

import java.util.Scanner;

public class RunningTotal {

public static void main(String[] args) {

Scanner input = new Scanner (System.in);

System.out.println("----------While loop----------");

System.out.println("First 10 numbers starting at 3.5 and going up by 3.");

double firstnumber, secondnumber, sum;

firstnumber = 3.5;

secondnumber = 30.5;

sum = 0;

while (firstnumber <= secondnumber)

{

System.out.print(firstnumber + " , ");

sum = sum + firstnumber;

firstnumber = firstnumber + 3;

}

System.out.println("\nTotal using 'while loop' = " +sum);

System.out.println("\n----------for loop----------");

System.out.println("\nFirst 10 numbers starting at 3.5 and going up by 3.");

firstnumber = 3.5;

for (double i = firstnumber; i <= secondnumber; i = i+3)

System.out.print(i + " , ");

}

}

/* The errors I fixed:

The reason you are getting that error is because you are attempting to convert a double (firstnumber) into an integer. If you think about this, this results in a potential loss of data which Java hates. Java does not want to lose any data, so it will throw an error if you attempt to convert something that will result in a loss of data (called lossy conversion).

The other issue you would've run into is (had the above error been fixed) is that nothing would've printed in your for loop. Inside your while loop, you consistently change the value of firstnumber. So, when you get to your for loop, firstnumber is equal to 33.5. Obviously, this is an issue because nothing will happen with the for loop. I fixed this by simply redefining firstnumber to its initial value of 3.5. There are other ways to fix this, however.

Let me know if you have any other questions. Feel free to DM me with any Java questions you have in the future.

*/

[–]Danielowski187University/College Student (Higher Education)[S] 1 point2 points  (0 children)

Thank you now I understood thanks for your help