Reputation: 5
In this program you (the user) keeps entering numbers until you enter a zero, which is when the list terminates & you get the sum of positive even & odd and negative numbers. I have tried my best in completing it, but the problem is that both http://ideone.com/ and DrJava hang when I try to run them. But they compile fine. Here's my program:
/**
*@author DarkIceDragon
*/
import java.util.*;
class huge_addition
{
public static void main (String[] args)
{
Scanner sc = new Scanner (System.in);
System.out.println ("Enter numbers. List terminates when you enter a zero. Enter a zero when you want to begin the addition.");
int a = sc.nextInt();
int esum=0;
int osum=0;
int nsum=0;
while (a !=0)
{
if (a>0)
{
if (a%2==0)
{
esum = esum+a;
}// end of 3rd innermost if statement
else
{
osum = osum+a;
}// end of 3rd else statement
}//end of 2nd middle if-else-loop
else if (a<0)
{
nsum=nsum+a;
}//end of 2nd middle else statement
}//end of while loop
System.out.println ("The sum of even positive numbers is "+esum);
System.out.println ("The sum of odd positive numbers is "+osum);
System.out.println ("The sum of negative numbers is "+nsum);
}//end of main
}//end of class
I'll admit that its for school, but I've completed all the rest by myself (there were around 16 or so), its 12:00AM at night and I've been trying to get this program to work for over an hour. And I'm still a complete novice (although noob would be more appropriate) in Java, so I just now only the basic commands and such. Heck, until today, I still used void main()
instead of public static void main(String[] args)
in my programs and spent 2 hours wondering why they weren't running on NetBeans. Too bad BlueJ stopped working for me.
Any help help would be appreciated greatly. Thanks for looking & Have a great day!
Upvotes: 0
Views: 2295
Reputation: 3566
You should accept the number each time in your while loop using sc.nextInt()
, and what you were doing was just going inside the while loop, and doing nothing.Accept the number every time, and based on that number, check if it is even or odd,and then add.
Here is the corrected code:
import java.util.*;
class huge_addition
{
public static void main (String[] args)
{
Scanner sc = new Scanner (System.in);
System.out.println ("Enter numbers. List terminates when you enter a zero. Enter a zero when you want to begin the addition.");
// no need ->> int a = sc.nextInt();
int num=-1;
int esum=0;
int osum=0;
int nsum=0;
while (num !=0)
{
System.out.println("enter the number");
num= sc.nextInt();
if (num%2==0)
{
esum = esum+num;
}// end of 3rd innermost if statement
else
{
osum = osum+num;
}// end of 3rd else statement
//end of 2nd middle if-else-loop
if (num<0)
{
nsum=nsum+num;
}//end of 2nd middle else statement
}//end of while loop
System.out.println ("The sum of even positive numbers is "+esum);
System.out.println ("The sum of odd positive numbers is "+osum);
System.out.println ("The sum of negative numbers is "+nsum);
}//end of main
}//end of class
Upvotes: 0
Reputation: 19
Because this is your school homework I will only answer a hint: examine the position of sc.nextInt() in your code. Second hint: try to enter number zero as the first input when running the code.
Upvotes: 1