gpt4 book ai didi

java - 需要帮助读取文本文件中的 2 位数字

转载 作者:行者123 更新时间:2023-11-30 02:23:10 25 4
gpt4 key购买 nike

这是我当前项目的代码。我有任何 10 或以上的数字,它会读取每个数字而不是整个数字。有什么帮助吗?我正在使用的数字:

1 3

1 1

-1 -5

5 3

45 45

1001001100 1001001100

import java.util.Scanner;
import java.io.*;
import java.io.PrintWriter;

public class comparison_rylan_howard {
public static void main(String[] args) throws IOException {
Scanner reader = new Scanner(new
File("H:\\NetBeansProjects\\Unit1\\comparison.txt"));
try (PrintWriter writer = new PrintWriter("Results.txt")) {

while (reader.hasNext()) {
double first = reader.nextDouble();
double second = reader.nextDouble();
if (first <= -1 || second <= -1) {
writer.println("Error");
}
if (first > second) {
writer.println(">");
writer.println(" ");
}
if (first < second) {
writer.println("<");
writer.println(" ");
} else {
writer.println("=");
writer.println(" ");
}
}
}
}
}

最佳答案

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class comparison_rylan_howard {
public static void main(String[] args) throws IOException {
Scanner reader = new Scanner(new
File("H:\\NetBeansProjects\\Unit1\\comparison.txt"));
try (PrintWriter writer = new PrintWriter("Results.txt")) {

while (reader.hasNext()) {

double first = reader.nextDouble();
double second = reader.nextDouble();
writer.println(first+","+second );
if (first <= -1 || second <= -1) {
writer.println("Error");
writer.println("");
}
else if (first > second) {
writer.println(">");
writer.println("");
}
else if (first < second) {
writer.println("<");
writer.println("");
} else {
writer.println("=");
writer.println("");
}
}
}
}
}

输出:

1.0,3.0
<

1.0,1.0
=

-1.0,-5.0
Error

5.0,3.0
>

45.0,45.0
=

1.0010011E9,1.0010011E9
=

关于java - 需要帮助读取文本文件中的 2 位数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46307532/

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