Skip to content

Latest commit

 

History

History
61 lines (43 loc) · 1.21 KB

File metadata and controls

61 lines (43 loc) · 1.21 KB

1.3 User Input

User Input Methods

readLine("prompt")

  • Read a line of text from the console

Example:

String colour = readLine("Enter your favourite colour? ");

readInt("prompt")

  • Read an integer from the console

Example:

int numGarage = readInt("Enter the number of garages: ");

readDouble("prompt")

  • Read a double value from the console

Example:

double avg = readDouble("Enter your grade average: ");

readBoolean("prompt")

  • Read as boolean value from the console

Example:

boolean pancakes = readBoolean("Do you like pancakes(true/false)");

General CodeHS User Input Template (from variable declaration)

<dataType> <variableName> = <readCommand>("<prompt>");

Example 1

Create a program UserInput.java that asks for the following information and then outputs the information back to the console

  • name
  • age
  • current mark
  • has been assigned a locker

Example Run

Enter your name: John
Enter your age: 16
Enter your current mark: 92.3
Have you been assigned a locker (true/false): true

Name: John
Age: 16
Current Mark: 92.3
Locker Assigned: true

Example 2

Modify UserInput.java so that it prompts for a first name and last name