Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

java - 什么(s.charAt(low)!= s.charAt(high)){“和” int high = s.length()-1;(whats (s.charAt(low) != s.charAt(high)) {“ and ” int high = s.length() - 1;)

So I was learning to code using a textbook and suddenly it gave an example using loops and it used a lot of concepts and code that I have previously never covered.

(因此,我在学习使用教科书进行编码的过程中,突然间它给出了一个使用循环的示例,并且使用了很多我以前从未涉及过的概念和代码。)

Please can someone explain to me what if (s.charAt(low) != s.charAt(high)) and int high = s.length() - 1;

(如果有人if (s.charAt(low) != s.charAt(high))int high = s.length() - 1;该怎么if (s.charAt(low) != s.charAt(high)) int high = s.length() - 1;)

are.

(是。)

Also, why is low = 0?

(另外,为什么低= 0?)

I haven't learnt th?s yet.

(我还没学会。)

This is a code for finding palindromes.

(这是查找回文的代码。)

Thanks

(谢谢)

import java.util.Scanner;

public class Palindrome {
    /** Main method */
    public static void main(String[] args) {
        // Create a Scanner
        Scanner input = new Scanner(System.in);

        // Prompt the user to enter a string
        System.out.print("Enter a string: ");
        String s = input.nextLine();

        // The index of the first character in the string
        int low = 0;

        // The index of the last character in the string
        int high = s.length() - 1;

        boolean isPalindrome = true;
        while (low < high) {
            if (s.charAt(low) != s.charAt(high)) {
                isPalindrome = false;
                break;
            }
            low++;
            high--;
        }

        if (isPalindrome)
            System.out.println(s + " is a palindrome");
        else
            System.out.println(s + " is not a palindrome");
    }
}
  ask by Aishah Hamman translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Let's assume, you enter the text, Hello .

(假设您输入文本Hello 。)

Length of the text, Hello is 5

(文字长度, Hello5)

int high = s.length() - 1 means you are storing 4 (ie 5 - 1) to the variable, high .

(int high = s.length() - 1表示您要将4 (即int high = s.length() - 1存储到变量high 。)

The index of the first character in a String is 0 .

(String第一个字符的索引为0 。)

Therefore, the index of H in Hello is 0 , the index of e is 1 and so on.

(因此,索引HHello0 ,的指数e1等。)

Thus the index of the last character, o is 4 which is equal to "Hello".length() - 1 .

(因此,最后一个字符的索引o4 ,等于"Hello".length() - 1 。)

In the first iteration of the while loop, the condition, if (s.charAt(low) != s.charAt(high)) , compares the first character (ie H ) with the last character (ie o ) of the text, Hello ;

(在while循环的第一次迭代中,条件if (s.charAt(low) != s.charAt(high))将文本的第一个字符(即H )与最后一个字符(即o )进行比较, Hello ;)

because the value of low is 0 and that of high is 4 .

(因为low值为0high值为4 。)

I recommend you understand the basics of Java from a good tutorial eg https://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html

(我建议您从一个好的教程中了解Java的基础知识,例如https://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...