netbeans 编写一个程序,读取一个未指定数目的整数,确定正,负,总计,平均

mgdq6dx1  于 2023-01-20  发布在  其他
关注(0)|答案(4)|浏览(124)

(计算正数和负数,并计算数字的平均值)编写一个程序,读取未指定数量的整数,确定已读取的正数和负数的数量,并计算输入值的总和和平均值(不计算零)。程序以输入0结束。将平均值显示为浮点数。下面是一个示例运行:
输入一个整数,如果为0则结束输入:1 2
阳性数量为3
负数的个数为1
总数为5.0
平均值为1.25

**我有两个主要问题。1)我无法停止循环。2)即使我停止了,平均值也不足。使用上面的例子,我的平均值总是1.0,而不是应该的1.25。这就像程序总共阅读5个数字,而不是等于5的4个数字。**下面的代码中所看到的是我所能使用的全部:Java的基础知识...

import java.util.Scanner;

public class NewClass {
 public static void main(String[] args) {
 Scanner input = new Scanner(System.in);

     int positive = 0, negative = 0, total = 0, count = 0;

     float average;

     System.out.println("Enter the number: ");
     int number = input.nextInt();

     while(number != 0) {
        total += number;
        count++;

        if(number > 0){
        positive++;
        }

        else if(number < 0){
        negative++;
        }

     average = total / count;

     System.out.println("The number of positives is "+ positive);
     System.out.println("The number of negatives is "+ negative);
     System.out.println("The total is "+ total);
     System.out.println("The average is "+ average);
     }
   }
}
mu0hgdu0

mu0hgdu01#

你需要读取更多的数字。在循环之前读取一个值。你可以这样做

int number;
while((number = input.nextInt()) != 0) {
    total += number;
    count++;
    if(number > 0){
        positive++;
    } else if(number < 0) {
        negative++;
    }
} // <-- end loop body.
float average = total / (float) count; // <-- not integer math.
System.out.println("The number of positives is " + positive);
System.out.println("The number of negatives is " + negative);
System.out.println("The total is " + total);
System.out.println("The average is " + average);
pu3pd22g

pu3pd22g2#

你应该用do while。

import java.util.*;  
public class PosAndNeg   {   
public static void main(String [] args){  
Scanner input = new Scanner(System.in);  

int positive = 0, negative = 0, total = 0, count = 0;  
int number;
float average;

     System.out.println("Enter the number: ");
     do { number = input.nextInt();
     total += number;
        count++;  
           if(number > 0){
           positive++;
        }
             else if(number < 0){
            negative++;
        }
    }
     while(number != 0);

     average = total / count;

     System.out.println("The number of positives is "+ positive);
     System.out.println("The number of negatives is "+ negative);
     System.out.println("The total is "+ total);
     System.out.println("The average is "+ average);

    }

}

jgzswidk

jgzswidk3#

// use type conversion while evaluating average
import java.util.Scanner;
public class newClass{
public static void main(String[] args){
    int pos=0;
    int neg=0;
    int total=0;
Scanner sc= new Scanner(System.in);
System.out.println("enter the total numbers user want to enter");

int  i=sc.nextInt();
int [] num= new int[i];
System.out.println("Please enter number");
for (int j = 0; j < num.length; j++)
{   

    num[j] = sc.nextInt();
    if(num[j]>0)
        pos++;
    else
        neg++;
    total=total+num[j];

}
System.out.println(num.length);
double avg =(float)total/(num.length);
System.out.println("positive count="+pos+"negative count="+neg+" average ="+avg);
}
}
fcg9iug3

fcg9iug34#

import java.util.Scanner;
public class NewClass{
public static void main(String[] args) {
    int N=0,p=0,t=0;
 System.out.println("enter 
integers");
Scanner obj=new Scanner 
(System.in);
    int a=obj.nextInt();
    while (a!=0){
    t+=a;
    if (a<0){
    N++;
    }
    else if (a>0){
    p++;
    }
    int b=obj.nextInt();
    a=b;
    }
    System.out.println("the 
    number of negative 
    numbers is\t\t"+N);
    System.out.println("the 
    number of positive number 
    is\t\t"+p);
    float l=N+p;
    float average=(float) t/l;
    System.out.println("the total 
    is\t\t"+t);
    System.out.println("the 
    average is\t\t"+average);
  }
}

相关问题