Java – \ u0026 \ u0026 operator problem

In the following code & & the syntax error is given in the if else statement. I'm not sure how to solve it Can anyone provide a solution?

//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.");
    }       
}

Solution

This is because you enclosed the brackets The if statement already consists of two parentheses, so you must write the statement as

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

replace

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

Because the second part of your statement (& & (goodcredithistory = = true)) is parsed as if it were part of the if body

You can write this declaration in a more concise way

if(age >= 18 && goodCreditHistory)

No additional parentheses are required== The true statement is also redundant

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>