---
# System prepended metadata

title: '[Java] 輸入輸出'
tags: [input/output, Java]

---

# [Java] 輸入輸出

---
Java程式的輸入、輸出大致上包含以下幾類:
1. 輸入整數
2. 輸入浮點數
```java=
import java.util.*;
import java.lang.*;
import java.io.*;
 
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		/*input 整數*/
		Scanner scan = new Scanner(System.in);
		int num = scan.nextInt();
		System.out.println(num);
 
		/*input 浮點數*/
		float float1 = scan.nextFloat();
		System.out.println(float1);
	}
}
```
```
stdin:
10
4.999
stdout:
10
4.999
```
3. 輸入字串(包含空白鍵、tab)
```java=
import java.util.*;
import java.lang.*;
import java.io.*;
 
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		/* 輸入字串(包含空白鍵、tab) */
		Scanner scan = new Scanner(System.in);
		String str1 = scan.nextLine();
		System.out.println(str1);
	}
}
```
```
stdin:
test 000
stdout:
test 000
```
4. 輸入字串(不包含空白鍵、tab)
```java=
import java.util.*;
import java.lang.*;
import java.io.*;
 
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		/* 輸入字串(不包含空白鍵、tab) */
        Scanner scan = new Scanner(System.in);
		String str2 = scan.next();
		System.out.println(str2);		
	}
}
```
```
stdin:
test 000
stdout:
test
```
5. 只取輸入的第一個字元
```java=
import java.util.*;
import java.lang.*;
import java.io.*;
 
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		/* 只取輸入的第一個字元 */
		Scanner scan = new Scanner(System.in);
		char ch = scan.next().charAt(0);
		System.out.println(ch);
	}
}
```
```
stdin:
abc
stdout:
a
```


###### tags: `Java` `input/output`