java的輸入和輸出

這學期學java,剛剛寫了個小程序才發現輸入輸出都不會,看書+百度總算知道了,小小的總結一下;

java輸入字符串:

     Scanner in=new Scanner(System.in);

     String str=new String();

     str=in.next();

輸入單個字符:

     Scanner in=new Scanner(System.in);

     String str=new String();

     str=in.next();

     char ch;

     ch=str.charAt(0);//ch即爲單個字符

輸入整形和浮點型變量:

     Scanner in=new Scanner(System.in);

     int x=in.nextInt();//輸入整形

     float f=in.nextFloat();//輸入浮點型


輸出調用函數System.out.print();或者System.out.println();後者輸出會自動換行

輸出字符串:

     System.out.print(".......");

輸出變量:

     System.out.print(x);

輸出字符串和變量,用加號連接:

     System.out.print("..."+x+"...");


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章