亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

兩種基本的輸入方式

1.使用Scanner類(lèi)

需要java.util包

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

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

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

例子:

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ù)類(lèi)型為InputStream。通過(guò)read();方法返回ASCII碼,若返回值為-1,說(shuō)明沒(méi)有讀取到任何字符結(jié)束工作。

使用時(shí)需要添加拋出聲明或用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類(lèi),可以用它創(chuàng)建一個(gè)對(duì)象

Scanner input = new Scanner(System.in);

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

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

使用語(yǔ)句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

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

2.2.2 printf

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

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


在這里插入圖片描述

這里的用法與C語(yǔ)言和C++中的類(lèi)似

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();
        // 計(jì)算圓的面積
        double area = 3.14 * radius * radius;
        // 輸出圓的面積,保留兩位小數(shù)
        System.out.printf("%.2f\n",area);   // 注意:在Java中double類(lèi)型用%f輸出(與C語(yǔ)言中的不同)
    }
}

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

相關(guān)文章

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

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

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

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

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

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

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

    詳解SpringBoot封裝使用JDBC

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

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

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

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

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

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

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

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

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

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

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

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

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

最新評(píng)論