最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

相關(guān)文章

最新評論

合水县| 华安县| 新野县| 衡山县| 扎鲁特旗| 呼伦贝尔市| 山东省| 青海省| 延长县| 大洼县| 临澧县| 孙吴县| 颍上县| 广州市| 昌宁县| 南漳县| 那曲县| 昌都县| 洛南县| 宜兰市| 井陉县| 静安区| 阿鲁科尔沁旗| 塔城市| 玛纳斯县| 福清市| 苏州市| 鞍山市| 葵青区| 铜山县| 蒲城县| 韩城市| 武鸣县| 盐城市| 育儿| 离岛区| 鹤庆县| 上思县| 延寿县| 延川县| 华安县|