gpt4 book ai didi

java - 如何在文件编写器中合并方法

转载 作者:行者123 更新时间:2023-12-02 10:01:27 25 4
gpt4 key购买 nike

我的一个作业问题遇到了麻烦,我想我已经完成了所有事情,直到最后一部分,即调用该方法并将该方法写入输出文件。这是作业:

Write a method isPrime which takes a number and determines whether the number is prime or not. It returns a Boolean.

Write a main method that asks the user for an input file that contains numbers and an output file name where it will write the prime numbers to.

Main opens the input file and calls isPrime on each number. Main writes the prime numbers to the output file.

Modify main to throw the appropriate exceptions for working with files.

我尝试了几种不同的方法来编写带有输出文件的方法,但我不确定具体如何操作。

 public static void main(String[] args) throws IOException {
Scanner keyboard = new Scanner(System.in);
System.out.println("What is the name of the input file?");
String inputfile = keyboard.nextLine();
File f = new File(inputfile);
System.out.println("What is the name of the output file?");
String outputfile = keyboard.nextLine();
Scanner inputFile = new Scanner(f);
FileWriter fw = new FileWriter(outputfile);
PrintWriter pw = new PrintWriter(new File(outputfile));
while (inputFile.hasNextLine()) {
pw.write(inputFile.nextLine().isPrime());
pw.write(System.lineSeparator());
}
pw.close();
inputFile.close();
}

public static void isPrime (int num) throws IOException {
boolean flag = false;
for (int i =2; i <= num/2; i++) {
if (num % i ==0) {
flag = true;
break;
}
}
if (!flag)
System.out.println(num + "is a prime number");
else
System.out.println(num + "is not a prime number");
}

我需要程序能够读取不同数字的输入文件,然后将其中哪个数字是素数写入输出文件。

最佳答案

您编写了“inputFile.nextLine().isPrime()”。但是 inputFile.nextLine() 会返回一个字符串。没有可以在 String 上调用的 isPrime() 方法,因此您将收到编译错误。您必须首先将其转换为整数,将其传递给您的方法,然后处理结果:

isPrime(Integer.parseInt(inputFile.nextLine()));

我建议你只从方法 isPrime() 返回一个消息字符串而不是 void,然后你就可以正确处理它:

pw.write(isPrime(Integer.parseInt(inputFile.nextLine())));

附录:我修改了您的代码,以便您可以看到在哪里添加建议的行。我还省略了不必要的行。

public static void main(String[] args) throws IOException {
Scanner keyboard = new Scanner(System.in);
System.out.println("What is the name of the input file?");
String inputfile = keyboard.nextLine();
File f = new File(inputfile);
System.out.println("What is the name of the output file?");
String outputfile = keyboard.nextLine();
Scanner inputFile = new Scanner(f);
PrintWriter pw = new PrintWriter(new File(outputfile));
while (inputFile.hasNextLine()) {
String nextLine = inputFile.nextLine();
boolean isPrime = isPrime(Integer.parseInt(nextLine));
if (isPrime) {
pw.write(nextLine + System.lineSeparator());
}
}
pw.close();
inputFile.close();
}

public boolean isPrime (int num) {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}

TODO:将文件打开代码放入 try-catch-finally block 中,并将 close() 命令放入其 finally block 中。 (如果你不知道为什么它应该在最后,就问)

关于java - 如何在文件编写器中合并方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55595953/

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