久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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

這篇文章主要介紹了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.輸入輸出實例

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

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)驗,希望能給大家一個參考,也希望大家多多支持html5模板網(wǎng)。

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

主站蜘蛛池模板: 午夜天堂精品久久久久 | 国产精产国品一二三产区视频 | 日韩精品久久久久久 | 欧美日韩在线视频一区 | 高清国产午夜精品久久久久久 | 国产目拍亚洲精品99久久精品 | 台湾佬成人网 | 精品乱码一区二区三四区 | 91久久夜色精品国产网站 | 久久精品亚洲精品国产欧美kt∨ | 亚洲精品久久久久久一区二区 | 精品视频在线免费观看 | 91在线观看 | 欧美精品中文字幕久久二区 | 九九热国产视频 | 超碰成人免费观看 | 男人的天堂亚洲 | 欧美在线视频不卡 | 国产精品视频在线观看 | 日本精品视频 | 欧美日韩精品一区二区三区蜜桃 | av网站免费看| 精品1区2区| 亚洲精品一区二区另类图片 | 精品久久久久久久久久久久 | 国产高清一区二区三区 | 国产三级在线观看播放 | 欧美日韩一区二区三区四区 | 黄色一级视频 | 新超碰97| 午夜午夜精品一区二区三区文 | 欧美激情国产精品 | 国产成人精品一区二区三区四区 | 天天干天天操 | www.精品一区 | 秋霞电影一区二区三区 | 精品国产精品一区二区夜夜嗨 | 亚洲黄色片免费观看 | 亚洲一区二区视频 | 久久国产一区二区三区 | 国产亚洲一区二区精品 |