㈠ 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中怎樣在終端輸入一個字元串、字元、數字。
假設你說的第一個字元串是a,第二個是b
判斷a中是否有一個字元或者一段字元串包含於b中:
boolean
ifcontrain
=
false;
for(int
i
=
0
;
i
<
a.length
-
1
;
i
++
)
{
for(int
j
=
i
+
1
;
j
<
a.length
;
j++
)
{
if(b.contains(a.substring(i
,
j
)))
{
ifcontrain
=
true;
}
}
}
最後看ifcontrain是true,則包含,是false,就是不包含。
如果想要看包含的是哪段,就在ifcontrain
=
true;一句後面再加一句
輸出
a.substring(i
,
j
)
就行了。
㈢ 在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);
}
}
==========================================
===學習中===
㈣ 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中輸入數字夾字元
可以聲明string類型的變數,scan到以後再轉換為其他類型
㈥ 在java中如何輸入一個字元形參
可以創建Scanner類來從鍵盤輸入一個字元,用String類型來接收,再使用String的charAt功能,就可以輸入字元了。
㈦ 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中如何輸入一個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語言中輸入單個字元
Scanner sc = new Scanner(System.in); String s = sc.next(); //Scanner類沒有提供直接接收一個字元的方法,這里當作字元串來接收; char c = s.charAt(0); // 調用字元串的charAt()方法取得第一個字元就是了
㈩ 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);