I am trying to create a program that prompts the user to enter a name (1 of three options x,y,or z) and a certain variable (that is less than 10). If both of these inputs are true it will print out the name and the number as : (" you choose x times 2")
I am stuck because I'm new to the java scanner class (and java) and don't know how to configure the code with if statements checking two factors.
Help?
My code so far: I know a lot of it wrong:
import java.util.Scanner;
public class ScannerAndKeyboard {
public static void main(String[] args)
{ Scanner s = new Scanner(System.in);
System.out.print("Welcome");
System.out.print( "Enter your name: " );
String name = s.nextLine();
System.out.println( "Hello " + name + "!" );
System.out.print("Please Enter letter and number: " );
if(s.nextLine().equals(X) && s.nextInt() <= 10)
System.out.println( "You choose x10");
}
}
This should work for you:
import java.util.Scanner;
public class ScannerAndKeyboard {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Welcome");
System.out.print( "Enter your name: " );
String name = s.nextLine();
System.out.println( "Hello " + name + "!" );
System.out.print("Please Enter letter and number: " );
String X = s.useDelimiter("\\s").next();
int num = s.nextInt();
if((X.equalsIgnoreCase("x") || X.equalsIgnoreCase("y") ||
X.equalsIgnoreCase("z")) && num < 10)
System.out.println( "You choose "+X+num);
}
}
I tested by verifying the following inputs (you can modify the prompts):
WelcomeEnter your name: Alvin
Hello Alvin!
Please Enter letter and number: X 9
You choose X9
From your code, I don't see a variable named X
. So I'm assuming you want a character comparison.
if(s.nextLine().equals("X") && s.nextInt() <= 10)
Wrap X
with double quotes and it should solve your problem.
EDIT:
If you need to add a variable and check for x,y or z
then do the following:
import java.util.Scanner;
public class ScannerAndKeyboard {
public static void main(String[] args)
{ Scanner s = new Scanner(System.in);
System.out.print("Welcome");
System.out.print( "Enter your name: " );
String name = s.nextLine();
System.out.println( "Hello " + name + "!" );
System.out.print("Please Enter letter and number: " );
String X = s.nextLine();
if((X.equalsIgnoreCase("x") || X.equalsIgnoreCase("y") || X.equalsIgnoreCase("z")) && s.nextInt() <= 10)
System.out.println( "You choose "+X+" 10");
}
}
EDIT 2
If they are to be in the same line, then do this:
import java.util.Scanner;
public class ScannerAndKeyboard {
public static void main(String[] args)
{ Scanner s = new Scanner(System.in);
System.out.print("Welcome");
System.out.print( "Enter your name: " );
String name = s.nextLine();
System.out.println( "Hello " + name + "!" );
boolean stat = true;
while(boolean)
System.out.print("Please Enter letter and number: " );
String X = s.nextLine();
String[] X1 = X.split(" ");
if((X1[0].equalsIgnoreCase("x") || X1[0].equalsIgnoreCase("y") || X1[0].equalsIgnoreCase("z")) && Integer.parseInt(X1[1]) <= 10)
System.out.println( "You choose "+X1[0]+" "+X1[1]);
stat = false;
}
else {
System.out.println("Invalid Input!");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.