[英]How can I count and print only once?
My file has 6 rows of integers 我的文件有6行整数
For example my file name is abc.txt it has following input 例如我的文件名为abc.txt,它具有以下输入
12 13
13 1000
12 -58
8 4
I added the inputs like this 12+13
, 13+1000
etc etc and took average of them 我添加了像这样的输入12+13
13+1000
等,并对它们取平均值
I want to count and print how many times the average was inside 0-10 that is more than 0 and less than10 我想计算并打印平均值在0-10内大于0且小于10的次数
From my example it should print more than 0 and less than 10 = 2 从我的示例中,它应该打印大于0且小于10 = 2
I intiated count = 0
at the beginning and run this code 我一开始就建议count = 0
并运行此代码
if(avg >=0 && avg<10){
count++;
System.out.println ("more than 0 and less than 10 = "+count);
}
But it prints 但它打印
more than 0 and less than 10 = 1
more than 0 and less than 10 = 2
How can I make it prints once so that the output will only be like 我怎样才能使它打印一次,以便输出只会像
more than 0 and less than 10 = 2
just put System.out.println ("more then 0 and less then 10 = "+count);
只需放入System.out.println ("more then 0 and less then 10 = "+count);
out of your looping code when reading the lines in the file. 读取文件中的行时出现循环代码。 For example: 例如:
......
Looping...
do count++ if avg >=0 && avg<10;
End Loop..
print it by System.out.println ("more then 0 and less then 10 = "+count);
Hope it helps and answered your question. 希望对您有所帮助,并回答了您的问题。
let me try and help u by listing some of the steps for ur problem. 让我尝试列出问题的一些步骤来帮助您。
First while scanning the numbers, put them into an array. 首先,在扫描数字时,将它们放入数组中。 While ur putting the numbers in the array , sum it up. 当您将数字放入数组中时,请将其相加。 This will help u to give the sum result at the end of it. 这将有助于u在最后给出总和结果。 After that take the average of the number by deviding with the array count.(This will help u save memory by preventing unnecessary variable creation. 之后,通过指定数组计数来取数字的平均值(这将有助于u通过避免不必要的变量创建来节省内存。
After that use the array and if else logic to achieve ur end result. 之后,使用数组以及其他逻辑来获得最终结果。
Also for the error that ur getting I guess ur printing it inside a loop. 也为您得到的错误我猜我们在循环内打印它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.