# how do i make the integer limited to 10

• 11-15-2011, 03:12 PM
andnlou2678
how do i make the integer limited to 10
my code allows you to enter integers but unlimited how can i restrict it to only ten

Code:

import java.util.*;
public class SumPositiveNegative
{

public static void main(String [] args)
{
//initialise variables
int counter=0; //used to count number of inputs by operator
int numbers;//the number keyed in by operator
int sumAll=0; //total of all numbers keyed in
int sumPos=0; //total positive number keyed in
int sumNeg=0; //total negative numbers keyed in
//initialise input display
Scanner keyIn = new Scanner (System.in);

//start of input code display on output
System.out.println("Enter 10 integers: ");
//while what is keyed in is less than 10 do this
while(counter<10){
numbers = keyIn.nextInt();//next number
counter++; //counter increased by 1
sumAll = sumAll + numbers; // add the number keyed in to sumAll

if(numbers > 0) //if positive number keyd in do this
sumPos = sumPos + numbers;//add the number keyed in to sumPos
else //if negative, <0, do this
sumNeg = sumNeg + numbers;//add the number keyed in to sumNeg
}

//output total: positive, negative and sum
System.out.println("Sum of the positive numbers: " + sumPos);
System.out.println("Sum of the negative numbers: " + sumNeg);
System.out.println("Sum of all the numbers: " + sumAll);
}
}

many Thanks Andy.
• 11-15-2011, 04:20 PM
KevinWorkman
Re: how do i make the integer limited to 10
It seems like you know what you have to do (in involves an if statement or a do/while loop of some kind, or just capping the value), but you're looking for a way around it. There's no way around it.