# The sum using for loop with break/continue statements

Page 1 of 1

## 5 Replies - 3681 Views - Last Post: 26 March 2009 - 04:28 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=95555&amp;s=2312b5703d801c9bb39724727c8aa6a0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 zwakili

Reputation: 0
• 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

• May Your Swords Stay Sharp

Reputation: 102
• 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?

### #3 zwakili

Reputation: 0
• 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.

### #4 zwakili

Reputation: 0
• 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

### #5 pbl

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

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

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

Posted 26 March 2009 - 04:24 PM

zwakili, 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 ???

why do you need that switch() statement ?

### #6 zwakili

Reputation: 0
• 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
}

```