79.2k views
4 votes
Write an if-else statement that prints "Goodbye" if userString is "Quit", else prints "Hello". End with newline.

2 Answers

5 votes

import java.util.Scanner;

public class DetectWord {

public static void main (String [] args) {

Scanner scnr = new Scanner(System.in);

String userString;

userString = scnr.next();

if(userString.equals("Quit")){

System.out.println("Goodbye");

}

else{

System.out.println("Hello");

}

}

}

This actually works for anything written and will provide the proper outcome for zybooks

User LoveAndHappiness
by
5.2k points
0 votes

If you are using Java then the IF statement would be:

If(userSting == "Quit"){

System.out.println("Goodbye");

}

else

System.out.println("Hello");

The IF statement will check whether the users input is the word "Quit". If the word is indeed "Quit" then the program will output the word "Goodbye". The ELSE statement on the other hand will only be displayed if the user inputs any other string than "Quit". The program will then output the word "Hello" any other string or number is inputted by the user.

User Steve Hawkins
by
5.3k points