這篇文章主要介紹java輸入輸出語句怎么寫,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

在java中,輸入語句為“Scanner對象.next()系列方法”,例“Scanner對象.nextLine()”表示輸入字符串;輸出語句為“System.out.println()”、“System.out.print()”等。
對于經常上機刷題的來說,首先得解決輸入輸出方法,Java的輸入輸出流在Java學習過程的后面部分才會接觸,但是我們可以掌握一些簡單的,常用的輸入輸出方法
輸出流
java常用的輸出語句有下面三種:
System.out.println();//換行打印,輸出之后會自動換行
System.out.print();//不換行打印
System.out.printf();//按格式輸出
輸出示例
public class test {
public static void main(String []args){
System.out.println(1111);//換行打印,輸出后自動換行
System.out.print(1111);//不換行打印
System.out.printf("分數是:%d",88);//按格式輸出
}
}輸入流
java的輸入需要依賴Scanner類:
import java.util.Scanner;
如果需要輸入,則先聲明一個Scanner對象:
Scanner s = new Scanner(System.in);
Scanner附屬于輸入流System.in,聲明Scanner對象之后,在輸入的時候需要使用next()方法系列指定輸入的類型,如輸入整數、輸入字符串等。
常用的next()方法系列:
nextInt():輸入整數
nextLine():輸入字符串
nextDouble():輸入雙精度數
next():輸入字符串(以空格作為分隔符)。
輸入示例
import java.util.Scanner;
public class test {
Scanner s = new Scanner(System.in); // 聲明Scanner的一個對象
System.out.print("請輸入名字:");
String name = s.nextLine();
System.out.println(name);
System.out.print("請輸入年齡:");
int age = s.nextInt();
System.out.println(age);
System.out.print("請輸入體重:");
double weight = s.nextDouble();
System.out.println(weight);
System.out.print("請輸入學校:")
String school = s.next();
System.out.println(school);
s.close(); // 關閉輸入流,若沒有關閉則會出現警告
}
}輸出如下:
請輸入名字:梁 十 安 梁 十 安 請輸入年齡:18 18 請輸入體重:70.5 70.5 請輸入學校:xxx大學 阿斯頓 xxx大學
以上是java輸入輸出語句怎么寫的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注創新互聯-成都網站建設公司行業資訊頻道!
網站名稱:java輸入輸出語句怎么寫-創新互聯
鏈接地址:http://www.yijiale78.com/article0/ceijio.html
成都網站建設公司_創新互聯,為您提供手機網站建設、品牌網站制作、網站策劃、外貿網站建設、企業網站制作、外貿建站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯