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

java – \u0026\u0026运算符问题

发布时间:2020-12-15 02:03:13 所属栏目:Java 来源:网络整理
导读:在下面的代码中在if else语句中给出语法错误,我不确定如何解决它.有人能提供解决方案吗? //Importing scannerimport java.util.Scanner;public class Credithistory { public static void main(String[] args) { //Stating scanner gets its input from con
在下面的代码中&&在if else语句中给出语法错误,我不确定如何解决它.有人能提供解决方案吗?

//Importing scanner
import java.util.Scanner;

public class Credithistory {

    public static void main(String[] args) {            
        //Stating scanner gets its input from console
        Scanner scanner= new Scanner (System.in);           
        //declaring int and boolean         
        int age;
        boolean goodCreditHistory;          
        System.out.println("Please enter your age... ");
        age= scanner.nextInt();         
        System.out.println("Do you have a good credit history (true or flase)?");
        goodCreditHistory= scanner.nextBoolean();           
        if  (age>=18) && ( goodCreditHistory== true){ // <-------------- Error   
            System.out.println("You have got a credit card!");              
        } else 
            System.out.println ("Sorry you are not eligible for a credit card.");
    }       
}

解决方法

这是因为你把括号括起来了. if语句已经由两个括号组成,因此您必须将语句写为

if ((age>=18) && (goodCreditHistory == true))

代替

if (age>=18) && (goodCreditHistory == true)

因为你的陈述的第二部分(&&(goodCreditHistory == true))被解析,好像它是if体的一部分.

您可以用更简洁的方式编写此声明

if(age >= 18 && goodCreditHistory)

不需要额外的括号. == true语句也是多余的.

(编辑:李大同)

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

    推荐文章
      热点阅读