加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java if()不起作用

发布时间:2020-12-15 02:04:00 所属栏目:Java 来源:网络整理
导读:当我运行一些代码(稍后显示)时,我告诉它检查字符串==“1”,如果是,输出“它是1
当我运行一些代码(稍后显示)时,我告诉它检查字符串==“1”,如果是,输出“它是1!”,否则输出字符串.

码:

double shirt3 = Math.random() * 9;
String shirt2 = Double.toString(shirt3);
char shirt1 = shirt2.charAt(0);
String shirt = Character.toString(shirt1);

if(shirt == "1") {
    System.out.println("It's 1!");
} else {
    System.out.println(shirt);
}

输出:

7
4
8
1
7
1
7
7
6
0

解决方法

你需要使用

if (shirt.equals("1"))
    ...

这将比较String对象的实际内容,而不是它们的身份.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读