java否则_Java初学者计划帮助(如果,否则问题)

我正在练习制作自己的字符串反转工具。

最重要的是我在一个简单的应用程序中使用了该方法的学习过程。

但..结果

当我在Eclipse中运行我的代码时,

即使单词是=赛车

并逆转成=赛车(反转)。

其他显示..

告诉我这个词和相反是不相等的..但.. ..他们是这样的情况?对?

请给我一些建议,这是非常糟糕的。

谢谢。

import java.util.Scanner;

public class Main {

static Scanner sc = new Scanner(System.in);

public static void main(String[] args){

System.out.println("Please enter a word, I'll show it reversed ");

System.out.println("and tell you if it is read the same forward and backward(A Palindrome)!: ");

String word = sc.nextLine();

String reverse = reverse(word);

if (reverse == word){

System.out.println("Your word: " + word + " backwards is " + reverse + ".");

System.out.println("Your word is read the same forward and backward!");

System.out.println("Its a Palindrome!");

}

else {

System.out.println("Your word: " + word + " backwards is " + reverse + ".");

System.out.println("Your word is not read the same forward and backward!");

System.out.println("Its not a Palindrome!");

}

}

public static String reverse(String source){

if (source == null || source.isEmpty()){

return source;

}

String reverse = "";

for(int i = source.length() -1; i >= 0; i--){

reverse = reverse + source.charAt(i);

}

return reverse;

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值