HW4.1

 1 import java.util.Scanner;
 2
 3 public class Solution
 4 {
 5     public static void main(String[] args)
 6     {
 7         Scanner input = new Scanner(System.in);
 8
 9         System.out.print("Enter an int value, the program exits if the input is 0: ");
10         int inputValue = input.nextInt();
11         int count = 1;
12
13         int positive = 0;
14         int negative = 0;
15         if(inputValue > 0)
16             positive = 1;
17         else if(inputValue < 0)
18             negative = 1;
19
20         int sum = inputValue;
21
22         while(inputValue != 0)
23         {
24             System.out.print("Enter an int value, the program exits if the input is 0: ");
25             inputValue = input.nextInt();
26             count++;
27             sum += inputValue;
28             if(inputValue > 0)
29                 positive++;
30             else if(inputValue < 0)
31                 negative++;
32         }
33
34         float average = (float)(sum * 1.0) / count;
35
36         System.out.println("The number of positives is " + positive);
37         System.out.println("The number of negatives is " + negative);
38         System.out.println("The total is " + count);
39         System.out.println("The average is " + average);
40     }
41 }
时间: 2024-10-07 17:12:38

HW4.1的相关文章

Stats 102A HW4

Stats 102A HW4 Due March 3, 2020General Notes• You will submit a minimum of three files, the core files must conform to the followingnaming conventions (including capitalization and underscores). 123456789 is a placeholder,please replace these nine d

HW4.30

1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 9 System.out.print("Enter the balance amount: "); 10 double balance = input.nextDouble(); 11 Syste

HW4.28

1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 9 System.out.print("Enter the year: "); 10 int year = input.nextInt(); 11 System.out.print("E

HW4.3

1 public class Solution 2 { 3 public static void main(String[] args) 4 { 5 final double POUND_PER_KILOGRAM = 2.2; 6 7 System.out.println("KILOGRAM" + "\t" + "POUND"); 8 for(int i = 1; i < 200; i += 2) 9 System.out.println(

HW4.32

1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 System.out.print("Enter your lottery pick (two digits): "); 9 int guess = input.nextInt(); 10 11 i

HW4.31

1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 9 System.out.print("Enter the balance: "); 10 double balance = input.nextDouble(); 11 System.out.p

HW4.19

1 public class Solution 2 { 3 public static void main(String[] args) 4 { 5 for(int i = 1; i <= 8; i++) 6 { 7 for(int j = 8 - i; j > 0; j--) 8 System.out.print(" "); 9 for(int j = 1; j <= i; j++) 10 System.out.print((int)(Math.pow(2, j -

HW4.6

1 public class Solution 2 { 3 public static void main(String[] args) 4 { 5 final double MILES_PER_KILOGRAM = 1.609; 6 7 System.out.println("Miles" + "\t" + "Kilograms" + "\t" + "Kilograms" + "\t"

HW4.2

1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 int correctCount = 0; 9 int answer = 0; 10 int number1; 11 int number2; 12 13 long startTime = System.c