adsense

2010-05-22

java 使用者 輸入 scanner

import java.util.Scanner;

public class test

{

  public static void main(String[] args)

    {

      Scanner scanner = new Scanner (System.in);

          System.out.print("please input your name: ");

          System.out.printf("hello! %s", scanner.next());

      }

}

//printf 直接輸出
//print 印

進階 加上 try catch

import java.util.Scanner;
public class test
{
 public static void main(String[] args)
 {
   Scanner scanner = new Scanner (System.in);
  try
   {
    System.out.print("請輸入姓名: ");
    System.out.printf("hello! %s", scanner.next());
    System.out.println(" ");
   }
  catch(Exception a)
   {
    System.out.println("您不可輸入空白");
   }
 
  try
   {
    System.out.print("請輸入數字: ");
    System.out.printf("輸入的數字為: %d!!",scanner.nextInt());
   }
  catch(Exception a)
   {
    System.out.println("您必須輸入數字");
   }
 }
}

沒有留言:

張貼留言