導航:首頁 > 編程語言 > java從控制台輸入字元

java從控制台輸入字元

發布時間:2022-08-30 10:38:00

java編程控制台輸入

方法/步驟

❷ java怎樣從控制台輸入字元串到一個字元串數組中!!

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class TestSystemIn {
public static void main(String[] args) throws IOException {
String[] strs = new String[4];

for(int i=0;i<4;i++){
System.out.println("請輸入:");
InputStream input = System.in;
BufferedReader buf = new BufferedReader(new InputStreamReader(input));
String str = buf.readLine();
strs[i] = str;
}

for(int i=0;i<4;i++){
System.out.println("數組第"+i+"個值為:"+strs[i]);
}
}
}

❸ java由控制台輸入字元串多次表達式

Scanner sc = new Scanner(System.in);String reg="^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";String[] k= new String[5];for(int j=0;j<k.length;j++) { k[j]=sc.next();}for (String string : k) { System.out.println(string + "\t\t\t" + (Pattern.matches(reg,string)?"正確":"不正確"));}

❹ java 從控制台輸入字元串,進if後只輸出else 不知道錯在哪裡

原因:字元串String,是對象, 如果要比較字元串的內容是否相同,那麼要使用equals()方法進行.

== 運算符,在java中,是比較兩個對象的內存地址值, 所以並沒有比較內容, 返回的結果不一定能作為字元串內容是否相同的依據

Strings1="ABC";
Strings2="ABC";
Strings3=newString("ABC");
System.out.println(s1==s2);//true說明s1和s2指向內存的同一位置
System.out.println(s1==s3);//false說明s1和s3指向內存的不同位置
System.out.println(s1.equals(s3));//true說明s1和s3的內容是相同的

所以: 修改後的代碼如下

publicstaticvoida(){
Scannerinput=newScanner(System.in);
System.out.print("請輸入品牌:");
Stringb=input.next();
System.out.println(b);
if(b.equals("高露潔")){
System.out.println("牙膏");
}elseif(b.equals("舒膚佳")){
System.out.println("肥皂");
}else{
System.out.println("暫時無其他品牌");

}
}

測試

請輸入品牌:高露潔
高露潔
牙膏

❺ java中怎麼在控制台輸入字元串

工具:

eclipse

方法如下:

輸入代碼如下:

importjava.util.Scanner;

publicclassHelloWorld{

publicstaticvoidmain(String[]args){
System.out.println("控制台輸入字元串開始");
Scannerinput=newScanner(System.in);
Stringinstr=input.nextLine();
System.out.println("控制台輸入字元串結束");
}

}

效果圖

❻ 6. 單選題 (5-1)Java程序中,從控制台輸入一行字元串可以採用( )。 A scanf(「

控制台不用直接exit,不得了

❼ java怎麼在控制台輸入string類型

一、使用標准輸入串System.in
//System.in.read()一次只讀入一個位元組數據,而我們通常要取得一個字元串或一組數字
//System.in.read()返回一個整數
//必須初始化
//int read = 0;
char read = '0';
System.out.println("輸入數據:");
try {
//read = System.in.read();
read = (char) System.in.read();
}catch(Exception e){
e.printStackTrace();
}
System.out.println("輸入數據:"+read);

❽ 如何從java控制台輸入帶空格的字元串

  1. /**按行讀取*/import java.io.*;public class SystemInTest {
    public static void main(String[] args) {BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));String str;try {str = stdin.readLine();System.out.println(str);} catch (IOException e) {e.printStackTrace();}}}

  2. Java控制台輸入帶空格的字元串,讀取的時候只要按照行來讀取,就可以獲取到輸入的空格,下面是示例:/**按行讀取 */import java.io.*;public class SystemInTest {public static void main(String[] args) { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));//建立從控制台輸入的類 String str; try { str = stdin.readLine();//讀取一行 System.out.println(str); } catch (IOException e) { e.printStackTrace(); } }}

  3. importjava.util.Scanner;
  4. publicclassencode2013
  5. {
  6. publicstaticvoidmain(String[] args){
  7. String inString;
  8. String delimiter;
  9. Scanner scan =newScanner(System.in);
  10. inString=scan.nextLine();
  11. delimiter=scan.next();
  12. inString= inString.replace(" ",delimiter);
  13. System.out.println(inString);
  14. }

❾ java怎麼從控制台輸入字元串

//操作步驟看注釋!
importjava.util.Scanner;//必要的包要導入!

publicclass輸入字元串
{
publicstaticvoidmain(String[]args)
{
System.out.println(" ==========java怎麼從控制台輸入字元串!========== ");
init();
}//初始化!
privatestaticvoidinit()
{
Strings;
//循環輸入!
while(true)
{
//提示!
System.out.println("請輸入:");
//掃描控制台!
s=newScanner(System.in).nextLine();
//顯示輸入的字元!
System.out.println("你輸入的是:"+s);
}

}
}

閱讀全文

與java從控制台輸入字元相關的資料

熱點內容
vc6查看編譯的錯誤 瀏覽:595
心理大全pdf 瀏覽:1002
區域鏈加密幣怎麼樣 瀏覽:341
查找命令符 瀏覽:95
壓縮工具zar 瀏覽:735
白盤怎麼解壓 瀏覽:474
辰語程序員學習筆記 瀏覽:47
程序員被公司勸退 瀏覽:523
java三子棋 瀏覽:692
加密空間怎麼強制進入 瀏覽:345
ug分割曲線命令 瀏覽:209
學碼思程序員 瀏覽:609
自考雲學習app為什麼登不上 瀏覽:410
domcer伺服器晝夜更替怎麼搞 瀏覽:436
plc和單片機哪個好 瀏覽:535
帝國神話組建雲伺服器 瀏覽:827
鄧散木pdf 瀏覽:199
方舟怎麼直連伺服器圖片教程 瀏覽:563
假相pdf 瀏覽:336
找對象找程序員怎麼找 瀏覽:976