Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2.32 Write a program that inputs five numbers and determines and prints the numb

ID: 3631468 • Letter: 2

Question

2.32 Write a program that inputs five numbers and determines and prints the number of negative
numbers input, the number of positive numbers input and the number of zeros input.

The answer :

1 // Exercise 2.32 Solution: Tally.java
2 // Program accepts five numbers as input and prints a tally of the
3 // number of negatives, positives and zeros.
4 import java.util.Scanner;
5
6
public class Tally
9 {
10 Scanner input = new Scanner( System.in );
11
12 int inputNumber;
13 int zeroTally;
14 int positiveTally;
15 int negativeTally;
16
17 // iniitialize counters
18 zeroTally = 0;
19 positiveTally = 0;
20 negativeTally = 0;
21
22 System.out.print( "Enter first integer: " ); // prompt for input
23 inputNumber = input.nextInt(); // read first number
24
25 if ( inputNumber == 0 )
26 zeroTally = zeroTally + 1;
27
28 if ( inputNumber < 0 )
29 negativeTally = negativeTally + 1;
30
31 if ( inputNumber > 0 )
32 positiveTally = positiveTally + 1;
33
34 System.out.print( "Enter second integer: " ); // prompt for input
35 inputNumber = input.nextInt(); // read second number
36
37 if ( inputNumber == 0 )
38 zeroTally = zeroTally + 1;
39
40 if ( inputNumber < 0 )
41 negativeTally = negativeTally + 1;
42
43 if ( inputNumber > 0 )
44 positiveTally = positiveTally + 1;
45
46 System.out.print( "Enter third integer: " ); // prompt for input
47 inputNumber = input.nextInt(); // read third number
48
49 if ( inputNumber == 0 )
50 zeroTally = zeroTally + 1;
51
52 if ( inputNumber < 0 )
53 negativeTally = negativeTally + 1;
54
55 if ( inputNumber > 0 )
56 positiveTally = positiveTally + 1;
57
58 System.out.print( "Enter fourth integer: " ); // prompt for input
59 inputNumber = input.nextInt(); // read fourth number
60
61 if ( inputNumber == 0 )
62 zeroTally = zeroTally + 1;
63
64 if ( inputNumber < 0 )
65 negativeTally = negativeTally + 1;
66
67 if ( inputNumber > 0 )
68 positiveTally = positiveTally + 1;
69
70 System.out.print( "Enter fifth integer: " ); // prompt for input
71 inputNumber = input.nextInt(); // read fifth number
72
73 if ( inputNumber == 0 )
74 zeroTally = zeroTally + 1;
75
76 if ( inputNumber < 0 )
77 negativeTally = negativeTally + 1;
78
79 if ( inputNumber > 0 )
80 positiveTally = positiveTally + 1;
81
82 // create a string describing the results
83 System.out.printf( " There are %d zeros ", zeroTally );
84 System.out.printf( "There are %d positive numbers ",
85 positiveTally );
86 System.out.printf( "There are %d negative numbers ",
87 negativeTally );
88 } // end main
89 } // end class Tally

Now my question , why always he add +1 ?

Explanation / Answer

please rate - thanks

 

adding plus 1 is like counting  5+1 =6

see the comments I've added to the code

this program inputs 5 numbers, and counts how many of them are 0, how many are positive (>0) and how many are negative (<0) and outputs the information

message me if you need more of an explanation

 

1 // Exercise 2.32 Solution: Tally.java
2 // Program accepts five numbers as input and prints a tally of the
3 // number of negatives, positives and zeros.
4 import java.util.Scanner;
5
6
public class Tally
9 {
10 Scanner input = new Scanner( System.in );
11
12 int inputNumber;
13 int zeroTally;
14 int positiveTally;
15 int negativeTally;
16
17 // iniitialize counters
18 zeroTally = 0;                                            //#of 0's = 0
19 positiveTally = 0;                                      // number of positive numbers is 0
20 negativeTally = 0;                                    //number of negative numbers = 0
21
22 System.out.print( "Enter first integer: " ); // prompt for input
23 inputNumber = input.nextInt(); // read first number
24
25 if ( inputNumber == 0 )                           //if the number that was input is 0
26 zeroTally = zeroTally + 1;                        //add 1 to the number of zeros there has been (count the 0's)
27
28 if ( inputNumber < 0 )                             //if the number that was input is <0 (negative)
29 negativeTally = negativeTally + 1;   //add 1 to the number of negative numbers there has been(count it)
30
31 if ( inputNumber > 0 )                                       //if the number that was input is >0 (positive)
32 positiveTally = positiveTally + 1;      //add 1 to the number of positive numbers there has been(count it)
33
34 System.out.print( "Enter second integer: " ); // prompt for input
35 inputNumber = input.nextInt(); // read second number
36
37 if ( inputNumber == 0 )                          //do the same for all the rest of the numbers
38 zeroTally = zeroTally + 1;
39
40 if ( inputNumber < 0 )
41 negativeTally = negativeTally + 1;
42
43 if ( inputNumber > 0 )
44 positiveTally = positiveTally + 1;
45
46 System.out.print( "Enter third integer: " ); // prompt for input
47 inputNumber = input.nextInt(); // read third number
48
49 if ( inputNumber == 0 )
50 zeroTally = zeroTally + 1;
51
52 if ( inputNumber < 0 )
53 negativeTally = negativeTally + 1;
54
55 if ( inputNumber > 0 )
56 positiveTally = positiveTally + 1;
57
58 System.out.print( "Enter fourth integer: " ); // prompt for input
59 inputNumber = input.nextInt(); // read fourth number
60
61 if ( inputNumber == 0 )
62 zeroTally = zeroTally + 1;
63
64 if ( inputNumber < 0 )
65 negativeTally = negativeTally + 1;
66
67 if ( inputNumber > 0 )
68 positiveTally = positiveTally + 1;
69
70 System.out.print( "Enter fifth integer: " ); // prompt for input
71 inputNumber = input.nextInt(); // read fifth number
72
73 if ( inputNumber == 0 )
74 zeroTally = zeroTally + 1;
75
76 if ( inputNumber < 0 )
77 negativeTally = negativeTally + 1;
78
79 if ( inputNumber > 0 )
80 positiveTally = positiveTally + 1;
81
82 // create a string describing the results
83 System.out.printf( " There are %d zeros ", zeroTally );
84 System.out.printf( "There are %d positive numbers ",
85 positiveTally );
86 System.out.printf( "There are %d negative numbers ",
87 negativeTally );
88 } // end main
89 } // end class Tally

Now my question , why always he add +1 ?