繁体   English   中英

从txt文件中读取数字并进行比较

[英]Reading numbers from a txt file and comparing them

使用此程序,我想阅读并比较文本文件中给出的数字,并在数字连续三个时间上升时打印“购买” ,并在数字连续三个下降时打印“出售”

我的程序的问题在于,它仅读取“ numbers.txt”的15行中的13行,并且买卖位置错误。

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.text.DecimalFormat;

public class Practise {
    public static void main(String[] args) throws IOException {
        int num = 0;
        int up = 0;
        int down = 0;
        int same = 0;
        FileInputStream file = new FileInputStream("numbers.txt");
        Scanner scanner = new Scanner(file);
        while (scanner.hasNextDouble()) {
            Double[] array = new Double[15];
            for (int i = 0; i < array.length; i++) {
                array[i] = scanner.nextDouble();
            }
            for (int a = 0; a < array.length && a + 1 < array.length - 1; a++) {
                num++;
                System.out.print(num + "  " + (array[a]));
                if (array[a] < array[a + 1]) {
                    up++;
                } else if (array[a] > array[a + 1]) {
                    down++;
                } else {
                    same++;
                }
                if ((up >= 3 && (down > 1 || same >= 1))) {
                    System.out.print("  " + "sell");
                    up = 0;
                    same = 0;
                } else if ((down >= 3 && (up > 1 || same >= 1))) {
                    System.out.print("  " + "buy");
                    down = 0;
                    same = 0;
                }
                System.out.println();
            }
        }
    }
}

文件numbers.txt:

26.375
25.500
25.125
25.000
25.250
27.125
28.250
26.000
25.500
25.000
25.125
25.250
26.375
25.500
25.500

预期产量:

1 26.375
2 25.500
3 25.125
4 25.000
5 25.250购买
6 27.125
7 28.250
8 26.000卖出
9 25.500
10 25.000
11 25.125购买
12 25.250
13 26.375
14卖出25.500
15 25.500

您使用a + 1 < array.length - 1 ,可以将其转换为a < array.length - 2 我想您知道&&中的第二个操作数将迭代限制为13。

a < array.length && a < array.length - 2 = a < 15 && a < 13 = a < 13

我对您的代码做了一些小的更改,以使其正常工作; 您可以对其进行很多重构,但是我坚持使用您的样式,因此您可以更好地理解其逻辑。

    int num = 1;
    //print the 1st element
    System.out.println(num + "  " + array[0]);
    for (int a = 1; a < array.length; a++) {
        num++;
        System.out.print(num + "  " + (array[a]));
        //plz note that we check with the before, not after
        if (array[a] < array[a - 1]) {
            down++;
        } else if (array[a] > array[a - 1]) {
            up++;
        } else {
            same++;
        }
        //changed down > to down ==
        if ((up >= 3 && (down == 1 || same >= 1))) {
            System.out.print("  " + "sell");
            up = 0;
            same = 0;
        } 
        //changed up > to up ==
        else if ((down >= 3 && (up == 1 || same >= 1))) {
            System.out.print("  " + "buy");
            down = 0;
            same = 0;
        }
        System.out.println();
    }

回答操作者评论:如果您要支持15条以上的记录,则可以继续添加到列表中:

List<Double> list = new ArrayList<>();
while (scanner.hasNextDouble()) {
    list.add(scanner.nextDouble());
}
//if you want to work with array
Double[] array = new Double[list.size()];
array = list.toArray(array);

这是一个工作示例(必须将数组更改为列表)

int num = 0, up = 0, down = 0, same = 0;

FileInputStream file = new FileInputStream("numbers.txt");
Scanner scanner = new Scanner(file);
List<Double> list = new ArrayList<>();

while (scanner.hasNextDouble()) {
    list.add(scanner.nextDouble());
}

int position = 0;

while (position + 2 < list.size()) {
    Double[] nums = new Double[3];

    System.out.println(nums[0] = list.get(position));
    System.out.println(nums[1] = list.get(position + 1));
    System.out.println(nums[2] = list.get(position + 2));

    if (nums[1] > nums[0] && nums[2] > nums[1]) {
        System.out.println("buy");
        up++;
    } else if (nums[1] < nums[0] && nums[2] < nums[1]) {
        System.out.println("sell");
        down++;
    } else {
        same++;
    }
    position += 2;
}
System.out.println("Ups total: " + up);
System.out.println("Downs total: " + down);
System.out.println("Same total: " + same);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM