【发布时间】:2015-03-04 21:49:51
【问题描述】:
此 Java 程序用于从包含多个整数的 txt 文件中查找数据。我已经找出了运行程序的最大值、平均值的代码。对于很多人来说,这应该很简单,但我在编程方面相对缺乏经验,所以请耐心等待。
我遇到问题的三个代码如下。
- 高于平均水平的数字数量
- 素数个数
- 求总和
有谁知道这样做的方法吗? 谢谢。任何建议都会有所帮助。
这是20个数字的txt文件示例
- 1
- 12
- 34
- 54
- 36
- 76
- 67
- 86
- 45
- 44
- 33
- 22
- 2
- 4
- 7
- 87
- 89
- 99
- 432
- 543
列表项
package Assignment1;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Assignment1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Define two arrays - local variables
// Declaring 10 integers
int[] myIntArray = new int[20];
// Declaring 10 strings
//File
File f = new File ("inputassignment1.txt");
try {
Scanner sc = new Scanner(f); //Scanner
for (int i=0; i < myIntArray.length; i++) {
myIntArray[i] = sc.nextInt();
}
// Closing the file
sc.close();
} catch (IOException e) {
System.out.println("Unable to create : "+e.getMessage());
}
System.out.println("Highest number: " + maxNum(myIntArray));
System.out.println("Average number: "+aveNum(myIntArray));
// System.out.println(allNum(myIntArray));
// System.out.println(avgPlus(myIntArray));
}// end of main
【问题讨论】:
-
到目前为止你尝试过什么?我看到您正在阅读数字,但到目前为止您还没有尝试对它们做任何事情。
-
您问“有人知道这样做的方法吗?”在回答是的,我知道很多方法。但是你尝试了什么,你坚持什么
-
我算出最大数是: public static int maxum(int[] MaxNum) { int Max = 0; for (int i = 0; i Max) { Max = MaxNum[i]; } } 返回最大值; }
-
已编辑问题:删除了请求代码。来吧,这些都没有。你已经有答案了,好好努力吧。如果您不理解答案,请通过评论他们的答案来要求他们澄清。另外,请避免在 cmets 中发布代码,因为它不会正确格式化并且不会得到应有的关注。如果您需要向我们展示新代码(但不要要求其他人为您提供代码),请编辑您的问题。
-
对不起,小伙子们,我一开始并不清楚,正如我之前在描述中提到的那样。我在编程方面经验不是很好,尤其是在 Java 方面。
标签: java arrays algorithm text-files