Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: I need help storing numbers in loops

    Quote Originally Posted by MrPosotive View Post
    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.

  3. #3
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: I need help storing numbers in loops

    Ok well heres my code
    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);
            	
            
    	}
    }
    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.

  4. #4
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default 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

  5. #5
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default 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?

  6. #6
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default 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;}
           
            }

  7. #7
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default 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

    Java 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
    Last edited by Daryn; 10-10-2012 at 04:36 AM.

  8. #8
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default 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.

  9. #9
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default 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
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: I need help storing numbers in loops

    Also i tried that, it dosent work

  11. #11
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default Re: I need help storing numbers in loops

    Were you getting an Error message if So what is it

  12. #12
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: I need help storing numbers in loops

    Its not an error message... here ill just show you
    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;
            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

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  14. #14
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default Re: I need help storing numbers in loops

    and your new and improved code is
    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));
           }
    	   }
               
          }
    }
    }
    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

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: I need help storing numbers in loops

    Quote Originally Posted by Daryn View Post
    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.

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  17. #17
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default 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

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: I need help storing numbers in loops

    Quote Originally Posted by Daryn View Post
    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.

  19. #19
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default Re: I need help storing numbers in loops

    Quote Originally Posted by Fubarable View Post
    But you haven't even fixed the errors yet.
    yes i have, ive ran it and everyting.Have you

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: I need help storing numbers in loops

    Quote Originally Posted by Daryn View Post
    yes i have, ive ran it and everyting.Have you
    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

    Java Code:
          for(int i = startingValue; i <= endingValue; i++) {
             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.

    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. help w/ storing/scanning numbers in arrays
    By clemsontigers in forum New To Java
    Replies: 2
    Last Post: 03-30-2011, 06:46 AM
  2. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  3. Replies: 4
    Last Post: 03-15-2011, 03:58 AM
  4. help w/ storing/scanning numbers in two dimensional arrays
    By clemsontigers in forum New To Java
    Replies: 15
    Last Post: 12-02-2010, 02:08 AM
  5. help w/ storing/scanning numbers in arrays
    By clemsontigers in forum New To Java
    Replies: 15
    Last Post: 11-18-2010, 05:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •