工预-java3
文章目錄
- JAVA基本計算方法和文件讀取的應用
- A. 編程實現一個命令窗程序,使得:
- 代碼
- B. 編程實現一個命令窗程序,使得:
- 代碼
- C. 編程實現一個命令窗程序,使得:
- 代碼
JAVA基本計算方法和文件讀取的應用
Java中獲取鍵盤輸入值,Scanner類。
equals,對比。
A. 編程實現一個命令窗程序,使得:
輸入“A”則在屏上回顯“Your input is A”輸入“我”則在屏上回顯“Your input is 我”等等。輸入ByeBye則退出程序.代碼
import java.util.Scanner;//鍵盤輸入語法 public class HX1 { public static void main(String[] args)throws java.io.IOException {Scanner cen=new Scanner(System.in);//鍵盤輸入while(true)//循環{System.out.println("input your word "); String str=cen.nextLine();//鍵盤輸入if(!str.equals("byebye"))//鍵盤輸入內容對比{System.out.println(" your word is "+str);} else{System.out.println("end");System.exit(0);}}} }編譯結果
運行結果
B. 編程實現一個命令窗程序,使得:
輸入“A”則在屏上回顯A字符的ASCII碼。輸入“4”則在屏上回顯4字符的ASCII碼。輸入“我”則在屏上回顯“我”字的漢字內碼。等等。
輸入ByeBye則退出程序.代碼
public static void main(String[] args)throws java.io.IOException{Scanner cen=new Scanner(System.in);//鍵盤輸入while(true)//循環{System.out.println("input your word "); String str=cen.nextLine();//鍵盤輸入if(!str.equals("byebye"))//鍵盤輸入內容對比{System.out.println((int)str.charAt(0)); //這個語法可以強行轉換為ASCII碼} else{System.out.println("end");System.exit(0);}}}}編譯結果
運行結果
C. 編程實現一個命令窗程序,使得:
輸入“你”則在屏上回顯“you”。輸入“書”則在屏上回顯“book”。輸入“中”則在屏上回顯“middle”。輸入“中國”則在屏上回顯“China”。...要能輸入至少100個詞。如輸入沒有記錄的詞則如下:輸入“東東”則在屏上回顯“查不到該詞”。輸入ByeBye則退出程序.(提示: 單詞字典應做一個文本文件讀入,其中每行為:<中文字詞><對應英文> )代碼
import java.io.BufferedReader; import java.io.IOException; import java.util.Scanner; import java.io.FileReader;public class HX3 {public static void main(String[] args) throws java.io.IOException {Scanner cen = new Scanner(System.in);// 鍵盤輸入while (true) {System.out.println("input your word ");String str = cen.next();// 鍵盤輸入FileReader word = new FileReader("E:\\javap\\dic.txt");// 讀取文本BufferedReader t = new BufferedReader(word);String c = "";// 一個空字符串boolean h=false;//定義布爾型變量用來查不到文本的詞while ((c = t.readLine()) != null) // 讀每行文本{// 得到文本的漢字,英文String chinese = c.substring(c.indexOf("<") + 1, c.indexOf(">"));String english = c.substring(c.lastIndexOf("<") + 1, c.lastIndexOf(">"));if (str.equals(chinese)) {// 對比中文,得出英文System.out.println(english);h=true;}if (str.equals("byebye")) {// 輸入byebye結束System.out.println("end");System.exit(0);}if(!h) {System.out.println("查不到該詞");}}}} }編譯結果
運行結果
參考
https://me.csdn.net/yga_airspace
https://me.csdn.net/atishoo_13
https://me.csdn.net/yangchenju
總結
- 上一篇: 工预-SQLite(weiwan)
- 下一篇: 工预-java4