The sum using for loop with break/continue statements

Redesign my for loop where it can contribute any input number

Page 1 of 1

5 Replies - 2925 Views - Last Post: 26 March 2009 - 04:28 PM Rate Topic: -----

#1 zwakili  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-March 09

The sum using for loop with break/continue statements

Posted 26 March 2009 - 03:23 PM

I need help in redesigning my program where I enter 20 numbers:
2 3 1 -6 10 2 5 6 9 -10 9 2 3 2 1 -4 -6 7 2 3
The program only calculates the positive numbers and the sum of these positive numbers is 67.
The point I'm trying come across if i want to change my code where i can add any positve number like for example 583 84 21
In this program i can only use a basic for loop and break / continue statements.

import java.util.*;

public class SumPositiveNumbers
{
 static Scanner scan = new Scanner(System.in);
 
 public static void main(String[]args)
 {
 int N = 20;
 int number;
 int pnum = 0;
 int negnum = 0;
 int sum = 0;
 
 System.out.println("This program asks the user to enter 20 numbers, \n"
 + "adds the positive numbers, and displays the totals.");
 
 System.out.print("Please enter " + N + " numbers; seperate each with a space: ");
 
 for (int counter = 1; counter <= N; counter++)
  {
	 
	  number = scan.nextInt(); 
				   
	 switch (number)
	 {
	 case 0 :
	  case 1 :
	  case 2 :
	  case 3 :
	  case 4 :
	  case 5 :
	  case 6 :
	  case 7 :
	  case 8 :
	  case 9 :
	  case 10 :
			 if (number > 0)
				 sum = sum + number;
			 break;
	 case -1:
	  case -2:
	  case -3:
	  case -4:
	  case -5:
	  case -6:
	  case -7:
	  case -8:
	  case -9:
	  case -10:
	  if ( number < 0)
	  negnum++;
	 } //end switch
  } //end for loop
  System.out.println("The sum of the positive numbers is: " + sum);
  } //end main
} 



Is This A Good Question/Topic? 0
  • +

Replies To: The sum using for loop with break/continue statements

#2 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: The sum using for loop with break/continue statements

Posted 26 March 2009 - 03:34 PM

So you want to skip all negative numbers and only add postive, just a simple if checkwill be sufficient in checking if the number is positive or negative, no need for switch or anything, also here:

Quote

case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
case 7 :
case 8 :
case 9 :
case 10 :
if (number > 0)
sum = sum + number;
break;

Why do an if check when all the cases you have are positive, and adding zero will have no effect anyways?
Was This Post Helpful? 1
  • +
  • -

#3 zwakili  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-March 09

Re: The sum using for loop with break/continue statements

Posted 26 March 2009 - 03:47 PM

The problem that I'm trying to figure out is if there is a way to change the case numbers where it can be any input number instead where my code ranges from -10 to 10.

The program that i tried to design was where i input the numbers with spaces between each number and the scanner class would scan the number. Then it would determine which number would classified a positve or negative number and sort the positive number to sum up a total.
Was This Post Helpful? 0
  • +
  • -

#4 zwakili  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-March 09

Re: The sum using for loop with break/continue statements

Posted 26 March 2009 - 04:09 PM

ok i solved my problem thanks
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: The sum using for loop with break/continue statements

Posted 26 March 2009 - 04:24 PM

View Postzwakili, on 26 Mar, 2009 - 02:47 PM, said:

The problem that I'm trying to figure out is if there is a way to change the case numbers where it can be any input number instead where my code ranges from -10 to 10.

The program that i tried to design was where i input the numbers with spaces between each number and the scanner class would scan the number. Then it would determine which number would classified a positve or negative number and sort the positive number to sum up a total.

What ??? :blink: :blink: :blink:

if(number > 0) add it
why do you need that switch() statement ?
Was This Post Helpful? 1
  • +
  • -

#6 zwakili  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-March 09

Re: The sum using for loop with break/continue statements

Posted 26 March 2009 - 04:28 PM

I solved my problem by taking out the switch statements.
import java.util.*;

public class Sum
{
 static Scanner console = new Scanner(System.in);
 static final int N = 20;
 
 public static void main(String[]args)
 {
 int number;
 int sum = 0;
 
 System.out.println("This program asks the user to enter 20 numbers, \n"
 + "adds the positive numbers, and displays the totals.");
 
 System.out.print("Please enter " + N + " numbers; seperate each with a space: ");
 
 for (int w = 1; w <= N; w++)
  {
	 
	  number = console.nextInt(); 
	  
			 if (number > 0)
				{ 
					  sum = sum + number;
			 }
				 continue;
  } //end for loop
   System.out.println("The sum of the positive numbers is: " + sum);

  }//end main 
  } 


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1