Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Kevin Ley
1,402 Pointsplease help with this beginner loop program for java
My code runs but I cannot get the sum of the numbers I input once the program runs. I am a beginner so if I can get help with this it would help me gain knowledge on how loops really work. THANK YOU public class SumOfNumbers { private int mNumber;
public SumOfNumbers(int number){
mNumber = number;
}
public void setNumber(int number) {
mNumber = number;
}
public int getNumber() {
return mNumber;
}
public int sumOfNumbers() {
int sum = 0;
do {
getNumber();
if(getNumber() < 0) {
break;
}
sum += getNumber();
} while (true);
return sum;
}
public String toString() {
String str = "Sum of numbers is :" +sumOfNumbers();
return str;
}
}
import java.util.Scanner; public class SumOfNumbersDemo {
public static void main(String[] args) {
int number = 0;
SumOfNumbers sum = new SumOfNumbers(number);
Scanner scan = new Scanner(System.in);
System.out.println("This Program will collect numbers and add them up. ");
System.out.println("Enter a nonzero integer. To stop input negative value: ");
sum.setNumber(scan.nextInt());
sum.sumOfNumbers();
System.out.print(sum.sumOfNumbers());
}
}
1 Answer
jcorum
71,830 PointsTo get things started, let's do something a bit simpler. We will still add up some numbers, but all in one program:
import java.util.Scanner;
public class SumOfNumbersDemo {
public static void main(String[] args) {
//create a Scanner object
Scanner scan = new Scanner(System.in);
//put a prompt to the user on the console
System.out.println("This Program will collect numbers and add them up. ");
System.out.println("Enter a nonzero integer. ");
//get the user's input, using the Scanner object scan
int limit = scan.nextInt();
//call the sumOfNumbers() method with the user's limit number, and print out the returned value
System.out.println(sumOfNumbers(limit));
}
//method that takes one parameter, a limit number
public static int sumOfNumbers(int limit) {
//initialize a variable to hold the sum
int sum = 0;
//loop through the integers from 0 to the number the user entered
for (int i = 0; i <= limit; i++) {
//add up the numbers - this is shorthand for sum = sum + i
sum += i;
}
return sum;
}
}
If you put this in a file named SumOfNumbersDemo, it will compile and run. It will ask the user for a number, and then it will print the sum of the numbers from 0 to that number, inclusive. It doesn't check to see if the user actually enters a number, or if that number is positive. So it needs a bit of work. But it's a start. Later, after you get this going, the next step would be to see if there's any need to create a class, like your SumOfNumbers, to replace the method.
Kevin Ley
1,402 PointsKevin Ley
1,402 Pointsthank you so much!!!!!