2017-05-28 89 views
-1

好吧,这是我的代码到目前为止,我真的需要帮助,使其退出后,我输入退出,也为程序不键入一个字符串后结束。任何帮助或链接到一些帮助将不胜感激我是一个非常新手的程序员。如何让我的程序在进入退出后退出

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); 
     } 
    } 




} 
+1

将'System.exit(0);'移至'if'条件。 –

+0

DANG,工作idk为什么我想离开它 – akay

+1

如果你希望你的程序能够接受多个字符串,你最好学习循环:'for'循环,'while循环和'do .. .while'循环。阅读关于他们[这里](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html)和[here](http://docs.oracle.com/javase/tutorial/java/对于初学者来说,nutsandbolts/for.html)。 –

回答

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(); 
} 
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 

如果你完成了whi le循环,只需加上break;即可。