我是靠谱客的博主 仁爱便当,这篇文章主要介绍java loop for_Java for loop,现在分享给大家,希望可以做个参考。

OriginaoQuestion:WriteasubroutinenamedisPalindromewhichreturnstrueifthewordpassedasaparameterisapalindromee.g.madamisapalindrome.Writeamainprogramwhichasksausertoenterawo...

Originao Question:Write a subroutine named isPalindrome which returns true if the word passed as a parameter is a palindrome e.g. madam is a palindrome. Write a main program which asks a user to enter a word and prints on the screen if the word is a palindrome or not. The program keeps on asking words until the user presses only Enter.

Hint! If the user presses only Enter the length of the word is 0.

My solution here:

import java.util.Scanner;

public class task5{

public static int isPalindrome(String word){

int length=word.length();

int half=0; //use the half to boolean palindrome

int place=0,place1,place2; //

int i=1;

int cases=length%2;//cases, two cases, one is the length is even and other one is the odd

if(cases==1){ //when it is odd

half=length/2; //eg 5/2=3 becasue of int

place1=half-1-i;

place2=half-1+i;

char textplace1=word.charAt(place1);

char textplace2=word.charAt(place2);

for(i=1;place=0 && textplace1=textplace2;i++){

}

if(place1==0 && textplace1==textplace2) {

System.out.println("It is palindrome");

}

else{

System.out.println("It is not palindrome");

}

}

}

public static void main(String args[]){

Scanner stdin = new Scanner(System.in);

System.out.println("Please enter a word");

String word=stdin.nextLine();

isPalindrome(word);

}

}

Here I just make one case for when it is the even.

---------- Capture Output ----------

> "C:Program FilesJavajdk1.6.0_14binjavac.exe" task5.java

task5.java:17: operator && cannot be applied to int,char

for(i=1;place=0 && textplace1=textplace2;i++){

^

task5.java:17: incompatible types

found : int

required: boolean

for(i=1;place=0 && textplace1=textplace2;i++){

^

2 errors

> Terminated with exit code 1.

Could you tell me why it is not correct?

展开

最后

以上就是仁爱便当最近收集整理的关于java loop for_Java for loop的全部内容,更多相关java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(128)

评论列表共有 0 条评论

立即
投稿
返回
顶部