编写一个程序,读取一个未指定数量的整数,确定pos、neg、total、average
Write a program that reads an unspecified number of integers, determines pos, neg, total, average
(计算正数和负数并计算数字的平均值)写
读取未指定数量的整数的程序,确定有多少
已读取正值和负值,并计算总值和平均值
输入值(不包括零)。您的程序以输入 0 结束。显示
作为浮点数的平均值。这是一个示例 运行:
输入一个整数,为0则输入结束:1 2 -1 3 0
正数为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);
}
}
}
您需要阅读更多数字。您在循环之前读取了一个值。你可以做类似
的事情
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);
你应该只使用 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);
}
}
// 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);
}
}
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);
}
}
(计算正数和负数并计算数字的平均值)写 读取未指定数量的整数的程序,确定有多少 已读取正值和负值,并计算总值和平均值 输入值(不包括零)。您的程序以输入 0 结束。显示 作为浮点数的平均值。这是一个示例 运行:
输入一个整数,为0则输入结束:1 2 -1 3 0
正数为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);
}
}
}
您需要阅读更多数字。您在循环之前读取了一个值。你可以做类似
的事情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);
你应该只使用 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);
}
}
// 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);
}
}
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);
}
}