programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Boolean task from 'Learnoo' website giving me trouble.

Greenhorn
Posts: 13
I have been given a task on the learneroo website: "You will be given two integer inputs, a and b. Simulate a NAND gate by returning a boolean false only if both inputs are equal to 1, and returning true in all other cases. Try to do this challenge without using any if statements."

Disclaimer I only had to finish existing code so my contribution is in bold:

import java.util.Scanner;

public class Main {

public static boolean doStuff(int a, int b){
if (a=1 && b=1) {
return false;
}
else {
return true;
}

}

//boilerplate code
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

int n = in.nextInt();
for(int i=0; i<n; i++){
int a = in.nextInt();
int b= in.nextInt();
boolean result = doStuff(a, b);
System.out.println(result);
}
}
}

From my internet googling I couldn't see how to use Booleans without if statments so I tried the above solution but it gives me an error code of:

Main.java:6: error: bad operand types for binary operator '&&'
if (a=1 && b=1) {
^
first type: int
second type: int
Main.java:6: error: incompatible types
if (a=1 && b=1) {
^
required: boolean
found: int
2 errors

Any help??

Bartender
Posts: 10575
66
Alex Drummond wrote:From my internet googling I couldn't see how to use Booleans without if statments ...

I think Junilu already basically answered this question in your other thread. Have a look at his post and see if you can't work it out.

Winston

Java Cowboy
Sheriff
Posts: 16060
88
Note that a single = means assignment, which is not what you want here.

A double == means comparison.

Marshal
Posts: 56600
172