尝试使用 “do -while” 循环迭代特定间隔。

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

Trying to iterate certain interval using "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');
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;
}
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: ", '-');

        // ... (the rest of your code)
    }
}
英文:

Hi this code is part of my code that is supposed to check if the number string is palindrome.
I want to iterate from top to botoom of my code but it doesn't iterate at all , what is wrong ??
I searched in youtube and realized this kind of things , people usually use do-while loop so I was trying to follow the instruction but it doesn't give me what I want .

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


        }

Here is my full code.

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

答案1

得分: 0

将此处代码:

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

改为:

String str = kbd.next();
英文:

Change this:

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

to this

String str = kbd.next();

答案2

得分: 0

当读取 choice 时,使用 nextLine() 而不是 next()

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

next() 只能读取输入直到空格。它不能读取由空格分隔的两个单词。此外,next() 在读取输入后将光标放在同一行,因此在下一次循环中它会读取最后的输入行,而那将是空字符串。

英文:

When read choice, using nextLine() intead of next():

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

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



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

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

        } else {

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

        }
        System.out.println(&quot;Do you want to continue Y or N&quot;);
        choice = kbd.nextLine().charAt(0); // &lt;---here, change to nextLine()
    }while(choice==&#39;y&#39;||choice ==&#39;Y&#39;);


    }

next() can read the input only till the space. It can't read two words separated by space. Also, next() places the cursor in the same line after reading the input. so in next loop it reads last input line that will be empty string.

huangapple
  • 本文由 发表于 2020年9月10日 17:13:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63826508.html
匿名

发表评论

匿名网友

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

确定