(counting positive and negative numbers and computing the average of numbers)
write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). your program ends with the input 0 . Display the average as a floating-point number.
run is:
كود PHP:
Enter an int value, the program exits if the input is 0: 1 2 -1 3 0 The number of positives is 3 The number of negatives is 1 The total is 5 The average is 1.25
السلام عليكم
تكفون اللي يقدر يكتب خطوات البرامج
الاخراج موجود لكن محتاج ترتيب الخطوات .! 
|
تفضل هذا حل السؤال
كود PHP:
package javaapplication95;
import java.util.Scanner;
public class JavaApplication95 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter an int value, the program exits if the input is 0: ");
int n = in.nextInt();
int positives = 0, negatives=0 , count = 0;
double sum=0 , ave=0;
for (; ;) {
sum+=n;
count++;
if (n == 0)
break;
if (n > 0) {
positives ++;
}
else{
negatives++;
}
ave= sum/count;
System.out.print("Enter an int value, the program exits if the input is 0:");
n = in.nextInt();
}
System.out.println("The number of positives " + positives );
System.out.print("The number of negatives " + negatives );
System.out.println (" \n The total is " + sum );
System.out.println (" The average is " + ave );
}
}
إن شاء الله أكون ساعدتك