Results 1 to 2 of 2
  1. #1
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default 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

    Java 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.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default 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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Limited word Dictionary
    By Magic8ballfollower in forum New To Java
    Replies: 6
    Last Post: 09-21-2011, 02:17 AM
  2. Replies: 1
    Last Post: 08-06-2011, 03:17 AM
  3. jtable showing limited rows
    By pink123 in forum AWT / Swing
    Replies: 6
    Last Post: 04-26-2011, 07:29 PM
  4. Limited seats to Open Source Enterprise search Tour
    By petermatilda in forum Java Software
    Replies: 0
    Last Post: 10-13-2010, 06:41 AM
  5. Replies: 1
    Last Post: 10-01-2009, 02:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •