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 Files\Java\jdk1.6.0_14\bin\javac.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?

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值