我已经为一个程序编写了代码,旨在让用户输入一个文本文件和一个输出文件,然后该程序必须读取输入文件中的每一行以找到最小的数字。输出应该有最小的数字,后跟“<==最小的数字”。当我运行它时,最小的数字每次都会成为文本文件中的最后一个数字
这是我到目前为止的代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class FindSmallest {
public static void main(String[] args) throws FileNotFoundException {
Scanner console = new Scanner(System.in);
System.out.print("Your input file: ");
String inputFileName = console.next();
System.out.print("Your output file: ");
String outputFileName = console.next();
File inputFile = new File(inputFileName);
Scanner in = new Scanner(inputFile);
PrintWriter out = new PrintWriter(outputFileName);
double smallNum = in.nextDouble();
while(in.hasNextDouble()){
double number = in.nextDouble();
if (number < smallNum); {
smallNum = number;
}
}
System.out.println(smallNum);
//out.printf("%8.2f <== smallest number", smallNum);
in.close();
out.close();
}
}
最佳答案
您有一个拼写错误,请删除“;”就在if (number < smallNum)
之后。 ;
if
之后导致“smallNum = number;”无论条件如何都运行指令。
关于java - 如何找到文本文件中最小的数字? ( java ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47169683/