Results 1 to 20 of 27
- 10-10-2012, 03:44 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
-
Re: I need help storing numbers in loops
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, 04:01 AM #3
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
Re: I need help storing numbers in loops
Ok well heres my code
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.Java 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); } }
- 10-10-2012, 04:13 AM #4
Member
- Join Date
- Oct 2012
- Posts
- 31
- Rep Power
- 0
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, 04:22 AM #5
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
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, 04:32 AM #6
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
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.
Java 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, 04:33 AM #7
Member
- Join Date
- Oct 2012
- Posts
- 31
- Rep Power
- 0
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
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 mindJava 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)); } } } }
p.s.s. What does scanner do?Can you please explain how to use itLast edited by Daryn; 10-10-2012 at 04:36 AM.
- 10-10-2012, 04:41 AM #8
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
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, 04:46 AM #9
Member
- Join Date
- Oct 2012
- Posts
- 31
- Rep Power
- 0
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, 04:47 AM #10
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
Re: I need help storing numbers in loops
Also i tried that, it dosent work
- 10-10-2012, 04:47 AM #11
Member
- Join Date
- Oct 2012
- Posts
- 31
- Rep Power
- 0
Re: I need help storing numbers in loops
Were you getting an Error message if So what is it
- 10-10-2012, 04:50 AM #12
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
Re: I need help storing numbers in loops
Its not an error message... here ill just show you
heres the outputJava 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)); } } } }
Enter Starting Value:
1
Enter Ending Value
4
the sum of the numbers,
The numbers in between 1 and 4 are
4
-
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, 05:03 AM #14
Member
- Join Date
- Oct 2012
- Posts
- 31
- Rep Power
- 0
Re: I need help storing numbers in loops
and your new and improved code is
alright so what I did was:Java 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)); } } } } }
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
-
Re: I need help storing numbers in loops
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.
-
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:
Java 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); } }Last edited by Fubarable; 10-10-2012 at 05:22 AM.
- 10-10-2012, 05:18 AM #17
Member
- Join Date
- Oct 2012
- Posts
- 31
- Rep Power
- 0
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
-
Re: I need help storing numbers in loops
- 10-10-2012, 05:28 AM #19
Member
- Join Date
- Oct 2012
- Posts
- 31
- Rep Power
- 0
-
Re: I need help storing numbers in loops
Yes, your code doesn't work.
To the original poster, I hate giving out solutions, but since you're getting bad advice, I feel I've no choice. In my code above where I state do something to sum here, replace with sum += i
sum += i is equivalent to sum = sum + i, so you'll add all the i values starting at the startingValue and going up to and including the endingValue.Java Code:for(int i = startingValue; i <= endingValue; i++) { sum += i; }
You can test it by using the famous Gaussian sum of all the numbers between 1 and 100 which should = 5050.Last edited by Fubarable; 10-10-2012 at 05:35 AM.
Similar Threads
-
help w/ storing/scanning numbers in arrays
By clemsontigers in forum New To JavaReplies: 2Last Post: 03-30-2011, 06:46 AM -
[Semi-Beginner] (nested loops) What's wrong with my code? (nested loops)
By Solarsonic in forum New To JavaReplies: 20Last Post: 03-22-2011, 04:02 AM -
help w/ storing/scanning numbers in arrays(what am i doing wrong)
By clemsontigers in forum New To JavaReplies: 4Last Post: 03-15-2011, 03:58 AM -
help w/ storing/scanning numbers in two dimensional arrays
By clemsontigers in forum New To JavaReplies: 15Last Post: 12-02-2010, 02:08 AM -
help w/ storing/scanning numbers in arrays
By clemsontigers in forum New To JavaReplies: 15Last Post: 11-18-2010, 05:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks