本文目錄一覽:
java中怎麼中鍵盤輸入字元串
首先,導入java.util.*包。
import java.util.*;
然後,你需要新建一個讀取標準輸入(鍵盤)的掃描器對象。
Scanner in = new Scanner(System.in);
現在,你可以從鍵盤輸入字元串了。
String s = in.nextLine();
以上這一行把鍵盤輸入的一行字元串讀取到變數 s 中。
請看一個完整的簡單示例:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String s = in.nextLine();
System.out.println(s);
}
}
Java的常用輸入輸出語句?
常用的輸入語句是:
輸入字元串:new Scanner(System.in).next();
輸入整數:new Scanner(System.in).nextInt();
輸入小數:new Scanner(System.in).nextDouble();
常用的輸出語句:
換行輸出: System.out.println(變數或字元串);
非換行輸出: System.out.print(變數或字元串);
換行輸出錯誤提示(默認是紅字):System.err.println(變數或字元串);
不換行輸出錯誤提示(默認是紅字): System.err.print(變數或字元串));
java中如何輸入一個字元
import java.util.*;
public class Test_01
{
public static void main(String[] args)throws Exception
{
System.out.println(“請輸入一個字元”);
char c=(char)System.in.read();
System.out.println(c);
}
}
擴展資料:
還可以輸入字元串,輸入字元串的方法
import java.io.*;
public class Test
{
public static void main(String[] args) throws IOException
{
BufferedReader buf = new BufferedReader (new InputStreamReader(System.in));
BufferedWriter buff = new BufferedWriter(new FileWriter(“abc.txt”));
String str = buf.readLine();
while(!str.equals(“exit”))
{
buff.write(str);
buff.newLine();
str = buf.readLine();
}
buf.close();
buff.close();
}
}
Java中怎麼輸入一個字元?(用char來定義)
1.先創建一個scanner對象
2.調用scanner對象的next()方法獲取控制台輸入,返回的是一個string類型,因為沒有nextchar()方法
3.調用string的charat(0)方法獲取第一個字元
scanner
sc
=
new
scanner(system.in);
string
s
=
sc.next();
char
c
=
s.charat(0);
在java中如何輸入一個字元形參
可以創建Scanner類來從鍵盤輸入一個字元,用String類型來接收,再使用String的charAt功能,就可以輸入字元了。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/204326.html