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