欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中兩種基本的輸入方式小結(jié)

 更新時間:2022年05月18日 10:53:24   作者:g28_gerwulf  
這篇文章主要介紹了Java中兩種基本的輸入方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

兩種基本的輸入方式

1.使用Scanner類

需要java.util包

構(gòu)造Scanner類的對象,附屬于標(biāo)準(zhǔn)輸入流System.in,之后通過其中的方法獲得輸入。

常用的方法:nextLine();(字符串),nextInt();(整型數(shù)),nextDouble();(雙精度型數(shù))等等。

結(jié)束時使用close();方法關(guān)閉對象。

例子:

import java.util.*;
?
class IOTest {
?? ?public static void main(String args[]) {
?? ??? ?Scanner sc = new Scanner(System.in);
?? ??? ?System.out.println("enter your name:");
?? ??? ?String name = sc.nextLine();
?? ??? ?System.out.println("enter your age:");
?? ??? ?int age = sc.nextInt();
?? ??? ?System.out.println("enter your occupation:");
?? ??? ?String occ = sc.next();
?? ??? ?System.out.println("name:" + name + "\n" + "age:" + age + "\n" + "occupation:" + occ);
?? ??? ?sc.close();
?? ?}
}

輸入:
enter your name:
g28
enter your age:
20
enter your occupation:
student
輸出:
name:g28
age:20
occupation:student

2.使用System.in.read();方法

需要java.io包。

System.in從標(biāo)注輸入獲取數(shù)據(jù),數(shù)據(jù)類型為InputStream。通過read();方法返回ASCII碼,若返回值為-1,說明沒有讀取到任何字符結(jié)束工作。

使用時需要添加拋出聲明或用try/catch包圍。

例子:

import java.io.*;
class IOTest {
?? ?public static void main(String args[]) {
?? ??? ?int c;
?? ??? ?System.out.println("please enter the string:");
?? ??? ?try {
?? ??? ??? ?while((c = System.in.read()) != -1)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?System.out.print((char)c);?
?? ??? ??? ??? ?}
?? ??? ?} catch (IOException e) {
?? ??? ??? ?System.out.println(e.toString());
?? ??? ?}
?? ?}
}

輸入:
please enter the string:
My name is g28.
輸出:
My name is g28.

輸入與輸出的使用講解

1.輸入

Java的輸入,我們用到Scanner類,可以用它創(chuàng)建一個對象

Scanner input = new Scanner(System.in);

然后input對象調(diào)用nextBoolean(),nextByte(),nextShort(),nextInt(),nextLong(),nextFloat(),nextDouble()方法來從輸入流中獲取數(shù)據(jù)。

package com.company;		// 包
import java.util.Scanner;
public class code {
    public static void main(String[] args){
        // 掃描對象,用來掃描系統(tǒng)的輸入
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();        // 輸入一個整型
        short b = input.nextShort();    // 輸入一個短整型
        long c = input.nextLong();      // 輸入一個長整型
        byte d = input.nextByte();      // 輸入一個字節(jié)型
        float f = input.nextFloat();    // 輸入一個單精度浮點(diǎn)型
        double g = input.nextDouble();  // 輸入一個雙精度浮點(diǎn)型
        // 輸入字符串
        // nextLine() 和 next()都可以錄入String型的,但是next()遇到空格就終止了,nextLine()可以把空格和空格后面的全部錄入
        String s = input.nextLine();    // 錄入一行,回車是終止符
        String ss = input.next();       // 遇到空格或回車都會終止·
        // 輸入一個char類型
        // 獲得用戶輸入字符串的第一個字符
        char ch = input.next().charAt(0);
    }
}

?多組輸入:

import java.util.Scanner; 
public class Mian { 
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);	// cin是自己定義的,這個是任意的
		while (cin.hasNext()) {
			int a = cin.nextInt();
			int b = cin.nextInt();
			System.out.println(a + b);
		}
	}
}

?T組輸入:

// 使用while循環(huán)
import java.util.Scanner; 
public class Mian {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);	// cin是自己定義的,這個是任意的
		int T = cin.nextInt();
		while (T>0) {
			int a = cin.nextInt();
			int b = cin.nextInt();
			System.out.println(a + b);
			T--;
		}
	}
}
// 使用for循環(huán)
import java.util.Scanner; 
public class Mian {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);	// cin是自己定義的,這個是任意的
		int T = cin.nextInt();
		for(int i=0;i<T;i++)
		 {
			int a = cin.nextInt();
			int b = cin.nextInt();
			System.out.println(a + b);
		}
	}
}

2.輸出

2.1.1 println直接輸出

使用語句System.out.println()輸出,System.out.println()為輸出并換行。

package com.company;
public class code {
    public static void main(String[] args){
        System.out.println("Hello World");
    }
}

2.1.2 println輸出變量

package com.company;
public class code {
    public static void main(String[] args){
        int num = 10;
        System.out.println("num的值為:" + num);
    }
}

輸入num的值并且輸出

package com.company;
import java.util.Scanner;
public class code {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        System.out.println("num的值為:" + num);
    }
}

2.2.1 print

使用語句System.out.print()輸出,System.out.print()為輸出但是不會換行,如果想要換行需要\n。print()與println()的作用類似,都是輸出,但唯一不同的是print()不會換行。

2.2.2 printf

jdk1.5新增了和C語言中printf函數(shù)類似的數(shù)據(jù)輸出方法,

System.out.printf(“格式控制部分”,表達(dá)式1,表達(dá)式2,……,表達(dá)式n);


在這里插入圖片描述

這里的用法與C語言和C++中的類似

package com.company;
import java.util.Scanner;
public class code {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        System.out.printf("num的值為:%d\n" , num);
    }
}

3.輸入輸出實(shí)例

輸入圓的半徑,求圓的面積()

package com.company;
import java.util.Scanner;
public class code {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        // 輸入圓的半徑
        double radius = input.nextDouble();
        // 計算圓的面積
        double area = 3.14 * radius * radius;
        // 輸出圓的面積,保留兩位小數(shù)
        System.out.printf("%.2f\n",area);   // 注意:在Java中double類型用%f輸出(與C語言中的不同)
    }
}

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Java設(shè)計模式之單例模式

    詳解Java設(shè)計模式之單例模式

    這篇文章主要為大家詳細(xì)介紹了Java設(shè)計模式之單例模式的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • 基于java socket實(shí)現(xiàn) 聊天小程序

    基于java socket實(shí)現(xiàn) 聊天小程序

    這篇文章主要介紹了基于java socket實(shí)現(xiàn) 聊天小程序,代碼分為服務(wù)器和客戶端,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • Java實(shí)現(xiàn)提取QSV文件視頻內(nèi)容

    Java實(shí)現(xiàn)提取QSV文件視頻內(nèi)容

    QSV是一種加密的視頻文件格式。是愛奇藝公司研發(fā)的一種視頻文件格式,這篇文章主要為大家介紹了如何利用Java實(shí)現(xiàn)提取QSV文件視頻內(nèi)容,感興趣的可以了解一下
    2023-03-03
  • 詳解SpringBoot封裝使用JDBC

    詳解SpringBoot封裝使用JDBC

    這篇文章主要介紹了SpringBoot封裝JDBC使用教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12
  • MyBatis獲取自動生成的(主)鍵值的方法

    MyBatis獲取自動生成的(主)鍵值的方法

    本文主要介紹了MyBatis獲取自動生成的(主)鍵值的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • 必知必會的SpringBoot實(shí)現(xiàn)熱部署兩種方式

    必知必會的SpringBoot實(shí)現(xiàn)熱部署兩種方式

    這篇文章主要為大家介紹了必知必會的SpringBoot實(shí)現(xiàn)熱部署兩種方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • 利用Thumbnailator輕松實(shí)現(xiàn)圖片縮放、旋轉(zhuǎn)與加水印

    利用Thumbnailator輕松實(shí)現(xiàn)圖片縮放、旋轉(zhuǎn)與加水印

    java開發(fā)中經(jīng)常遇到對圖片的處理,JDK中也提供了對應(yīng)的工具類,不過處理起來很麻煩,Thumbnailator是一個優(yōu)秀的圖片處理的開源Java類庫,處理效果遠(yuǎn)比Java API的好,這篇文章主要介紹了利用Thumbnailator如何輕松的實(shí)現(xiàn)圖片縮放、旋轉(zhuǎn)與加水印,需要的朋友可以參考下
    2017-01-01
  • Java中的.concat()方法實(shí)例詳解

    Java中的.concat()方法實(shí)例詳解

    concat()方法用于將指定的字符串參數(shù)連接到字符串上,.concat()方法是一種連接兩個字符串的簡單方法,可以幫助我們在Java中處理字符串,對java .concat()方法用法感興趣的朋友一起看看吧
    2024-01-01
  • java中如何獲取時間戳的方法實(shí)例

    java中如何獲取時間戳的方法實(shí)例

    時間戳通常是一個字符序列,唯一地標(biāo)識某一刻的時間,所以下面這篇文章主要給大家介紹了關(guān)于java中如何獲取時間戳的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • java實(shí)現(xiàn)把兩個有序數(shù)組合并到一個數(shù)組的實(shí)例

    java實(shí)現(xiàn)把兩個有序數(shù)組合并到一個數(shù)組的實(shí)例

    今天小編就為大家分享一篇java實(shí)現(xiàn)把兩個有序數(shù)組合并到一個數(shù)組的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05

最新評論