Example Java user input and output

import java.io.*;
 
class GetUserInput {

 public static void main(String[] args) {
  // the data that will be entered by the user
  String name;
  // an instance of the BufferedReader class
  // will be used to read the data

  BufferedReader reader;
  // specify the reader variable
  // to be a standard input buffer
  reader = new BufferedReader(new InputStreamReader(System.in));
  // ask the user for their name
  System.out.println("What is your name?");
  // read the data entered by the user using
  // the readLine() method of
  // the BufferedReader class
  // and store the value in the name variable

  try {
   name = reader.readLine();
   System.out.println("Your name is " + name);
  } catch (IOException e) {
   //e.printStackTrace();
   System.out.println("An unexpected error occured.");
  }
 }

}

本篇發表於 java。將永久鏈結加入書籤。

發表留言