2016-02-24 111 views
-2

我開發了一個程序,用於檢查用戶輸入的字符串是否是迴文。這部分工作正常,但我希望程序再次啓動,如果用戶想輸入另一個字符串。我看過不同的論壇,並嘗試不同的循環,但我無法讓程序重複正確。我目前有一個do語句,我認爲問題在於調用main(),因爲它在netbeans中被標記。我不知道爲什麼。任何幫助或在正確的方向微調將不勝感激。我需要一些幫助讓我的程序重複

/* 
* This program checks to see if the user input is a palindrome. 
* white space and non alphanumeric characters will be ignored. 
*/ 
package firstsubroutines; 

/** 
* 
* @author anonymous 
*/ 

public class FirstSubroutines { 

    static String str; // global variable 
    static String reversed; // global variable 

/* 
* The subroutine strp accepts a string as an argument 
* and returns a string stripped of spaces and 
* non alphanumeric characters 
*/  
    static String strip(String str){ 
     str = str.replaceAll("[^a-zA-Z0-9]", ""); 
     str = str.toLowerCase(); 
     System.out.println("Stripped: " + str); 
     return str; 
    } // end of subroutine stripped 

/* 
* The subroutine reverse accepts a string as an argument 
* and returns a string in reverse order 
*/ 
    static String reverse(String str){ 
     int i; 
     reversed = ""; 
     for (i = str.length() - 1; i >= 0; i--) { 
      reversed = reversed + str.charAt(i); 
     } 
     System.out.println("Reversed: " + reversed); 
     return reversed; 
    } // end of subroutine reversed 

/* 
* This is the main progam where the subroutines 
* will be called 
*/ 
    public static void main(String[] args) { 
     String userInput; // The input by the user. 

     System.out.println("This program checks to see if the user's input is a palindrome."); 
     System.out.println("White space and non alphanumeric characters will be ignored."); 
     System.out.println(); 
     System.out.print("Please enter a string: "); 
     userInput = TextIO.getln(); // assigns the user input to a variable 

     // subroutine strip is called and an the value of 
     // the variable userInput is passed 
     str = strip(userInput); 

     // subroutine reverse is called and an the value of 
     // the variable str is passed 
     String rev = reverse(str); 

     // compares the two objects 
     if (str.equals(rev)) { 
      System.out.println("This IS a palindrome"); 
     } 
     else { 
      System.out.println("This NOT a palindrome"); 
     } // end of if statement 


     boolean toContinue; // True if user wants to play again. 
     do { 
      main(); 
      System.out.print("Do you want enter another string?: "); 
      toContinue = TextIO.getlnBoolean(); 
      } while (toContinue == true); 

    } // end main 

} // end class 
+0

你是誰實際上在你的循環中調用public static void main(String [] args)方法? – Stilleur

+0

是的。這不是我應該做的嗎? – robotsruin

+0

你不應該那樣做。主要方法不應該被稱爲:)請參閱http://stackoverflow.com/questions/21992659/calling-main-method-inside-main-in-java。我會以另一種方式向你解釋如何去做。 @Berger沒有回答我在想什麼 – Stilleur

回答

3

獨立的輸入處理邏輯,另一種方法:

private static void processString(){ 

     String userInput; // The input by the user. 

     System.out.println("This program checks to see if the user's input is a palindrome."); 
     System.out.println("White space and non alphanumeric characters will be ignored."); 
     System.out.println(); 
     System.out.print("Please enter a string: "); 
     userInput = TextIO.getln(); // assigns the user input to a variable 

     // subroutine strip is called and an the value of 
     // the variable userInput is passed 
     str = strip(userInput); 

     // subroutine reverse is called and an the value of 
     // the variable str is passed 
     String rev = reverse(str); 

     // compares the two objects 
     if (str.equals(rev)) { 
      System.out.println("This IS a palindrome"); 
     } 
     else { 
      System.out.println("This NOT a palindrome"); 
     } // end of if statement 


} 

然後只是把它從main在一個循環:

public static void main(String[] args) { 

     boolean toContinue = false; // True if user wants to play again. 
     do { 
      processString(); 
      System.out.print("Do you want enter another string?: "); 
      toContinue = TextIO.getlnBoolean(); 
      } 
     while (toContinue == true); 
} 
+1

非常感謝,我認爲我對礦難爲什麼不起作用有了更好的理解。 – robotsruin

0

使用Scanner類:

Scanner sc = new Scanner(System.in); 

while (sc.hasNext()) { 
    String input = sc.next(); 
    // do something with input 
} 
+0

感謝您的幫助 – robotsruin