好的,所以这是我的代码到目前为止我真的需要帮助才能在我输入quit之后退出,并且在输入一个字符串后程序也不会结束。任何帮助或链接到一些帮助将不胜感激我是一个非常新手的程序员。
import java.util.Scanner;
import java.io.*;
public class reverse
{
public static void main(String [] args)
{
String input = null;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter a string for reversal or type QUIT to exit: ");
input = keyboard.nextLine();
if(input.equals("quit"))
{
//close the program
}
Reverser(input, (input.length() - 1));
System.exit(0);
}
public static void Reverser(String str, int size)
{
if(size>=0)
{
System.out.print(str.charAt(size));
Reverser(str,size-1);
}
}
}
答案 0 :(得分:0)
您可以尝试这样的事情
input = keyboard.nextLine();
while(!input.equals("quit"))
{
Reverser(input, (input.length() - 1));
System.out.println("Please enter a string for reversal or type QUIT to exit: ");
input = keyboard.nextLine();
}
答案 1 :(得分:0)
这是一个可能的解决方案,它在键入多个输入后不会结束:
Scanner keyboard = new Scanner(System.in);
String input;
System.out.println("Please enter a String for reversal or type quit to exit:");
while (!(input = keyboard.nextLine()).equals("quit")) { // wait until the input is quit
// do stuff with input
}
keyboard.close(); // don't forget to close the scanner when you are done
控制台输出:
Please enter a String for reversal or type quit to exit:
blah
test
quit
Process finished with exit code 0
如果您已完成while循环,只需添加break;
。