2 Replies - 1754 Views - Last Post: 11 December 2009 - 12:27 AM Rate Topic: -----

#1 outlawzero2099  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-December 09

Sum of numbers code problems

Posted 10 December 2009 - 11:49 PM

 import java.util.Scanner;   // needed for the scanner class.


	public class SumofAll
   {
	   public static void main(String[] args)
	  {
	  
		 int number;		// To use as a counter.
		 int maxValue;	// Upper limit of numbers.
		 int total = 0;	// accumulator.
	  
		   //create a scanner object to input variables.
		 Scanner keyboard = new Scanner(System.in);
	  
		 System.out.println("I will show you the sum of a set of " 
		 		+ "consecutive numbers.");
		 
		  System.out.print("Select a positive non-zero number: ");
		  
		  maxValue = keyboard.nextInt();  // records the user's input as an int.
		  
		while (maxValue <= 0)
	{
	System.out.print("That is not a valid number please try again ");
	maxValue = keyboard.nextInt();
				
	}
	  for (number = 1; number <= maxValue;)
	{
	number++;
				
	} 

				 				
		 System.out.println("The sum of all your numbers is: " + total);
		 
		 
	  
	  
	  }
   }



What I want this program to do is make a sum of numbers starting from the number 1. The user will input the ending number so say 5... the sum should be 15 well somehow it's not adding up for me. can somone please point me in the right direction.. I believe my error is in the latter part of the code where I have my for statement

This post has been edited by outlawzero2099: 10 December 2009 - 11:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Sum of numbers code problems

#2 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 285
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: Sum of numbers code problems

Posted 11 December 2009 - 12:17 AM

I have edited your code and show you where the error was, it was in the use of your for loop and the fact you never change the value of total - see the code, i made a comment in it

import java.util.Scanner;   // needed for the scanner class.


public class SumOfAll
{
      public static void main(String[] args)
      {
      
         int number;        // To use as a counter.
         int maxValue;    // Upper limit of numbers.
         int total = 0;    // accumulator.
      
           //create a scanner object to input variables.
         Scanner keyboard = new Scanner(System.in);
      
         System.out.println("I will show you the sum of a set of " 
                 + "consecutive numbers.");
         
          System.out.print("Select a positive non-zero number: ");
          
          maxValue = keyboard.nextInt();  // records the user's input as an int.
          
          while ( maxValue <= 0 )
	  {
    		System.out.print("That is not a valid number please try again ");
		maxValue = keyboard.nextInt();
                
    	  }
      
	  for (number = 1; number <= maxValue; number++ )
    	  {
    		total += number;	// added this line                
    	  } 

                                 
         System.out.println("The sum of all your numbers is: " + total);         
         
      
      
      }
}


Hope this helped :)
Was This Post Helpful? 0
  • +
  • -

#3 outlawzero2099  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-December 09

Re: Sum of numbers code problems

Posted 11 December 2009 - 12:27 AM

View Postbbq, on 10 Dec, 2009 - 11:17 PM, said:

I have edited your code and show you where the error was, it was in the use of your for loop and the fact you never change the value of total - see the code, i made a comment in it

import java.util.Scanner;   // needed for the scanner class.


public class SumOfAll
{
      public static void main(String[] args)
      {
      
         int number;        // To use as a counter.
         int maxValue;    // Upper limit of numbers.
         int total = 0;    // accumulator.
      
           //create a scanner object to input variables.
         Scanner keyboard = new Scanner(System.in);
      
         System.out.println("I will show you the sum of a set of " 
                 + "consecutive numbers.");
         
          System.out.print("Select a positive non-zero number: ");
          
          maxValue = keyboard.nextInt();  // records the user's input as an int.
          
          while ( maxValue <= 0 )
	  {
    		System.out.print("That is not a valid number please try again ");
		maxValue = keyboard.nextInt();
                
    	  }
      
	  for (number = 1; number <= maxValue; number++ )
    	  {
    		total += number;	// added this line                
    	  } 

                                 
         System.out.println("The sum of all your numbers is: " + total);         
         
      
      
      }
}


Hope this helped :)



MAN!! thanks I knew it was something small... I can't believe it.. cool thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1