import java.util.Scanner;
import java.io.*;
public class Main {
public static void main(String[] args){
Scanner userQuestion = new Scanner(System.in); // Gets user input using Scanner
System.out.println("\u001B[34m" + "What color is this text?");
String colorGuess = userQuestion.nextLine(); // Sets user input equal to new variable colorGuess
System.out.print("Your guess is: " + colorGuess);
if (colorGuess.equalsIgnoreCase("blue")) { // Uses String method equalsIgnoreCase to check if the two strings are identical. The result is a boolean expression that if true will execute the if statement.
System.out.print(" which is correct!"); // When the answer is correct, this statement is printed
}
}
}
Main.main(null);
If-Else Statements
If the code statement in the if is true then the code within the the if statement will execute. If the code statement in the if is false then the code within the else statement will execute. The example below uses if and else statements to check if the response to a question is correct or not.
import java.util.Scanner;
import java.io.*;
public class Main {
public static void main(String[] args){
Scanner userQuestion = new Scanner(System.in); // Gets user input using Scanner
System.out.println("\u001B[34m" + "What color is this text?");
String colorGuess = userQuestion.nextLine(); // Sets user input equal to new variable colorGuess
System.out.print("Your guess is: " + colorGuess);
if (colorGuess.equalsIgnoreCase("blue")) { // Uses String method equalsIgnoreCase to check if the two strings are identical. The result is a boolean expression that if true will execute the if statement.
System.out.print(" which is correct!"); // When the answer is correct, this statement is printed
}
else {
System.out.print(" which is incorrect!"); // If the statement is incorrect, the else statement will execute and tell the user that their answer is incorrect.
}
}
}
Main.main(null);
If-Else-If-Else Statements
This option of conditionals is helpful if there are multiple options that are correct. If the code statement in the if is true, then the code segment under the if statement will execute. If the code statement in the if is false, then the code segment in the else if statement will execute if it is true. If the code segment in the else if statement is false, then the code segment in the else statement will execute.
import java.util.Scanner;
import java.io.*;
public class Main {
public static void main(String[] args){
Scanner userQuestion = new Scanner(System.in); // Gets user input using Scanner
System.out.println("\u001B[34m" + "What color is this text?");
String colorGuess = userQuestion.nextLine(); // Sets user input equal to new variable colorGuess
System.out.print("Your guess is: " + colorGuess);
if (colorGuess.equalsIgnoreCase("blue")) { // Uses String method equalsIgnoreCase to check if the two strings are identical. The result is a boolean expression that if true will execute the if statement.
System.out.print(" which is correct!"); // When the answer is correct, this statement is printed
}
else if (colorGuess.equalsIgnoreCase("cyan")) {
System.out.print(" which is correct!"); // This is another possible correct answer, so this statement will be printed if the user does not guess "blue"
}
else {
System.out.print(" which is incorrect!"); // If the statement is incorrect, the else statement will execute and tell the user that their answer is incorrect.
}
}
}
Main.main(null);
import java.util.Scanner;
import java.io.*;
public class Main {
public static void main(String[] args){
Scanner userQuestion = new Scanner(System.in); // Gets user input using Scanner
System.out.println("\u001B[34m" + "What color is this text?");
String colorGuess = userQuestion.nextLine(); // Sets user input equal to new variable colorGuess
System.out.print("Your guess is: " + colorGuess);
if (colorGuess.equalsIgnoreCase("blue")) { // Uses String method equalsIgnoreCase to check if the two strings are identical. The result is a boolean expression that if true will execute the if statement.
System.out.print(" which is correct!"); // When the answer is correct, this statement is printed
}
else if (colorGuess.equalsIgnoreCase("cyan")) {
System.out.print(" which is correct!"); // This is another possible correct answer, so this statement will be printed if the user does not guess "blue"
}
else if (colorGuess.equalsIgnoreCase("red")) {
System.out.print(" which is incorrect!"); // This is another possible incorrect answer, so this statement will be printed if the user does not guess "blue"
}
else if (colorGuess.equalsIgnoreCase("green")) {
System.out.print(" which is incorrect!"); // This is another possible incorrect answer, so this statement will be printed if the user does not guess "blue"
}
else if (colorGuess.equalsIgnoreCase("yellow")) {
System.out.print(" which is incorrect!"); // This is another possible incorrect answer, so this statement will be printed if the user does not guess "blue"
}
else if (colorGuess.equalsIgnoreCase("torquise")) {
System.out.print(" which is correct!"); // This is another possible correct answer, so this statement will be printed if the user does not guess "blue"
}
else if (colorGuess.equalsIgnoreCase("orange")) {
System.out.print(" which is incorrect!"); // This is another possible incorrect answer, so this statement will be printed if the user does not guess "blue"
}
else {
System.out.print(" which is incorrect!"); // If the statement is incorrect, the else statement will execute and tell the user that their answer is incorrect.
}
}
}
Main.main(null);
public class Main {
public static void main(String[] args){
Scanner userQuestion = new Scanner(System.in); // Gets user input using Scanner
System.out.println("\u001B[34m" + "What color is this text?");
String colorGuess = userQuestion.nextLine(); // Sets user input equal to new variable colorGuess
System.out.print("Your guess is: " + colorGuess);
String response;
switch (colorGuess) { // Depending on what the user guesses, the case that matches the color guessed will execute.
case "blue":
response = " which is correct!";
break;
case "cyan":
response = " which is correct!";
break;
case "purple":
response = " which is incorrect!";
break;
case "turquise":
response = " which is correct!";
break;
case "yellow":
response = " which is incorrect!";
break;
case "red":
response = " which is incorrect!";
break;
case "green":
response = " which is incorrect!";
break;
case "orange":
response = " which is incorrect!";
break;
default:
response = " which is not a valid response!";
break;
}
System.out.print(response);
}
}
Main.main(null)
De Morgan's Law
De Morgan's law shows how numbers are related through their opposites. According to De Morgan's Law logic, "the complement of the union of two sets is equal to the intersection of their separate complements." In code the definition is "if two (or more) input variables are AND’ed and negated, it should be equivalent to the OR of the complements of the individual input variables"
public class Main {
public static void main(String[] args){
boolean a = false;
boolean b = false;
if (!(a&&b)){
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("Test 1: True");
}
}
}
Main.main(null)
public class Main {
public static void main(String[] args){
boolean a = false;
boolean b = false;
if (!a || !b){
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("Test 2: True");
}
}
}
Main.main(null)