Java 编写一个程序,读取未指定数量的整数
计算正数和负数并计算数字的平均值)编写一个程序,读取未指定数量的整数,确定读取了多少正数和负数,并计算输入值的总数和平均值(不计算零).程序以输入0结束。将平均值显示为浮点数。“ 我不知道我做错了什么Java 编写一个程序,读取未指定数量的整数,java,Java,计算正数和负数并计算数字的平均值)编写一个程序,读取未指定数量的整数,确定读取了多少正数和负数,并计算输入值的总数和平均值(不计算零).程序以输入0结束。将平均值显示为浮点数。“ 我不知道我做错了什么 import java.util.Scanner; public class NewClass { public static void main(String[] args) { Scanner input = new Scanner(System.in);
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;
double average;
System.out.println("Enter the number: ");
int number;
while ((number = input.nextInt()) != 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.printf("The average is %d ", average);
}
}
import java.util.Scanner;
公共类新类{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
整数正=0,负=0,总计=0,计数=0;
双倍平均;
System.out.println(“输入编号:”);
整数;
而((number=input.nextInt())!=0){
总数+=数量;
计数++;
如果(数字>0){
正++;
}否则如果(数字<0){
负++;
}
}
平均数=总数/计数;
System.out.println(“正数为”+正数);
System.out.println(“负片数为”+负片);
System.out.println(“总计为”+总计);
System.out.printf(“平均值为%d”,平均值);
}
}
首先:它应该是平均值=(双倍)总计/计数代码>因为int/int比您得到的是一个整数
第二:System.out.println(“平均值为”+平均值)代码>或系统输出打印f(“平均值为%f”,平均值)代码>首先:它应该是平均值=(双倍)总计/计数代码>因为int/int比您得到的是一个整数
第二:System.out.println(“平均值为”+平均值)代码>或系统输出打印f(“平均值为%f”,平均值)
如果需要数字的平均值,则不能将整数总数
除以整数计数
,因为结果将是一个整数,而不考虑小数点
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;
double average;
System.out.println("Enter the number: ");
int number;
while ((number = input.nextInt()) != 0) {
total += number;
count++;
if (number > 0) {
positive++;
} else if (number < 0) {
negative++;
}
}
average = (double) 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.printf("The average is: " + average);
}
}
import java.util.Scanner;
公共类新类{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
整数正=0,负=0,总计=0,计数=0;
双倍平均;
System.out.println(“输入编号:”);
整数;
而((number=input.nextInt())!=0){
总数+=数量;
计数++;
如果(数字>0){
正++;
}否则如果(数字<0){
负++;
}
}
平均值=(双)总数/计数;
System.out.println(“正数为”+正数);
System.out.println(“负片数为”+负片);
System.out.println(“总计为”+总计);
System.out.printf(“平均值为:“+平均值”);
}
}
另外,您不必在行System.out.printf中使用%d(“平均值为%d”,average)代码>
您可以编写System.out.printf(“平均值为:“+average”)
因为当您打印字符串时,括号内连接的任何内容也将转换为字符串,并按此方式打印出来。如果您需要数字的平均值,则不能将整数除以整数,因为结果将是整数,这不包括小数点
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;
double average;
System.out.println("Enter the number: ");
int number;
while ((number = input.nextInt()) != 0) {
total += number;
count++;
if (number > 0) {
positive++;
} else if (number < 0) {
negative++;
}
}
average = (double) 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.printf("The average is: " + average);
}
}
import java.util.Scanner;
公共类新类{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
整数正=0,负=0,总计=0,计数=0;
双倍平均;
System.out.println(“输入编号:”);
整数;
而((number=input.nextInt())!=0){
总数+=数量;
计数++;
如果(数字>0){
正++;
}否则如果(数字<0){
负++;
}
}
平均值=(双)总数/计数;
System.out.println(“正数为”+正数);
System.out.println(“负片数为”+负片);
System.out.println(“总计为”+总计);
System.out.printf(“平均值为:“+平均值”);
}
}
另外,您不必在行System.out.printf中使用%d(“平均值为%d”,average)代码>
您可以编写System.out.printf(“平均值为:“+average”)
因为当您打印出字符串时,括号内连接的任何内容也将转换为字符串,并按此方式打印,只需将int变量乘以1.0即可将其转换为浮点变量
average=1.0*总计/计数代码>
这应该可以。
您可以使用以下语句来显示该值
System.out.println(“数字的平均值为”+平均值)代码>只需将int变量乘以1.0即可将其转换为浮点变量
average=1.0*总计/计数代码>
这应该可以。
您可以使用以下语句来显示该值
System.out.println(“数字的平均值为”+平均值)代码>//扫描仪位于java.util包中
导入java.util.Scanner;
类计数
{
公共静态void main(字符串参数[])
{
//创建扫描仪对象
扫描仪输入=新扫描仪(System.in);
//提示用户输入数字
System.out.println(“输入+和-数字”);
System.out.println(“完成后输入0”);
//初始化变量
int n,countP,countN,count;
n=input.nextInt();
countP=0;
countN=0;
计数=0;
整数和=n;
浮动平均值=(浮动)总和/2;
而(n!=0)
{
n=input.nextInt();
计数++;
如果(n>=0)
countP++;
if(n<0)
countN++;
}
系统输出打印项次(“Tot