Results 1 to 2 of 2
- 11-15-2011, 02:12 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
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
many Thanks Andy.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); } }
- 11-15-2011, 03:20 PM #2
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
-
Limited word Dictionary
By Magic8ballfollower in forum New To JavaReplies: 6Last Post: 09-21-2011, 01:17 AM -
Advise.. What to do when you have too much limited time for a project
By Nazneen Ali in forum New To JavaReplies: 1Last Post: 08-06-2011, 02:17 AM -
jtable showing limited rows
By pink123 in forum AWT / SwingReplies: 6Last Post: 04-26-2011, 06:29 PM -
Limited seats to Open Source Enterprise search Tour
By petermatilda in forum Java SoftwareReplies: 0Last Post: 10-13-2010, 05:41 AM -
Creating many Sessions for limited USers in Hibernate
By SE123 in forum JDBCReplies: 1Last Post: 10-01-2009, 01:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks