Java 带扫描仪的是/否循环
我正在编写一个java程序,其中初始部分是一个扫描器。我需要用户输入文件夹名称,然后程序需要确认。扫描仪正在询问相关问题并接受答案。然后我需要它来确认Y或N.Y,程序将继续。N、 我需要代码来循环并再次问第一个问题。我已经到处搜索过了,我可以看到许多整数的解决方案,但文本的解决方案不多Java 带扫描仪的是/否循环,java,loops,Java,Loops,我正在编写一个java程序,其中初始部分是一个扫描器。我需要用户输入文件夹名称,然后程序需要确认。扫描仪正在询问相关问题并接受答案。然后我需要它来确认Y或N.Y,程序将继续。N、 我需要代码来循环并再次问第一个问题。我已经到处搜索过了,我可以看到许多整数的解决方案,但文本的解决方案不多 import java.util.Scanner; public class webSiteGenerator { public static void main(String[] args) {
import java.util.Scanner;
public class webSiteGenerator {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
System.out.println("Please enter a source folder: ");
String sourceFolder = obj.nextLine();
System.out.println("You have selected the folder '" + sourceFolder + "'. Are you sure (Y/N)");
String confirmation = obj.nextLine();
while (!"Y".equalsIgnoreCase(confirmation) && "N".equalsIgnoreCase(confirmation)) {
System.out.println("Response not recognised. Please confirm... Are you sure (Y/N)");
confirmation = obj.next();
}
}
}
“Y”和“N”的响应将存储在字符串源文件夹
中,因此您只需将字符串源文件夹
与Y和N进行比较
因此,您的代码应该是:
p.Sflag
实际上用于退出while
条件。您可以使用任何单词代替flag
询问是否有任何问题会产生疑问。什么问题的解决方案?你的问题是什么?
!“Y”。equalsIgnoreCase(确认)
表示N
和“N”。equalsIgnoreCase(确认)也表示N`
int flag = 0;
while (flag!=1){
if(sourceFolder.equals("Y") || sourceFolder.equals("y")){
//Your Code
flag=1;
}else if((sourceFolder.equals("N") || sourceFolder.equals("n")){
flag=1;
} else{
print("Invalid Input! Please choose only between Y or N ");
flag=1;
}
}