Help so stumped....

• October 28th, 2010, 03:58 AM
Macgrubber
Help so stumped....
i have this program.. basically it keeps reading an integer until the user inputs 0... then it outputs the count of positives and negatives, the sum of the postives and the negatives, largest values and lowest value..

basically everything is working all except the maximum negative value...
it always prints out 0 as an output.... could any1 help me? im a newbie so sorry for the shitty code.

Code Java:

```import java.util.Scanner; import java.io.*; public class lab31 {       public static void Numbers (String [] args) { Scanner input=new Scanner (System.in);   int sumnegatives = 0; int sumpositives = 0; int negatives = 0; int positives = 0; int number; int max = 0; int min = 0; int max2 = 0; int min2 = 0;     System.out.println("Arithmetic Fun"); System.out.println("Please enter series of numbers(to terminate enter 0)"); System.out.print("Enter an integer: "); number=input.nextInt(); if(number>0){ max=number; min=number; } if(number<0){ max2=number; min2=number; }   while (number != 0){   if (number > 0) { positives++; sumpositives += number;   if (number>max) max=number; if(number<min) min=number; }   else if (number < 0){ negatives++; sumnegatives += number;   if(number>max2) max2=number; if (number<min2) min2=number; }         System.out.print("Enter an integer: "); number=input.nextInt();   }//while loop   System.out.println("\nStatistics for positive values"); System.out.println("\nCount of positive numbers : " + positives); System.out.println("Sum of positive numbers : " + sumpositives); System.out.println("Maximum positive numbers : " + max); System.out.println("Minumum positive numbers : " + min); System.out.println("\nStatistics for negative values"); System.out.println("\nCount of negative numbers: " + negatives); System.out.println("Sum of negative numbers : " + sumnegatives); System.out.println("Maximum negative number : " + max2); System.out.println("Minumum negative number : " + min2);     } }```
• October 28th, 2010, 05:26 AM
JavaPF
Re: Help so stumped....
I have compiled this code.

It doesn't seem like the logic is quite right. Depending on the sequence you enter these numbers, the results differ.

As an example, if you enter just negative numbers, the 'Maximum negative number' works fine.
If you enter a negative number, then a positive number, the 'Minumum positive numbers' doesn't work.
• October 28th, 2010, 08:48 AM
copeg
Re: Help so stumped....
The code initializes the max and min to zero, which will restrict their settings. Typically the better way to initialize these values is to a) initialize them to the first value or b) initialize to Integer.MAX_VALUE (min) or Integer.MIN_VALUE (max)
• October 28th, 2010, 10:41 AM
Macgrubber
Re: Help so stumped....
Quote:

Originally Posted by JavaPF
I have compiled this code.

It doesn't seem like the logic is quite right. Depending on the sequence you enter these numbers, the results differ.

As an example, if you enter just negative numbers, the 'Maximum negative number' works fine.
If you enter a negative number, then a positive number, the 'Minumum positive numbers' doesn't work.

so can you tell me whats wrong? i cant seem to determine... thanks i didnt realize that problem now im even more confused.... :(
• October 28th, 2010, 10:54 AM
Macgrubber
Re: Help so stumped....
Quote:

Originally Posted by copeg
The code initializes the max and min to zero, which will restrict their settings. Typically the better way to initialize these values is to a) initialize them to the first value or b) initialize to Integer.MAX_VALUE (min) or Integer.MIN_VALUE (max)

their initialized to 0 because if the user enters the first number.. it checks the number if its <0 or >0 then it enters the loop...
• October 28th, 2010, 11:50 AM
copeg
Re: Help so stumped....
Quote:

Originally Posted by Macgrubber
their initialized to 0 because if the user enters the first number.. it checks the number if its <0 or >0 then it enters the loop...

Yes, but think about the logic: if the first number is positive, max2 is set to 0, which will always be great than any negative number entered (same goes for the alternative where the user enters a negative number first). Reread my post above carefully, and just as a test, try changing the initialization to
Code :

`int max2 = Integer.MIN_VALUE;`
and see what happens
• October 28th, 2010, 12:26 PM
Macgrubber
Re: Help so stumped....
it works if i the 1st number i enter is positive but if you enter a negative number 1st then it has problems :(
• October 28th, 2010, 01:40 PM
copeg
Re: Help so stumped....
Quote:

Originally Posted by Macgrubber
it works if i the 1st number i enter is positive but if you enter a negative number 1st then it has problems :(

One more time, read my posts above. Then think about the logical flow of the program - manually go through the flow of the program with some input, writing down how the values change, you will see that it is necessary to initialize a max to a min, and a min to a max.
• October 28th, 2010, 03:53 PM
Macgrubber
Re: Help so stumped....
o yeah thanks that was some big of a help.... :)