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
}

New Topic/Question
Reply




MultiQuote





|