字符串:Scanner类的使用



/*
 * 2018年3月30日16:54:07
 * 代码目的:
 *     演示Scanner类的使用。
 * */

//: strings/BetterRead.java
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.*;

public class BetterRead {
	 public static BufferedReader input = new BufferedReader(
			    new StringReader("Sir Robin of Camelot\n22 1.61803"));
  public static void main(String[] args) {
    Scanner stdin = new Scanner(input);
    System.out.println("What is your name?");
    String name = stdin.nextLine();
    System.out.println(name);
    System.out.println(
      "How old are you? What is your favorite double?");
    System.out.println("(input:  )");
    int age = stdin.nextInt();
    double favorite = stdin.nextDouble();
    System.out.println(age);
    System.out.println(favorite);
    System.out.format("Hi %s.\n", name);
    System.out.format("In 5 years you will be %d.\n",
      age + 5);
    System.out.format("My favorite double is %f.",
      favorite / 2);
  }
} /* Output:
What is your name?
Sir Robin of Camelot
How old are you? What is your favorite double?
(input:  )
22
1.61803
Hi Sir Robin of Camelot.
In 5 years you will be 27.
My favorite double is 0.809015.
*///:~

你可能感兴趣的:(Java,编程思想)