+ 2
How to create a program that ask for your name and only says hello when your name is Alice or Bob
im using java
5 Answers
+ 3
That's right Shamima. Also you should use equalsIgnoreCase() just incase the user inputs one of the names in small casing. Should be something like:
if (name.equalsIgnoreCase("Alice") || name.equalsIgnoreCase("Bob"))
{
System.out.println("Hello");
}
+ 15
Use equals method to compare Strings instead of ==
if(name.equals("Alice") || name.equals("Bob")){
// do your stuff
}
+ 3
Scanner sc = new Scanner(System.in)!
String name = sc.nextLine();
if(name == "Alice" || name == "Bob") {
System.out.print("Hello");
}
put that code into your public static void main don't forget to import java.util.Scanner;
+ 2
#python3
name = input("What is your name?")
print(' ')
if name == 'Alice' or name == 'Bob':
print('Hello ')
+ 2
grab the user input via system.in,
wrap it in an if statement like this if(input=="Alice"||input=="Bob"{ under greet message here }
now you might want to make all letters lowercase in case input is like aLiCE and you still want to execute code