【问题标题】:I need some help getting my program to repeat [closed]我需要一些帮助让我的程序重复[关闭]
【发布时间】:2016-06-06 13:33:21
【问题描述】:

我开发了一个程序来检查用户输入的字符串是否是回文。该部分工作正常,但如果用户想输入另一个字符串,我希望程序重新启动。我查看了不同的论坛并尝试了不同的循环,但我无法让程序正确重复。我目前有一个 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

【问题讨论】:

  • 你真的在循环中调用 public static void main(String[] args) 方法吗?
  • 是的。这不是我应该做的吗?
  • 你不应该那样做。不应调用 main 方法 :) 参见stackoverflow.com/questions/21992659/…。我会用另一种方式向你解释如何做。 @Berger 确实回答了我的想法
  • 谢谢@Stilleur 我会阅读你提到的堆栈溢出答案。

标签: java loops netbeans


【解决方案1】:

将您的输入处理逻辑分离到另一种方法:

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

【讨论】:

  • 谢谢,效果很好,我想我对为什么我的不起作用有了更好的理解。
【解决方案2】:

使用Scanner 类:

Scanner sc = new Scanner(System.in);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    相关资源
    最近更新 更多