2 – Selection Answers

1) Prompt the user as follows: “What’s the capital of France?” Output “Correct” if they enter “Paris”, output “Incorrect” otherwise

Scanner scan = new Scanner(System.in);
System.out.println("What's the capital of France?");
String ans = scan.nextLine();
if(ans.equalsIgnoreCase("Paris"))
{
    System.out.println("Correct");
}
else
{
    System.out.println("Incorrect");
}

2) Prompt the user as follows: “Name a month that starts with the letter A”: Output “Correct” if they enter “April” or “August”, output “Incorrect” otherwise

Scanner scan = new Scanner(System.in);
System.out.println("Name a month that starts with the letter A");
String ans = scan.nextLine();
if(ans.equalsIgnoreCase("April")||ans.equalsIgnoreCase("August"))
{
    System.out.println("Correct");
}
else
{
    System.out.println("Incorrect");
}

3) Prompt the user as follows: “Name a Beatle”. Output “Correct” if they enter “John”, “Paul”, “George” or “Ringo”, output “Incorrect” otherwise

Scanner scan = new Scanner(System.in);
System.out.println("Name a Beatle");
String ans = scan.nextLine();
if(ans.matches("John|Paul|George|Ringo"))
{
    System.out.println("Correct");
}
else
{
    System.out.println("Incorrect");
}

4) An online whisky shop charges for shipping as follows: One bottle, £5.99; two to five bottles, £7; more than five bottles, free. Prompt the user to enter the number of bottles bought and output the shipping cost.

Scanner scan = new Scanner(System.in);
System.out.println("Number bottles purchased:");
int number = scan.nextInt();
if(number == 1)
{
    System.out.println("Shipping £5.99");
}
else if (number <=5)
{
    System.out.println("Shipping £7");
}
else
{
    System.out.println("Shipping Free");
}

5) An online bookshop charges shipping as follows: Orders less than £10, £2.99; orders £10 and over, free; add on £2.50 for all orders if next day delivery is selected. Prompt the user to enter the cost of the order, and then prompt for next day delivery. Output the shipping cost.

Scanner scan = new Scanner(System.in);
System.out.println("Next Day Delivery (y/n)?");
String del = scan.nextLine();
System.out.println("Input order Cost:");
double cost = scan.nextDouble();
double shipping = 0;
shipping = cost < 10 ? 2.99 : 0;
if(del.equals("y"))
{
    shipping += 2.50;
}
System.out.println("Cost of shipping: £" + shipping);

6) Prompt the user to enter a number. Output if the number is odd or even.

Scanner scan = new Scanner(System.in);
System.out.println("Input Number:");
int number = scan.nextInt();
if(number % 2 == 0)
{
    System.out.println("even");
}
else
{
    System.out.println("odd");
}

7) Prompt the user to enter a number. Output Fizz if the number is divisible by 3, otherwise just output the number

Scanner scan = new Scanner(System.in);
System.out.println("Input Number:");
int number = scan.nextInt();
if(number % 3 == 0)
{
    System.out.println("Fizz");
}
else
{
    System.out.println(number);
}

8) Extend yesterday’s problem so that the computer will output Fizz if the number is divisible by 3, output Buzz if the number is divisible by 5 and otherwise just output the number.

Scanner scan = new Scanner(System.in);
System.out.println("Input Number:");
int number = scan.nextInt();
if(number % 3 == 0)
{
    System.out.println("Fizz");
}
else if(number % 5 == 0)
{
    System.out.println("Buzz");
}
else
{
    System.out.println(number);
}

9) Now extend yesterday’s problem further so that the computer will output Fizz if the number is divisible by 3, output Buzz if the number is divisible by 5, output Fizz Buzz if the number is divisible by both 5 and 3 and otherwise just output the number.

Scanner scan = new Scanner(System.in);
System.out.println("Input Number:");
int number = scan.nextInt();
if(number % 3 == 0 && number % 5 ==0)
{
    System.out.println("Fizz Buzz");
}
else if(number % 3 == 0)
{
    System.out.println("Fizz");
}
else if(number % 5 == 0)
{
    System.out.println("Buzz");
}
else
{
    System.out.println(number);
}

Leave a Comment