Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我需要解释一下这个回文_Java_For Loop_Palindrome - Fatal编程技术网

Java 我需要解释一下这个回文

Java 我需要解释一下这个回文,java,for-loop,palindrome,Java,For Loop,Palindrome,有谁能用一个例子来解释一下“for循环”中的这个宫殿吗?我不明白for循环是如何工作的,如果你们能帮我理解,那将是一件伟大的事情 import java.util.*;` public class palindrome { public static void main(String args[]) { String original, reverse = ""; Scanner in = new Scanner(System.in); System.out.println("

有谁能用一个例子来解释一下“for循环”中的这个宫殿吗?我不明白for循环是如何工作的,如果你们能帮我理解,那将是一件伟大的事情

 import java.util.*;`
 public class palindrome {

public static void main(String args[])
{
  String original, reverse = "";


  Scanner in = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome");
  original = in.nextLine();

  int x = original.length();

  for ( int i = x - 1; i >= 0; i-- ){
     reverse  = reverse + original.charAt(i);
  }

   if (original.equals(reverse))
     System.out.println("Entered string is a palindrome.");
  else
     System.out.println("Entered string is not a palindrome.");
    }
    }   

它只是以向后的方式一次读取输入字符串的一个字符,然后将它们转换为空字符串

然后是比较

randomtest
         ^  start here ( x-1 ) cuz x is full lenght, not index

randomtest
     <--^    and go backwards (i--)
随机测试
^从这里开始(x-1),因为x是全长,而不是索引
随机试验

变量X是原始字符串的长度。由于索引从0开始,这意味着最后一个字符的索引是(x-1)。因此,for循环用变量i=x-1初始化,以使循环从字符串的最后一个字符开始

然后,它将索引i处的字符(此时为字符串中的最后一个字符)添加到新的空白字符串中。然后检查i是否大于或等于0(检查字符串是否还有更多字符或是否已到达开头)。如果条件为true(有更多字符需要处理),则将i的值递减1,以获得字符串中的第二个最后一个元素。然后将其添加到反向字符串中,并再次检查。依此类推,直到到达字符串的开头

如果您有输入字符串“FooBar”,它的工作原理如下:

长度=6

i=6-1=5

5>=0吗?对 进入循环

反向+=字符串(5) //反向=“r”

减少一

i=4

4>=0吗?对 反向+=字符串(4) //反向=“ra”

减少一

i=3

//等等,直到我在零度以下

端环

反向=“拉博夫”


这有帮助吗?

关于此代码,您有什么问题?“解释它是如何工作的”不是一个好问题。当你不理解这样一个循环时,用笔和纸一步一步地做。这不是我的代码,我试过用笔和纸。怎么做?我试图理解这一点,请详细说明(inti=x-1;I>=0;I--){因此,如果我的单词是mom,它会读第一个字母吗,因为mom=3和I=x-1….I--for循环声明(
I--
)中的最后一个语句只在循环体之后执行。因此,for循环
for(a;B;C){D;}
相当于
A;而(B){D;C;}
你说的是实话!尽管我越来越担心我们的共同熟人对抽象解释的理解,尽管是他自己的解释。谢谢SIzik,你的评论真的帮了我大忙!你太专业了:谢谢你了!这正是我想要的
 // let's assume 
 // String original = "question" (some random String)
 // this assigns length of the string into int variable x

 int x = original.length(); // x = 8



  // for loop starts here
  // for ex - if length of string "question" is 8 (index will be from 0 to 7)
  // so, it has to starts from last index i.e 7, which is nothing but (8-1) or (length-1) index

  // it will start from i = 7, since 7th character gives last character i.e 'n'
  // so this for loop starts picking up one character from last
  // value of 'i' (index) is decreased every time.


for ( int i = x - 1; i >= 0; i-- ){
     // in this step the character is picked (present at 'i'th index) and 
     // added to the 'reverse' string and overwritten to the 'reverse' object
     // for ex - 
     // if loop starts from i = 7, original.charAt(7) will be n in String "question", then reverse will be reverse ("" (empty string)) + 'n' which is "n"
     // next time reverse will be "n" and i will be = 6, then original.charAt(6) will be = 'o'.
     // since concatenation of reverse and original.charAt(i) has to be assigned to reverse, the new reverse will be = "no" 
     // when i = 5, reverse = "no" + 'i' = "noi"
     // i = 4,  reverse = "noi" + 't' = "noit"
     // i = 3,  reverse = "noit" + 's' = "noits"
     // i = 2,  reverse = "noits" + 'e' = "noitse"
     // i = 1,  reverse = "noitse" + 'u' = "noitseu"
     // i = 0,  reverse = "noitseu" + 'q' = "noitseuq"
     // when i = -1, loop exits, so reverse String will have "noitseuq" after for loop ends
     reverse  = reverse + original.charAt(i);
  }