[英]Reading numbers from a txt file and comparing them
With this program I want to read and compare the numbers that I'm given in a text file and print out "buy" whenever the number goes up three consecutive time and "sell" whenever the number goes down three consecutive times. 使用此程序,我想阅读并比较文本文件中给出的数字,并在数字连续三个时间上升时打印“购买” ,并在数字连续三个下降时打印“出售” 。
The problem with my program is that it only reads 13 of the 15 lines of the "numbers.txt" and the buy-sell is at wrong places. 我的程序的问题在于,它仅读取“ 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();
}
}
}
}
The file numbers.txt: 文件numbers.txt:
26.375
26.375
25.50025.500
25.12525.125
25.00025.000
25.25025.250
27.12527.125
28.25028.250
26.00026.000
25.50025.500
25.00025.000
25.12525.125
25.25025.250
26.37526.375
25.50025.500
25.50025.500
Expected output: 预期产量:
1 26.375
1 26.375
2 25.5002 25.500
3 25.1253 25.125
4 25.0004 25.000
5 25.250 buy5 25.250购买
6 27.1256 27.125
7 28.2507 28.250
8 26.000 sell8 26.000卖出
9 25.5009 25.500
10 25.00010 25.000
11 25.125 buy11 25.125购买
12 25.25012 25.250
13 26.37513 26.375
14 25.500 sell14卖出25.500
15 25.50015 25.500
You use a + 1 < array.length - 1
, which can be transformed to a < array.length - 2
. 您使用
a + 1 < array.length - 1
,可以将其转换为a < array.length - 2
。 I guess you understand that the second operand in && limits your iteration to 13. 我想您知道&&中的第二个操作数将迭代限制为13。
a < array.length && a < array.length - 2
= a < 15 && a < 13
= a < 13
a < array.length && a < array.length - 2
= a < 15 && a < 13
= a < 13
I did some minor changes on your code for it to work; 我对您的代码做了一些小的更改,以使其正常工作; you could refactor it a lot, but I stick to your style, so you can better understand the logic.
您可以对其进行很多重构,但是我坚持使用您的样式,因此您可以更好地理解其逻辑。
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();
}
Answering to OPs comment: if you want to support more than 15 records you can keep adding to a list: 回答操作者评论:如果您要支持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);
Here's a working example (had to change array to list) 这是一个工作示例(必须将数组更改为列表)
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.