简体   繁体   English

如何计数和打印一次?

[英]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.

相关问题 我怎样才能只打印一次字符串? - how can i print a string only once? 如何仅从嵌套的 for 循环中打印一次结果 - How can I print a result from a nested for loop only once 如何检查两个字符串是否具有相同的字母,但只打印一次常见的字母? - How can I check if two strings have the same letters, but only print the common letters once? count [i]保持字符串中每个字符的频率,如何在Java中仅从中打印最大count [i] - count[i] holds frequency for each character from a string how can i print only max count[i] from it in java 如何在 while 循环中只打印一次语句? - How do I only print statement once in a while loop? 尽管我多次运行程序,但如何只打印pw.println的三个语句 - How can I print the three statements of pw.println only once although I run the program many times 如何只打印一次递归字符串值,然后计算它出现了多少次? - How to print a recursive string value only once and then count how many times it ocurred? Java- For循环,带有带默认情况的switch语句。 如何获得默认情况下仅打印输出一次? - Java- For loop with a switch statement containing default case. How can I get the default case to print the output only once? 我如何一次打印for循环HashMap结果? - how can I print the for loop HashMap result once? 如何只在字符串数组中打印重复项一次 - How to print duplicates in string array only once
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM