java怎麼輸入一個字元串
① 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);
}
}
(1)java怎麼輸入一個字元串擴展閱讀:
還可以輸入字元串,輸入字元串的方法
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如何輸入字元串
首先需要引入import java.util.Scanner;
實例化Scanner
Scanner input=new Scanner(System.in);//從控制台輸入,也可以是從文件或者流輸入.
String s1=input.next();
System.out.println("您剛才輸入的字元串是:"+s1) //將輸入的字元串輸出
③ 在java中如何輸入一個char型字元。
方法一:
Scanner cin=new Scanner(System.in);
String s=cin.nextLine();
char ans=s.charAt(0);
這樣即可獲取一個字元。
方法二:
byte[] b=new byte[2];
try{
System.in.read(b)
}catch(Exception e){}
char ans=new String(b).charAt(0);
這樣即可獲取一個字元
④ 怎麼輸入字元串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中如何用鍵盤輸入一個數,字元,字元串
輸入一個數
Scanner in=new Scanner(System.in); //使用Scanner類定義對象
System.out.println("請輸入float型數據");
float a=in.nextFloat(); //接收float型數據
System.out.println(a);
System.out.println("請輸入float型整形數據");
int b=in.nextInt(); //接收整形數據
System.out.println(b);字元串
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
System.out.println("請輸入一串字元串");
String text = buffer.readLine();
System.out.println("您輸入的字元串是:" + text);字元
System.out.println("請輸入一字元");
char c=(char)System.in.read();
System.out.println(c);
⑥ JAVA中怎麼輸入一個字元
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中如何輸入字元串求一個最最基本,最最官方的例子!
你指的是控制台輸入字元串么?
packagecom.test;
importjava.util.Scanner;
publicclassTest001{
publicstaticvoidmain(String[]args){
Scannerscanner=newScanner(System.in);
//此處要求控制台輸入字元串
Stringinput=scanner.next();
//然後列印輸入的字元串
System.out.println("Output:"+input);
scanner.close();
}
}
⑧ 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中,如何從鍵盤輸入到字元串中
BufferedReader類 ------JDK1.4
==========================================
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BoffReader {
public static void main(String[] args) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try {
String s=in.readLine();
System.out.println(s);
} catch (IOException e) {
e.printStackTrace();
}
}
}
==========================================
Scanner類 ----JDK1.5以上
import java.util.Scanner;
public class Scanner1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();//接受鍵盤字元串
System.out.println(s);
}
}
==========================================
===學習中===