如何在Java中设置一个y/n循环?

huangapple go评论86阅读模式
英文:

How to setup a y/n loop in java?

问题

以下是已经翻译好的部分:

public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    String controller = "y";
    do {

        System.out.println("Enter a palindrome!");
        String input = scan.nextLine();
        String original = input;
        input = input.replaceAll("\\s", "");
        int len = input.length();
        char[] charArray = new char[len];
        char[] charArray2 = new char[len];

        for(int i = 0; i < len; i++){
            charArray2[i] = input.charAt(i);
        }
        for(int j = 0; j < len; j++){
            charArray[j] = charArray2[len-1-j];
        }
        String palindrome = new String(charArray);
        if(palindrome.equals(input)){
            System.out.println(palindrome);
            System.out.println(original + " is a palindrome!\nWould you like to test another palindrome? (y/n)");
        }else{
            System.out.println(palindrome);
            System.out.println(original + " is not a palindrome!\nWould you like to test another palindrome? (y/n)");

        }
        scan.next(controller);

    }while(controller.equalsIgnoreCase("y"));

}
英文:

I have a program that is a palindrome checker that takes an input reverses it and checks if the original input is equal to the reverse. I'm trying to get a y/n loop inside of the program to check if the user wants to enter another palindrome. This is my code:

  public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String controller = &quot;y&quot;;
    do {

        System.out.println(&quot;Enter a palindrome!&quot;);
        String input = scan.nextLine();
        String original = input;
        input = input.replaceAll(&quot;\\s&quot;, &quot;&quot;);
        int len = input.length();
        char[] charArray = new char[len];
        char[] charArray2 = new char[len];

        for(int i = 0; i &lt; len; i++){
            charArray2[i] = input.charAt(i);
        }
        for(int j = 0; j &lt; len; j++){
            charArray[j] = charArray2[len-1-j];
        }
        String palindrome = new String(charArray);
        if(palindrome.equals(input)){
            System.out.println(palindrome);
            System.out.println(original + &quot; is a palindrome!\nWould you like to test another palindrome? (y/n)&quot;);
        }else{
            System.out.println(palindrome);
            System.out.println(original + &quot; is not a palindrome!\nWould you like to test another palindrome? (y/n)&quot;);

        }
        scan.next(controller);

    }while(controller.equalsIgnoreCase(&quot;y&quot;));

    }

This is the output I get:

Enter a palindrome!
was it a cat i saw
wasitacatisaw
was it a cat i saw is a palindrome!
Would you like to test another palindrome? (y/n)
y
Enter a palindrome!

 is a palindrome!
Would you like to test another palindrome? (y/n)
y
Enter a palindrome!

 is a palindrome!
Would you like to test another palindrome? (y/n)
y
Enter a palindrome!

 is a palindrome!
Would you like to test another palindrome? (y/n)
y
Enter a palindrome!

 is a palindrome!
Would you like to test another palindrome? (y/n)
y
Enter a palindrome!

 is a palindrome!
Would you like to test another palindrome? (y/n)
y

I can't figure out how to get it to wait for another entry and test that one instead. Any help would be great.

答案1

得分: -1

好的,我稍微修改了你的代码,实际上你的代码已经很接近正确了。以下是你的代码的翻译部分:

public static void main(String[] args) {
    String checker = "y"; // 设置 checker 以确保第一个循环

    while(checker.equalsIgnoreCase("y")) { // 检查正确的短语
        System.out.println("输入要检查的字符串!"); // 初始消息
        Scanner reader = new Scanner(System.in); // 读取第一行
        String input = reader.nextLine();
        if (isPalindrome(input)) { // 检查是否为回文
            System.out.println(input + " 是回文!");
        } else {
            System.out.println(input + " 不是回文!");
        }
        System.out.println("是否要输入另一个字符串? y/n");
        checker = reader.nextLine(); // 将 checker 设置为下一个循环的响应
    }
    System.out.println("好的,再见 :)"); // 结束消息
}

private static boolean isPalindrome(String s){
    String reverse = ""; // 创建用于后续比较的字符串

    for (int i = 0; i < s.length(); i++) {
        reverse += s.trim().charAt(s.length() - i - 1); // 反转字符串
    }
    return reverse.equalsIgnoreCase(s); // 返回不区分大小写的布尔值
}

我真的没有做太多改动:

  • 我将检查回文的部分放在了一个函数中,以提高可读性。
  • 在处理用户输入和一般文本时,使用字符串(Strings)通常是更好的做法。
英文:

Ok i reworked your code a bit and tbh you werent far off.
Here is a pastebin link with my code.

public static void main(String[] args) {
    String checker = &quot;y&quot;; //set checker to ensure the first cycle

    while(checker.equalsIgnoreCase(&quot;y&quot;)) { // check for correct phrase
        System.out.println(&quot;Enter a string to check!&quot;); // inital message
        Scanner reader = new Scanner(System.in);        // read first line
        String input = reader.nextLine();
        if (isPalindrome(input)) {                      // check input for palindrome
            System.out.println(input + &quot; is a Palindrome!&quot;);
        } else {
            System.out.println(input + &quot; is not a Palindrome!&quot;);
        }
        System.out.println(&quot;Do you want to enter another string? y/n&quot;);
        checker = reader.nextLine();                    // set checker to response for next cycle
    }
    System.out.println(&quot;Ok bye :)&quot;);                    // death message
}


private static boolean isPalindrome(String s){
    String reverse = &quot;&quot;;                            // create string for later comparison

    for (int i = 0; i &lt; s.length(); i++) {
        reverse+=s.trim().charAt(s.length()-i-1);   // reverse the string
    }
    return reverse.equalsIgnoreCase(s);             // return boolean ignoring case
}

I really didnt change that much:

  • I check the palindrome in a function for better readablility
  • Used Strings instead of char[]. Its generally better practice handling userinput and general text that way.

EDIT:
my output:

Enter a string to check!
hey
hey is not a Palindrome!
Do you want to enter another string? y/n
y
Enter a string to check!
brb
brb is a Palindrome!
Do you want to enter another string? y/n
n
Ok bye :)

Process finished with exit code 0

huangapple
  • 本文由 发表于 2020年10月14日 07:01:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64344317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定