java程序循环回来开始

时间:2014-04-22 15:44:48

标签: java

我试图让这个程序循环回来,当用户想要尝试另一个单词时重新开始。我尝试了一段时间,但无法弄明白。我是编程的新手,所以任何帮助都表示赞赏。

import java.util.Scanner;


public class PEX5{

    public static void main(String[] theArgs){

        int numLetters;
        char fromStack;
        char fromQueue;
        int charCount;

        String x;

        UnboundedStackInterface<Character> myStack = new LinkedStack<Character>();
        UnboundedQueueInterface<Character> myQueue = new LinkedUnbndQueue<Character>();

        System.out.println("Enter a word to check for Palindrome");

        @SuppressWarnings("resource")
        Scanner keyboard = new Scanner(System.in);

        x = keyboard.nextLine();

        int i;
        numLetters = 0;

        for(i = 0; i < x.length(); i++){
            Character ch = x.charAt(i);

            numLetters++;

            myStack.push(ch);
            myQueue.enqueue(ch);
        }

        charCount = 0;

        while(charCount <= numLetters) {

            fromStack = myStack.top();
            myStack.pop();
            fromQueue = myQueue.dequeue();

            if(fromStack != fromQueue)
                System.out.println("Not Palindrome");

            if(fromStack == fromQueue) {
                charCount++;
                System.out.println("Palindrome");
            }

            System.out.println("Would you like to try another? (Y = yes  N = no): ");
            x = keyboard.nextLine();
            System.out.println("");
        }

    }
}

4 个答案:

答案 0 :(得分:2)

你可以将所有内容放入这样的while循环中:

boolean isRunning = true;
String tryAgain = "";

while (isRunning) {

 // All your code you have in your example.

 // Ask user if he wants to retry with a scanner.
 tryAgain = <use scanner here>;
 if (tryAgain.equals("no") || tryAgain.equals("No") ) isRunning = false;

}

或者

String tryAgain = "";

while (true) {

 // All your code you have in your example.

 // Ask user if he wants to retry with a scanner.
 tryAgain = <use scanner here>;
 if (tryAgain.equals("no") || tryAgain.equals("No") ) break;

}

显然你需要调整代码以便它向用户询问系统是否打印出来,你可能想要做更多的检查,如果他写的是或者根本没有等等,但你必须自己做一点。 ;)

答案 1 :(得分:1)

你要做的是创建一个外循环来持续游戏,直到有人想要退出。有几种方法可以做到这一点,过去我做过的一种方法是这样的:

boolean willContinue = true;
do {
    // Game code here

    // prompt user to continue (or quit)
    if(keyboard.nextLine().startsWith("N")) willContinue = false;

} while (willContinue)

通过使其成为一个while(True)循环并且具有无响应调用中断,您可以获得相同的结果;代替。

答案 2 :(得分:0)

您可以尝试这样做,添加一段时间并让用户输入要继续的内容。

import java.util.Scanner;

public class Palindrome {
public static void main(String[] theArgs)

{   int numLetters;
    char fromStack;
    char fromQueue;


    int charCount;
    String x;
    UnboundedStackInterface<Character> myStack = new LinkedStack<Character>();
    UnboundedQueueInterface<Character> myQueue = new LinkedUnbndQueue<Character>();
    String ch;
    do{
    System.out.println("Enter a word to check for Palindrome");
    @SuppressWarnings("resource")
    Scanner keyboard = new Scanner(System.in);
    x = keyboard.nextLine();



    int i;
    numLetters = 0;



    for(i = 0;i<x.length();i++)
    {

    numLetters++;

    myStack.push(ch);
    myQueue.enqueue(ch);
    }


     charCount = 0;

    while (charCount<=numLetters)
    {

        fromStack = myStack.top();
        myStack.pop();
        fromQueue = myQueue.dequeue();

        if(fromStack != fromQueue)
          System.out.println("Not Palindrome");

       if(fromStack == fromQueue)
        {charCount++;
          System.out.println("Palindrome");
        }

        System.out.println("Would you like to try another? (Y = yes  N = no): ");
        x = keyboard.nextLine();
        System.out.println("");
        }

    System.out.println("To continue enter 'y'");
    keyboard = new Scanner(System.in);
    ch = keyboard.nextLine();

    }while(ch=="y");
  }
}

答案 3 :(得分:0)

您的代码存在很多问题。以下是对它们的一些修复:

  • 使用&#34; try-with-resources&#34;进行输入流声明。声明。每个输入/输出元素都应该这样包裹;它使得以后重用代码变得容易得多。

  • 您需要在代码中添加javadoc注释。

  • 将回文检查封装在自己的功能中。通过从输入/输出逻辑重构决策逻辑,您可以使代码更清晰,更简洁。写一个方法来填充这个签名:

    public static boolean isPalindrome(String word)
    
  • 简化回文检查算法。您需要做的就是检查单词本身是否反转。没有什么花哨。例如,您可以这样做:

    public static boolean isPalindrome(String word){
        return String.valueOf(Arrays.reverse(word.toCharArray())).equals(word);
    }
    

    这不仅更容易阅读,而且实际上也更快。

  • 更改提示结构。他们真的每次想要输入另一个单词时都需要告诉你吗?我建议使用&#39;留空以退出&#39;退出类型。提示可以是:

    System.out.print("Enter a word (leave blank to quit):");
    
  • 使用loop-and-a-half完成提示周期,而不是发明自己的劣质循环模式:

    public static void main(String [] args) {
        try(Scanner in = new Scanner(System.in), PrintWriter out = System.out){
            while(true){
                out.print("Enter a word (leave blank to quit):");
    
                String word = in.nextLine()
                if(in.isEmpty()) break;
    
                out.printf("%s is%s a palindrome.%n",
                        word, isPalindrome(word)?" not":"");
            }
        }
    }
    

    所有这些代码完成的内容与您编写的内容完全相同,但更简洁,更清晰,更快。