Bookmark Topic Watch Topic
  • New Topic

Boolean task from 'Learnoo' website giving me trouble.  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
Note that a single = means assignment, which is not what you want here.

A double == means comparison.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Report post to moderator
Winston Gutkowski wrote: . . . I think Junilu already basically answered this question in your other thread. . . .
In which case you shouldn't ask the same question twice, so I shall close this thread.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!