为什么我的总输出不正确? [英] Why is my total output incorrect?

查看:87
本文介绍了为什么我的总输出不正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这段代码下面我写了我需要的东西





Below this code i wrote what i need


public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		
		int positives = 0; //count numbers of positive numbers
		int negatives = 0; //count numbers of negative numbers
		int count = 0; //count all numbers
		double total = 0; //Accumulate total

		//prompt user to enter integer or 0 to exit
		System.out.printf("Enter an integer, the input ends if it is 0: ");
 
		
		int number = input.nextInt( );

	
		if (number == 0) {
		System.out.println("No numbers were entered except 0");
		
		System.exit(0);

	}

	
			while ( number != 0) {
		if ( number > 0)
			positives ++; //increase positives
		else
			negatives++; //increase negatives
		total += number; //total 
		count ++; // increase count
	  number = input.nextInt( );
	}

	// calculate average
	double average =  total / count;


			// Display results
				
				System.out.println("The number of positives is " + positives );
				System.out.println("The number of negatives is " + negatives );
				System.out.println("The total is " + (int) total);
				System.out.printf("Average is %,.2f\n", + average);





我尝试过:



读入整数,空格分隔(自由格式数据 - 空格是空格,制表符或换行符只需使用nextInt()),输入0时停止读取,输出正整数的数量,负整数,和和浮点平均值(类型double - 小数点后2位数)。您将首先提示用户输入一个字符串输入一个整数,输入结束,如果它是0:



(计算正数和负数并计算平均值编写一个读取未指定数量的整数的程序,确定读取了多少正值和负值,并计算输入值的总和平均值(不计算零)。程序以输入0结束。显示平均值为浮点数,使用标准的%,。2f格式说明符。

样本运行:



输入整数,输入结束,如果它是0:1

2

-1

9

0

积极数为3

底片数为1

总数为11

平均为2.75



当我运行这个我得到正确的答案,如样本运行,但当我提交到一个网站,我需要提交这个,它告诉我,我总产出是不合理的rrect。我很新,所以我希望有人可以帮助我。



What I have tried:

Read in integer numbers, white space separated (free form data - a white space is either space, tab or newline just use nextInt() ), stop reading when you input 0, you will output the number of positive integers, negative integers, sum and floating-point average (type double - 2 digits after decimal point). You will first prompt the user with the string "Enter an integer, the input ends if it is 0: "

"(Count positive and negative numbers and compute 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, using the standard "%,.2f" format specifier.
Sample run:

Enter an integer, the input ends if it is 0: 1
2
-1
9
0
The number of positives is 3
The number of negatives is 1
The total is 11
Average is 2.75

when i run this i get the correct answer like the sample run but when i go to submit this into a website i need to submit this in, it tells me that my total output is incorrect. im pretty NEW to this so i hope somebody can help me out.

推荐答案

Quote:

当我运行它时得到正确的数字,但当我提交此代码时,它说我的总输出不正确。

get the correct numbers when i run it, but when i go submit this code it says that my total output is incorrect.



你忘了告诉我们你提交代码的位置什么是要求。

没有这些,这对任何人来说都是猜谜游戏。



学会正确缩进你的代码,它显示它的结构有助于阅读和理解。它还有助于发现结构错误。


You forgot to tell us where you submit the code and what is the requirement.
Without those, it is a guessing game for anyone.

Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.

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

  int positives = 0; //count numbers of positive numbers
  int negatives = 0; //count numbers of negative numbers
  int count = 0; //count all numbers
  double total = 0; //Accumulate total

  //prompt user to enter integer or 0 to exit
  System.out.printf("Enter an integer, the input ends if it is 0: ");


  int number = input.nextInt( );


  if (number == 0) {
    System.out.println("No numbers were entered except 0");

    System.exit(0);

  }


  while ( number != 0) {
    if ( number > 0)
      positives ++; //increase positives
    else
      negatives++; //increase negatives
    total += number; //total
    count ++; // increase count
    number = input.nextInt( );
  }

  // calculate average
  double average =  total / count;


  // Display results

  System.out.println("The number of positives is " + positives );
  System.out.println("The number of negatives is " + negatives );
  System.out.println("The total is " + (int) total);
  System.out.printf("Average is %,.2f\n", + average);



专业程序员的编辑器具有此功能以及其他功能,例如括号匹配和语法突出显示。

Notepad ++ Home [ ^ ]

ultraedit [ ^ ]


这篇关于为什么我的总输出不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆