Need Help With Java Code


#1

import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("+, -, /, or * ");
    String operation = keyboard.nextLine();
    System.out.print("What is the first number? ");
    double num1 = keyboard.nextDouble();
    System.out.print("What is the second number ");
    double num2 = keyboard.nextDouble();
    switch (operation) {
      case "+":
        System.out.print("The answer is ");
        System.out.println(num1 + num2);
        break;
      case "-":
        System.out.print("The answer is ");
        System.out.println(num1 - num2);
        break;
      case "/":
        System.out.print("The answer is ");
        System.out.println(num1 / num2);
        break;
      case "*":
        System.out.print("The answer is ");
        System.out.println(num1 * num2);
        break;
    }
    System.out.print("Did I get it correct? (y or n) ");
    String correct = keyboard.nextLine();
    //The If Statement Doesn't Work
    if (correct.equals("y")) {
      System.out.println("YAY!!!");
    }
    else {
      System.out.println("What!? HOW!?");
    }
  }     
}

#2

i found the answer to your question:

https://stackoverflow.com/questions/5032356/using-scanner-nextline

something i didn’t know either, always good to learn :slight_smile: