Kali ini saya akan membagikan tentang cara menghitung angka positif dan negatif pada pemrograman java, ..
Silahkan dicoba, . .
import java.util.Scanner;
public class CountPositiveNegative {
public static void main(String[] iriahus) {
int count = 0;
int positive = 0;
int negative = 0;
@SuppressWarnings("resource")
Scanner s = new Scanner(System.in);
System.out.print("Enter an integer (enter 0 to end): ");
int number = s.nextInt();
count++;
double total = number;
if (number > 0)
positive++;
if (number < 0)
negative++;
while (number != 0) {
System.out.print("Enter an integer (enter 0 to end): ");
number = s.nextInt();
count++;
total += number;
if (number > 0)
positive++;
if (number < 0)
negative++;
}
System.out.println("The number of positive is " + positive);
System.out.println("The number of negative is " + negative);
System.out.println("The number of data is " + (count - 1));
System.out.println("The total is " + total);
System.out.println("The average number is " + total / (count - 1));
}
}
Semoga bermanfaat, . .
0 Response to "Menghiting Nilai Positif dan Negatif"
Post a Comment