First off, im a beginner at Java. Im taking a class where we are currently learning about If-statements and While-statements, and theyre tripping me up a little bit. Any help would be appreciated.
Here is the desired output for what were doing...
Enter 10 whole numbers: 0 2 -10 21 0 8 -5 5 -3 -2
Sum of the positive numbers: 36
Sum of the negative numbers: -20
Sum of all the numbers: 16
Probably simple stuff for you guys, but heres whats causing me problems...We need to input the "10 whole numbers" after running the program, but I dont know how I can put just 10 numbers and have the program go to the next step, which is finding the sum.
Currently, there is no limit to the numbers I can put in there. I think I have a decent understanding of if statements, so I believe I can figure out the rest once I can just get how to put in just 10 numbers and have the program continue.
Thanks in an advance to anyone who can help me out a little bit.
Comments
i found this great post on how to accept input through the console:
http://www.coderanch.com/t/386991/Java-General-beginner/java/reading-integers-using-System
-- the one who posted this is really good (i also don't know this..)
well hope this helps.. good luck..
start of the program..
import java.io.*;
public class inputTenNumbers{
public static void main(String [] args) throws IOException
{
int counter=0;
int sumOfAllInput=0, sumOfPositive=0, sumOfNegative=0;
while(counter<10){
//start of input code
System.out.println("Please Enter a Number (No: " + (counter+1) + "): ");
BufferedReader consoleIn =
new BufferedReader(new InputStreamReader(System.in));
String stringInt = consoleIn.readLine();
int fromConsoleInput = Integer.parseInt(stringInt);
// the variable frmConsoleInput hold the integer value of input
// which is converted to integer because the actual input from
// the console is actually string/characters
//end of input code
counter++;
sumOfAllInput = sumOfAllInput + fromConsoleInput;
if(fromConsoleInput > 0) //sum of positive numbers
sumOfPositive = sumOfPositive + fromConsoleInput;
else //sum of negative numbers
sumOfNegative = sumOfNegative + fromConsoleInput;
}
System.out.println("Sum of all positive numbers: " + sumOfPositive);
System.out.println("Sum of all negative numbers: " + sumOfNegative);
System.out.println("Sum of all numbers: " + sumOfAllInput);
}
}
Like I said, im about as much of a beginner as it gets. I use BlueJ, since that is what we use in class. The numbers are supposed to be inputted all at once on the same line, then the program will get the sums following the 10th number.
I notice you did it a little differently, but it helped me enough to finish the assignment. I had problems figuring out what exactly the while loop did with the 'counter'. Once I understood how to separate each number that I input the rest was cake.
However, one more question regarding this assignment. What if I wanted to modify the program to also find averages of positive, negative and all the numbers? How would that work with the if and else statements?
Thanks again, Im sure Ill have more questions about future assignments that Ill have to post here
how to get the average..
new variables
- posCtr = 0; //counter on how many positive numbers entered
- negCtr = 0; // negative
if(fromConsoleInput > 0){ //sum of positive numbers
sumOfPositive = sumOfPositive + fromConsoleInput;
[color=Green]posCtr++;[/color]
}else{ //sum of negative numbers
sumOfNegative = sumOfNegative + fromConsoleInput;
[color=Green]negCtr++;[/color]
}
...
...
at the end of the loop then you can divide the total of the positive Numbers to the number of positive numbers entered (using the variable posCtr), same with the negative numbers and also with all the numbers use the variable holding the total of all the numbers and divide by ten..
ex: System.out.println("Average of Positive Numbers: " sumAllPositive / posCtr);
but can you tell me how did you accept 10 inputs in one line? so that I have something new to learn.. :-)
and by the way.. i'm not an math expert.. but how would you handle an input of zero.. would it fall in the positive or negative?? he he.. you should revise the if condition depending on how you would handle zero..
until next time..
import java.util.*;
public class test7{
public static void main(String [] args)
{
int counter=0, numbers;
int sumAll=0, sumPos=0, sumNeg=0;
Scanner keyIn = new Scanner (System.in);
//start of input code
System.out.println("Enter 10 whole numbers: ");
while(counter<10){
numbers = keyIn.nextInt();
counter++;
sumAll = sumAll + numbers;
if(numbers > 0)
sumPos = sumPos + numbers;
else
sumNeg = sumNeg + numbers;
}
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);
}
}
hello friends
why is the output of System.out.println(010); is 8 not 10
what is the difference between int and INTEGER.
Use array to store 10 integers.
Example:
int a[]=new int[10]; // array declaration
a[]={0, 2 ,-10, 21, 0, 8 ,-5, 5, -3, -2}
--After that use if else as follows:
for(int i=0;i<a.length;i++)
{
if( a[i]>=0)
{
sum_pos+=a[i]; // for adding +ve nos
}
else if(a[i]<0)
{
sum_neg+=a[i]; // for adding -ve nos
}
}
System.out.println("Sum of the positive numbers:"+sum_pos);
System.out.println("Sum of the negative numbers:"+sum_neg);
System.out.println("Sum of all the numbers:"+(sum_pos+sum_neg);
For more Details Refer following link:
http://java.meritcampus.com/t/32/Array---declaration--creation?tc=mm233