I need help storing numbers in loops

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-10-2012, 04:44 AM
MrPosotive
I need help storing numbers in loops
Ok guys i have this program i have to make for class where the user inputs 2 numbers and it tells you the numbers in between and then i have to find the sum of those numbers, can any of you help me figure this out.
• 10-10-2012, 04:48 AM
Fubarable
Re: I need help storing numbers in loops
Quote:

Originally Posted by MrPosotive
Ok guys i have this program i have to make for class where the user inputs 2 numbers and it tells you the numbers in between and then i have to find the sum of those numbers, can any of you help me figure this out.

Based on what you've posted so far about all I can say is for you to break the problem down into small steps and then try to solve each step one at a time. Please come on back with your code and a specific question if you get stuck at one of the small steps. Good luck.
• 10-10-2012, 05:01 AM
MrPosotive
Re: I need help storing numbers in loops
Ok well heres my code
Code:

```import java.io.*; import java.util.Scanner; public class Prog152d {         public static void main(String args[])         {                 Scanner a = new Scanner(System.in);         System.out.println("Enter Starting Value: ");         int aa = a.nextInt();                                 Scanner b = new Scanner(System.in);         System.out.println("Enter Ending Value");         int bb = b.nextInt();                 System.out.println("the sum of the numbers, ");                int g = 0;         {       for(g = aa; g >= bb;g++);         }                                System.out.println(g);                 System.out.println("is " + h);                                 } }```
Pretty much if you enter the two numbers 1 and 4 it needs to add the numbers 1,2,3,4 and im not sure how to do that. obviously the sum would be 10 but i dont know how to get it to add them up.
• 10-10-2012, 05:13 AM
Daryn
Re: I need help storing numbers in loops
I made this with a certain amount of simplicitymy only question is have you tried anything yet. I do not see the point if you have came here and to have other people write the code for them, but ill be positive and assume you tried some stuff.
Heres what I did:
1st set 2 textfieldS(for number 1 and two),a button(to perform it),and place for the answer
2nd have a loop with a variable(lets say g)that starts at one above your first number and goes up to one below the second number.Insid the loop have it print whats already in the answer area plus your variable(g)
3rd inside the loop put a if statement that says if your variable(g)is equal two one less then your second number. Than print the sum of number1 and number2.

hope i made it clear enough. I would of just put up the code but i dont want to take all the fun away from ya. Also if i wasnt clear enough on anything or you need some more help please just ask
• 10-10-2012, 05:22 AM
MrPosotive
Re: I need help storing numbers in loops
yes i did try some stuff but its not working and what do you mean "Print the sum of number1 and number2" is that the inputted numbers? What about the ones in between?
• 10-10-2012, 05:32 AM
MrPosotive
Re: I need help storing numbers in loops
Like right now ive got one thing together *did it before you posted* but for some reason it is just adding the first number plus the second number and its not even printing right, not to sure what im doing wrong but ok.
Code:

```  System.out.println("the sum of the numbers, ");                int g = 0;         int h = 0;         {       for(g = aa; g <= bb; g++);       {       System.out.println(g);               h = g + h;}               }```
• 10-10-2012, 05:33 AM
Daryn
Re: I need help storing numbers in loops
alright so i had to remake this to work with the console instead of a interface but heres my code

Code:

```//it says applet1 cause thats my file name that i keep saving over //you can name it what ever public class applet1 {     public static void main(String args[])     {         int b = 3;         int c = 5;;         for (int a = b+1;a<c;a++){                 //this states what your seeing         System.out.println("The numbers inbetween "+b+" and "+c+" are: ");                 //a will start at the first number entered plus one                 //and increase by one until it is one less then the second number                 System.out.println(a);                 //if it is done listing numbers                 if(a==c-1){                 //print sum                 System.out.println("The Sum is: "+(b+c));                 }                 }             } }```
ps.if the code looks familiar its because its your code from another thread were you wanted to make it count by three (so i helped you out there to) hope you dont mind

p.s.s. What does scanner do?Can you please explain how to use it
• 10-10-2012, 05:41 AM
MrPosotive
Re: I need help storing numbers in loops
Thanks, and scanner is input from a keyboard. you just click where it is running and type in like a number and it stores it as the variable.
• 10-10-2012, 05:46 AM
Daryn
Re: I need help storing numbers in loops
Alright thanks I sorta understand what it is now I just dont see the point of using it in when you can pull the number from a textfeild on the user interface
• 10-10-2012, 05:47 AM
MrPosotive
Re: I need help storing numbers in loops
Also i tried that, it dosent work
• 10-10-2012, 05:47 AM
Daryn
Re: I need help storing numbers in loops
Were you getting an Error message if So what is it
• 10-10-2012, 05:50 AM
MrPosotive
Re: I need help storing numbers in loops
Its not an error message... here ill just show you
Code:

``` import java.io.*; import java.util.Scanner; public class Prog152d {         public static void main(String args[])         {                 Scanner a = new Scanner(System.in);         System.out.println("Enter Starting Value: ");         int aa = a.nextInt();                                 Scanner b = new Scanner(System.in);         System.out.println("Enter Ending Value");         int bb = b.nextInt();                 System.out.println("the sum of the numbers, ");                int g = 0;         int h = 0;         {       for(g = aa + 1; g < bb; g++);       {       System.out.println("The numbers in between " + aa + " and " + bb + " are ");     System.out.println(g);                        }       if(g==bb-1)       {               System.out.println("The sum is: " + (aa+bb));       }               }              } }```
heres the output
Enter Starting Value:
1
Enter Ending Value
4
the sum of the numbers,
The numbers in between 1 and 4 are
4
• 10-10-2012, 06:02 AM
Fubarable
Re: I need help storing numbers in loops
For your sake and our sake, avoid using nonsense variable names. Use names that are self-commenting and that make sense.
• 10-10-2012, 06:03 AM
Daryn
Re: I need help storing numbers in loops
and your new and improved code is
Code:

```import java.io.*; import java.util.Scanner;   public class applet1 {     public static void main(String args[])     {         Scanner a = new Scanner(System.in);         System.out.println("Enter Starting Value: ");         int aa = a.nextInt();                         Scanner b = new Scanner(System.in);         System.out.println("Enter Ending Value");         int bb = b.nextInt();                 System.out.println("the sum of the numbers, "+(aa+bb));                   {       for(int g = aa + 1; g < bb; g++)       {       System.out.println("The numbers in between " + aa + " and " + bb + " are ");     System.out.println(g);                    if(g==bb-1)       {           System.out.println("The sum is: " + (aa+bb));       }           }                 } } }```
alright so what I did was:
1.noticed you never tried to add the numbers to get the sum so added that in the print line function on line 15.
2.dont know what h was for got rid of it, it just used up more memory and declared g in the for statement.
3.do not put; after the loop or it will not put the next stuff in it. instead use {} to contain what you want the loop to do
4.I believe that was it i just ran the program
5.I was completly mesmorized by being able to use scanner as i had always dreamed of something like that even if it is now almost useless to me
• 10-10-2012, 06:09 AM
Fubarable
Re: I need help storing numbers in loops
Quote:

Originally Posted by Daryn
and your new and improved code is...

Improved? Why are you using two different Scanner objects? Why the use of code blocks for no reason? Why the use of bizarre and meaningless variable names? Why the unusual code indentation. Why aa + 1? Where is your summation variable? Where are you adding to it?

I have to wonder if this help is really helping.
• 10-10-2012, 06:17 AM
Fubarable
Re: I need help storing numbers in loops
I'd recommend that your variables make sense. For instance the starting value int variable could be named "startingValue", and the end value, "endValue". The sum that you want to calculate could be called "sum". The code indentations should be consistent and all code on the same block should line up, making your code look something like:

Code:

```import java.util.Scanner; public class AddRange {   public static void main(String[] args) {       System.out.println("Add Range");             Scanner input = new Scanner(System.in);       System.out.print("Enter Starting Value: ");       int startingValue = input.nextInt();       System.out.print("Enter Ending Value: ");       int endingValue = input.nextInt();             int sum = 0;       for(int i = startingValue; i <= endingValue; i++) {         // do something to sum here!       }             System.out.println("Sum is: " + sum);   } }```
• 10-10-2012, 06:18 AM
Daryn
Re: I need help storing numbers in loops
all the things you mentioned i did not do I just took thier program found there errors and fixed it that is all
• 10-10-2012, 06:23 AM
Fubarable
Re: I need help storing numbers in loops
Quote:

Originally Posted by Daryn
all the things you mentioned i did not do I just took thier program found there errors and fixed it that is all

But you haven't even fixed the errors yet.
• 10-10-2012, 06:28 AM
Daryn
Re: I need help storing numbers in loops
Quote:

Originally Posted by Fubarable
But you haven't even fixed the errors yet.

yes i have, ive ran it and everyting.Have you
• 10-10-2012, 06:32 AM
Fubarable
Re: I need help storing numbers in loops
Quote:

Originally Posted by Daryn
yes i have, ive ran it and everyting.Have you

```      for(int i = startingValue; i <= endingValue; i++) {         sum += i;       }```