首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >将文件中的数字放入数组时出错(Java)

将文件中的数字放入数组时出错(Java)
EN

Stack Overflow用户
提问于 2018-03-01 06:09:56
回答 2查看 30关注 0票数 0

我想把下面的前15位数放入一个数组中。问题是它输入了直到"344“的所有数字。此数字仅显示为"34“。我假设这是因为计数将像"xxx“这样的无效输入算作数字。我不确定如何解决这个问题。

代码语言:javascript
代码运行次数:0
运行
复制
0
4
23
566
34
xxx
45
555
11
34
35
45
xxx
65

55
98
344
54

这是我的代码(三个部分,一个添加文件,一个玩游戏,一个打印结果):

代码语言:javascript
代码运行次数:0
运行
复制
public class JumpIt {

    // Constants
    private int count = 0;
    private final int MAX_SIZE = 15;
    public int[] arr = new int[MAX_SIZE];

    public JumpIt(String theName) throws FileNotFoundException {
        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(new File("file.txt"));
        int i=0;
        while(scanner.hasNext() && count < 15) {   //only need first 15 
            if (scanner.hasNextInt()) {
                arr[i++] = scanner.nextInt();
                count+= 1;
            }
            else {
                String s = scanner.nextLine();
                System.out.println(s);
            }
        }
    }

    int n = 0;
    public int play() throws BadInputException{             //gets size
        if(arr[0]!= 0) {
            throw new BadInputException(); 
        }
        return play(arr,0,count-1);
    }

    private static int play(int arr[],int first, int last) { 
        if(first > (last)) {
            return 0;
        }
        else if ((first + 1)>last){
            return arr[first];
        }
        else {
            if (arr[first] < arr[first + 1]) {
                return arr[first] + play(arr,first+2,last);
            }
            else {
                return arr[first+1] + play(arr,first+2,last);
            }
        }
    }

    public void printGame() {
        if(count > 10) {
            for(int i = 0; i < 10; i++) { 
                System.out.print(arr[i] + " ");
            }
            n = count - 10;
            for(int i = 0; i < n; i++) {
                System.out.print(arr[i] + " ");
            }
        }
        //if(count > 15) {
        //  System.out.println("The file has more than 15 integers");
        //  System.out.println("Only the first 15 integers are considered");
        //}
        else {
            n = count; 
            for(int i = 0; i < count;i++) {
                System.out.print(arr[i]);
            }
        }
        System.out.println("The file has "); 
        System.out.print(count);
        System.out.println(" " + "integers.");
        }
}

这是我的主要内容:

代码语言:javascript
代码运行次数:0
运行
复制
public class Driver {
public static void main(String[] args) throws FileNotFoundException {
    JumpIt game4 = new JumpIt("file.txt");
    game4.printGame();
    System.out.println("");
    System.out.println("play game");
    try {
            System.out.println("the cost is " + game4.play());
            System.out.println("");
    } catch (BadInputException e){
            System.out.println("bad input: the first int must be 0");
    }
EN

回答 2

Stack Overflow用户

发布于 2018-03-01 06:46:55

您的代码可以正常工作。使用小清理:

代码语言:javascript
代码运行次数:0
运行
复制
int[] arr = new int[15];
Scanner scanner = new Scanner(new File("file.txt"));
int i = 0;
while (scanner.hasNext() && i < 15) {
  if (scanner.hasNextInt()) {
    arr[i++] = scanner.nextInt();
  } else {
    scanner.nextLine();
  }
}

创建具有以下元素0、4、23、566、34、45、555、11、34、35、45、65、55、98、344的数组。

会不会是代码中的count变量在进入循环之前大于0?你没有在你的代码片段中初始化它。

票数 0
EN

Stack Overflow用户

发布于 2018-03-01 07:46:13

您的问题表明您的程序没有正确地将前15个数字读入数组。事实上是这样的(我已经通过测试进行了验证)。

您认为数组没有正确填充的原因是因为您在数组的打印输出的末尾看到了数字34。最后一个数字是344,但是这个逻辑导致了欺骗(打印前10,然后从数组的开头开始):

代码语言:javascript
代码运行次数:0
运行
复制
public void printGame() {
    if(count > 10) {
        for(int i = 0; i < 10; i++) { 
            System.out.print(arr[i] + " ");
        }
        n = count - 10;   // Here you are restarting back at index=0
        for(int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
    }
    ....

出现数字34是因为它是第五个元素(即15-10)。不幸的是,34与344相似。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49039443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档