Java 如何重新启动此代码?
我的程序要求用户输入他们在工作目录(包含文本)中的文件名,然后输入同样在同一目录中的输出文件名。之后,用户必须选择是将文件中的所有文本大写还是小写 一旦他们选择了,他们就可以选择处理另一个文件。这就是我遇到麻烦的地方。打印“是否要处理另一个文件?Y表示是,N表示否?”后,如何使其循环回到开始 现在,我的代码不断循环回“大写或小写所有单词”,我需要它停止这样做,并询问用户是否要处理另一个文件,如果是这样,它需要返回并再次询问输入和输出文件名Java 如何重新启动此代码?,java,Java,我的程序要求用户输入他们在工作目录(包含文本)中的文件名,然后输入同样在同一目录中的输出文件名。之后,用户必须选择是将文件中的所有文本大写还是小写 一旦他们选择了,他们就可以选择处理另一个文件。这就是我遇到麻烦的地方。打印“是否要处理另一个文件?Y表示是,N表示否?”后,如何使其循环回到开始 现在,我的代码不断循环回“大写或小写所有单词”,我需要它停止这样做,并询问用户是否要处理另一个文件,如果是这样,它需要返回并再次询问输入和输出文件名 public static void main(Stri
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the input data file name:");
String fileInput = sc.next();
System.out.println("Please enter the output data file name:");
String fileOutput = sc.next();
while(true){
System.out.println("A: Capitalize all words.\nB: Lowercase all words.");
System.out.println("enter choice:");
char choice = sc.next().charAt(0);
if(choice == 'A'){
capitalize(fileInput, fileOutput);
}else{
lowercase(fileInput, fileOutput);
}
}
System.out.println("Process another file? Y for Yes or N for No");
}
您只需将所有代码包装在while循环中,如下所示;while循环仅重复其中的代码:
public static void main(String[] args) {
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the input data file name:");
String fileInput = sc.next();
System.out.println("Please enter the output data file name:");
String fileOutput = sc.next();
System.out.println("A: Capitalize all words.\nB: Lowercase all words.");
System.out.println("enter choice:");
char choice = sc.next().charAt(0);
if (choice == 'A') {
capitalize(fileInput, fileOutput);
} else {
lowercase(fileInput, fileOutput);
}
System.out.println("Process another file? Y for Yes or N for No");
String processAnother = sc.next();
if (processAnother.equals("N") || processAnother.equals("n")) break;
}
}
是的,我想我必须把所有的代码都放到while循环中,我只是不知道我必须添加一个新字符串来处理另一个字符串。谢谢