Java 如何让我的程序在输入退出后退出
好的,这是我的代码到目前为止,我真的需要帮助,使它退出后,我键入退出,也为程序不结束后,在一个字符串中键入。任何帮助或链接到一些帮助将不胜感激我是一个非常新手程序员Java 如何让我的程序在输入退出后退出,java,Java,好的,这是我的代码到目前为止,我真的需要帮助,使它退出后,我键入退出,也为程序不结束后,在一个字符串中键入。任何帮助或链接到一些帮助将不胜感激我是一个非常新手程序员 import java.util.Scanner; import java.io.*; public class reverse { public static void main(String [] args) { String input = null; Scanner ke
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);
}
}
}
你可以试试这样的
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();
}
以下是一个可能的解决方案,它不会在键入多个输入后结束:
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
移动系统。退出(0)如果你想让你的程序能够接受多个字符串,你最好学习循环:for
循环,while
循环和do…while
循环。首先,请阅读有关它们的文章,非常感谢