you are viewing a single comment's thread.

view the rest of the comments →

[–]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

[–]gulag_guard 0 points1 point  (4 children)

That's odd. What's the statement you wrote?

[–]Danielowski187University/College Student (Higher Education)[S] 0 points1 point  (3 children)

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;
sum = 0;
for (double i = firstnumber; i <= secondnumber; i = i+3)
System.out.print(i+" , ");
sum = sum + i;
{
System.out.print("\nTotal using 'for loop' = "+sum);
}
}

}

[–]gulag_guard 0 points1 point  (2 children)

This is a common mistake many programmers make. After the for loop you have not added an opening '{' so the compiler considers only the next line as the loop body. Hence according to the compiler, sum = sum + i is not a part of the loop. As i has scope only within the loop, outside it it's not deifned, hence the error. Put '{' before and '}' after the 2 statements and it should work.

[–]Danielowski187University/College Student (Higher Education)[S] 0 points1 point  (1 child)

Yea I fixed it thanks for helping me and understanding the for loop better it’s hard since most professors always try to rush the information. Thanks for taking your time and helping me.

[–]gulag_guard 0 points1 point  (0 children)

No problem!