gpt4 book ai didi

将文本文件的内容放入数组然后显示和操作数组内容的 Java 问题

转载 作者:行者123 更新时间:2023-12-02 04:12:34 27 4
gpt4 key购买 nike

好吧,这是一个学校项目,我似乎无法弄清楚我做错了什么,我意识到有更好的方法可以做很多事情,但必须以这种方式完成(我会注意到具体是什么)。

我需要编写两个非常简单的程序。

  1. 第一个程序创建一个包含 300 个 1 到 54 范围内的随机整数的文件,然后打印到文本文件中。

  2. 第二个程序读取该文本文件并将内容放入数组中。然后操作该数组以输出一些内容:数字的平均值以及数字在特定范围内出现的次数。我还尝试立即输出整个数组。不幸的是,所有部分基本上都不起作用,或者我不理解某些事情,但我很确定这都是哈哈。

方案1如下

import java.util.*;
import java.io.*;
public class OutStream{
public static void main(String[] args){
int Counter = 0,
Value;
Random Gen = new Random();
System.out.println("Generating Numbers and Text File Now");
try{
File out = new File("F:\\Text.txt");
PrintWriter P = new PrintWriter(out);
while(Counter <= 300){
Value = Gen.nextInt(54)+1;
P.println(Value);
++Counter;
System.out.println("Value: " + Value);
}
P.close();
}catch(Exception E){
E.printStackTrace();
System.out.println(E.getMessage());
}
System.out.println("The Program has ended");
}
}

第二个程序是一切崩溃的地方。

import java.util.*;
import java.io.*;
public class InStream {

public static void main(String[] args) {
int [] NumArray = new int[301];
int PosNum = 0,
Total = 0,
Average = 0,
Range1 = 0,
Range2 = 0,
Range3 = 0,
Range4 = 0,
Range5 = 0,
Range6 = 0;

try{
File Fout = new File("F:\\Text.txt");
PosNum = 0;
Scanner Fin = new Scanner(Fout);
while(Fin.hasNextInt()){
NumArray[++PosNum] = Fin.nextInt();


}
Fin.close();

}catch(Exception E){
E.printStackTrace();
System.out.println("***ERROR ***** " + E.getMessage());
}


for(NumArray[PosNum] = 0; NumArray[PosNum] <= 301;){
Total += NumArray[PosNum++];

if(NumArray[Range1] >= 1 && NumArray[Range1] <= 10){
Range1++;
}

if (NumArray[Range2] >= 11 && NumArray[Range2] <= 20){
Range2++;
}
if(NumArray[Range3] >= 21 && NumArray[Range3] <= 30){
Range3++;
}
if(NumArray[Range4] >= 31 && NumArray[Range4] <= 40){
Range4++;
}
if(NumArray[Range5] >= 41 && NumArray[Range5] <= 50){
Range5++;
}
if(NumArray[Range6] >= 51 && NumArray[Range6] <= 54){
Range6++;
}

}
Average = Total / 301;
System.out.println("The Average Is: " + Average);
System.out.println("1 Through 10 appear: " + Range1 + " Times");
System.out.println(" 11 Through 20 appear: " + Range2 + "Times");
System.out.println("21 Through 30 appear: " + Range3 + " Times");
System.out.println("31 Through 40 appear: " + Range4 + " Times");
System.out.println("41 Through 50 appear: " + Range5 + " Times");
System.out.println("51 Through 54 appear: " + Range6 + " Times");
}

}

这是我最新的尝试。

import java.util.*;
import java.io.*;
public class FixedInstream {

public static void main(String[] args) {
int [] MyArray = new int[301];
int PosNum = 0,
Average,
Total = 0,
R1 = 0,
R2 = 0,
R3 = 0,
R4 = 0,
R5 = 0,
R6 = 0,
R7 = 0;

try{
File Fout = new File("F:\\Text.txt");
PosNum = 0;
Scanner Fin = new Scanner(Fout);
while(Fin.hasNextInt()){
MyArray[PosNum] = Fin.nextInt();
Total += MyArray[PosNum];
}
Fin.close();

}catch(Exception E){
E.printStackTrace();
System.out.println("***ERROR ***** " + E.getMessage());
}
for(MyArray[PosNum] = 0; MyArray[PosNum] <= 300; ++MyArray[PosNum]){


if(MyArray[R1] >= 1 && MyArray[R1] <= 10){
R1++;
}

if (MyArray[R2] >= 11 && MyArray[R2] <= 20){
R2++;
}
if(MyArray[R3] >= 21 && MyArray[R3] <= 30){
R3++;
}
if(MyArray[R4] >= 31 && MyArray[R4] <= 40){
R4++;
}
if(MyArray[R5] >= 41 && MyArray[R5] <= 50){
R5++;
}
if(MyArray[R6] >= 51 && MyArray[R6] <= 54){
R6++;
}
}

Average = Total / 301;
System.out.println("The Average Is: " + Average);
System.out.println("1 Through 10 appear: " + R1 + " Times");
System.out.println("11 Through 20 appear: " + R2 + " Times");
System.out.println("21 Through 30 appear: " + R3 + " Times");
System.out.println("31 Through 40 appear: " + R4 + " Times");
System.out.println("41 Through 50 appear: " + R5 + " Times");
System.out.println("T51 Through 54 appear: " + R6 + " Times");
}

}

程序未正确输出下面的示例,我显然忘记在最初的评论中添加。

输出:

The Average Is: 27
The Numbers 1 Through 10 appear: 1 Times
The Numbers 11 Through 20 appear: 1 Times
The Numbers 21 Through 30 appear: 1 Times
The Numbers 31 Through 40 appear: 1 Times
The Numbers 41 Through 50 appear: 1 Times
The Numbers 51 Through 54 appear: 1 Times

文本文件 1 - 54 中有 300 个数字,这些数字重复的方式不同,但通常重复 10 - 50 次,正如您从上面的输出中看到的那样,它只显示一次,这让我相信我没有增加某些东西正确。

最佳答案

我认为这里有很多令人困惑的事情...下面是答案,但我建议您尝试自己调试此代码片段以了解问题所在。

  1. 您正在使用 301 迭代数组细胞。数组索引从0开始至length-1 。当您使用 numArray[++posNum] 进行迭代时当 posNum 初始化为 0 时,第一个索引将为 ++0 = 1 。所以你永远不会到达数组的第一个单元格。这导致 out of range exception因为最后一个索引将是 301但实际上最后一个索引是 300 ...只需阅读异常消息即可找到解决方案。

  2. 我建议您看看 for loop 的工作原理了解如何使用递增索引逐单元浏览数组。然后你将像这样纠正你的 for 循环:

    for(int idx = 0; idx < NumArray.length; idx++)

  3. 您使用错误的变量访问当前单元格。这可以附加,但您会在 Debug模式下非常快地检测到它...

    NumArray[idx]

而不是

NumArray[Range1]
  • 这对于让你的程序正常工作并不是必需的,但是在 Java 中有一些约定(就像在每种语言中一样)和 variables写在 camelCase .

    公共(public)类 InStream {

    public static void main(String[] args) {
    int [] NumArray = new int[301];
    int PosNum = 0,
    Total = 0,
    Average = 0,
    Range1 = 0,
    Range2 = 0,
    Range3 = 0,
    Range4 = 0,
    Range5 = 0,
    Range6 = 0;

    try{
    File Fout = new File("F:\\Text.txt");
    PosNum = 0;
    Scanner Fin = new Scanner(Fout);
    while(Fin.hasNextInt()){
    NumArray[PosNum++] = Fin.nextInt();
    }
    Fin.close();

    }catch(Exception E){
    E.printStackTrace();
    System.out.println("***ERROR ***** " + E.getMessage());
    }


    for(int idx = 0; idx < NumArray.length; idx++){
    Total += NumArray[idx];

    if(NumArray[idx] >= 1 && NumArray[idx] <= 10){
    Range1++;
    }

    if (NumArray[idx] >= 11 && NumArray[idx] <= 20){
    Range2++;
    }
    if(NumArray[idx] >= 21 && NumArray[idx] <= 30){
    Range3++;
    }
    if(NumArray[idx] >= 31 && NumArray[idx] <= 40){
    Range4++;
    }
    if(NumArray[idx] >= 41 && NumArray[idx] <= 50){
    Range5++;
    }
    if(NumArray[idx] >= 51 && NumArray[idx] <= 54){
    Range6++;
    }
    }
    Average = Total / 301;
    System.out.println("The Average Is: " + Average);
    System.out.println(" 1 Through 10 appear: " + Range1 + " Times");
    System.out.println("11 Through 20 appear: " + Range2 + " Times");
    System.out.println("21 Through 30 appear: " + Range3 + " Times");
    System.out.println("31 Through 40 appear: " + Range4 + " Times");
    System.out.println("41 Through 50 appear: " + Range5 + " Times");
    System.out.println("51 Through 54 appear: " + Range6 + " Times");
    }

    }

  • 不要犹豫,调试你的程序,这是学习语言和享受 Java 的最佳方式

    关于将文本文件的内容放入数组然后显示和操作数组内容的 Java 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33657890/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com