Java 尝试使用“迭代”来迭代某个间隔;“边做边做”;

Java 尝试使用“迭代”来迭代某个间隔;“边做边做”;,java,while-loop,do-while,Java,While Loop,Do While,嗨,这段代码是我代码的一部分,它应该检查数字字符串是否是回文的。 我想从代码的顶部迭代到底部,但它根本不迭代,怎么了?? 我在youtube上搜索并意识到这类事情,人们通常使用do-while循环,所以我试图按照说明进行操作,但它并没有给我想要的东西 do { System.out.println("You passed Catch-Block stage! , Please enter the number that you want to check if it i

嗨,这段代码是我代码的一部分,它应该检查数字字符串是否是回文的。 我想从代码的顶部迭代到底部,但它根本不迭代,怎么了?? 我在youtube上搜索并意识到这类事情,人们通常使用do-while循环,所以我试图按照说明进行操作,但它并没有给我想要的东西

do {
        System.out.println("You passed Catch-Block stage! , Please enter the number that you want to check if it is palindrome");

        String str = kbd.nextLine().trim();
        String org_str = str;
        String rev = "";
        int len = str.length();



            for (int i = len - 1; i >= 0; i--) {
                rev = rev + str.charAt(i);

            }
            if (org_str.equals(rev)) {
                System.out.println(org_str + " is Palindrome Number");

            } else {

                System.out.println(org_str + "is Not Palindrome String");

            }
            System.out.println("Do you want to continue Y or N");
            choice = kbd.next().charAt(0);
        }while(choice=='y'||choice =='Y');


        }
这是我的全部代码

package com.company;
import java.util.Scanner;
public class Main {

    public static void main(String[] args) {

        Scanner kbd = new Scanner(System.in);
        char choice;
        long firstNum = 0;

        firstNum = getLong(" Enter the first number: ", '-');


        do {
        System.out.println("You passed Catch-Block stage! , Please enter the number that you want to check if it is palindrome");

        String str = kbd.nextLine().trim();
        String org_str = str;
        String rev = "";
        int len = str.length();



            for (int i = len - 1; i >= 0; i--) {
                rev = rev + str.charAt(i);

            }
            if (org_str.equals(rev)) {
                System.out.println(org_str + " is Palindrome Number");

            } else {

                System.out.println(org_str + "is Not Palindrome String");

            }
            System.out.println("Do you want to continue Y or N");
            choice = kbd.next().charAt(0);
        }while(choice=='y'||choice =='Y');


        }

    public static long getLong(String prompt, char exitChar)
    {
        long retVal = 0;

        boolean validInput = false;

        String userInput = "";

        Scanner kbd = new Scanner(System.in);


        while (!validInput) {
            System.out.println(prompt);


            try
            {

                userInput = kbd.nextLine().trim();
                if (userInput.length() > 0 && userInput.charAt(0) == exitChar)
                {
                    System.out.println("Ending the program at the user's request");
                    System.exit(1);
                }

                retVal = Long.parseLong(userInput);
                validInput = true;
            }
            catch (Exception ex)
            {
                System.out.println("That is not numeric. Try again or press  " + exitChar + "to Quit");

            }



        }
        return retVal;
    }
}
更改此项:

String str = kbd.nextLine().trim();
对此

String str = kbd.next();

读取
选项时,使用
nextLine()
intead of
next()

do{
System.out.println(“您通过了Catch Block stage!,请输入要检查其是否为回文的数字”);
字符串str=kbd.nextLine().trim();
字符串org_str=str;
字符串rev=“”;
int len=str.length();
对于(int i=len-1;i>=0;i--){
rev=rev+str.charAt(i);
}
如果(组织结构等于(修订)){
println(org_str+“是回文数”);
}否则{
println(org_str+“不是回文字符串”);
}
System.out.println(“您想继续Y还是N”);
choice=kbd.nextLine().charAt(0)//
do {
    System.out.println("You passed Catch-Block stage! , Please enter the number that you want to check if it is palindrome");

    String str = kbd.nextLine().trim();
    String org_str = str;
    String rev = "";
    int len = str.length();



        for (int i = len - 1; i >= 0; i--) {
            rev = rev + str.charAt(i);

        }
        if (org_str.equals(rev)) {
            System.out.println(org_str + " is Palindrome Number");

        } else {

            System.out.println(org_str + "is Not Palindrome String");

        }
        System.out.println("Do you want to continue Y or N");
        choice = kbd.nextLine().charAt(0); // <---here, change to nextLine()
    }while(choice=='y'||choice =='Y');


    }