dimanche 11 juin 2017

Re prompt user input [duplicate]

What I am trying to achieve with this program, is to make the program re-prompt the user to input their mood via the scanner, if it reaches the 'else' statement of incorrect input.

Also I am recieving the error 'cannot find symbol' for the method mood = nextString(), and also name = nextString(). Why is this?

Thank you for you assistance, I am still learning java.

import java.util.Scanner;

public class Test {

  static final Scanner scan = new Scanner(System.in);

  public static void main(String[] args) {

    String name;
    String mood;
    int age;

    System.out.println("Welcome New User");

    System.out.println("I want to learn about you, what is your name?");
    name = scan.nextString();
    scan.close();

    System.out.println("Welcome " + name  + ", how are you feeling today, good or bad?");
    mood = scan.nextString();

    if (mood.equalsIgnoreCase("bad")) {
      System.out.println(name + ", I am sorry to hear that your day is bad");
    }
    else if (mood.equalsIgnoreCase("good")) {
      System.out.println(name + ", I am very pleased to hear that you are good");
    }
    else {
      System.out.println("PLEASE RE-ENTER YOUR INPUT");
    }
    scan.close();
  }
}

Aucun commentaire:

Enregistrer un commentaire