[英]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.