• Post Reply Bookmark Topic Watch Topic
  • New Topic

simple program help  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my class we have been learning about loops. The problem is I have to fill-in the blanks to make this program display the sum of the integers 1 through 5.

public class Lab10A {
public static void main(String[] args) {
int ________________;
for (________________________________) {
total += n;
}
System.out.println("The sum of the digits 1 to 5 is " + total);
}
}

Now this is what I was working on

public class Lab10A {
public static void main(String[] args) {
int n = 0;
for (n = 1; n <= 15; n ++) {
total += n;
}
System.out.println("The sum of the digits 1 to 5 is " + total);
}
}

This is just after many tries. So I asked the teacher and he told me I need a "broad scope" because you will add to its value inside the loop and display its value after the loop ends(which I have no idea what he means by that). I can't figure out what "broad scope" is.

The problem still wont compile and keeps telling me there is a problem with (total on java line 5 and 7) and how it cannot resolve symbol.

Well I have been working on this program for like 3 hours and its been driving me nuts.

Thanks in advance
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem still wont compile and keeps telling me there is a problem with (total on java line 5 and 7) and how it cannot resolve symbol.


What the compiler is trying to tell you is -- you never declared the variable "total". You need to tell the compiler what kind of variable it is -- and to initialize it.

Henry
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one rule I have learned the hardway, make sure that you have the word int infront of your varaible in your look like so
.


without the int in the front it will not work. Also think about what the total line is doing. What is the n representing. Does it represent the total your trying to reach or the number of numbers you have to add together. I hope that helps If you need any clarification let me know.

Vicky
 
Ian Dudek
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"What the compiler is trying to tell you is -- you never declared the variable "total". You need to tell the compiler what kind of variable it is -- and to initialize it."


So... hmm I am just really confused on what to write in there. I can only work in the blanks. So do I need to write something like I have no idea.
public class Lab10A {
public static void main(String[] args) {
int n = 0;
for (n = 1; n <= 15; n ++) {
total += n;
}
System.out.println("The sum of the digits 1 to 5 is " + total);
}
}
Maybe n <=5. But this is not going to solve the problem with totals err this is driving me nuts.

I have been looking over all the examples and from what I see I cant find an example on what to write in there. All the examples that he gave to the class has not been helping with this problem.
 
Ian Dudek
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Lab10A {
public static void main(String[] args) {
int n = 0;
for (int n = 1; n <= 15; n ++) {
total += n;
}
System.out.println("The sum of the digits 1 to 5 is " + total);
}
}

now it says n is already defined. Maybe there is a problem with
for (int n = 1; n <= 15; n ++).
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you slmost have it. what you have the the first blank is whats casuing you problems. look at your sheet what varible on there has not been defined.
 
Ian Dudek
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Lab10A {
public static void main(String[] args) {
int total = 0;
for (int n = 1; n <= 5; n++) {
total += n;
}
System.out.println("The sum of the digits 1 to 5 is " + total);
}
}

Wow thanks a ton. If I can get it to compile than I can do some trial and error but I just could not do that this time.

Thanks again
Ian
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!