哈氏蜈蚣有什么作用:JAVA初级编程的小问题

来源:百度文库 编辑:高校问答 时间:2024/05/07 12:00:45
import java.io.*;
class jex{
public static void main(String args[])throws IOException{
System.out.println("输入你的成绩:");
char a=(char)System.in.read();
char b=(char)System.in.read();
int s=(a-'0')*10+b-'0';
if(s>=60)
System.out.println("pass!");
else
System.out.println("lose!");
}
}

程序中“int s=(a-'0')*10+b-'0';”改为“int s=a*10+b;”也可以用,书上的“-‘0’”有什么其他的作用!~~

为了取得整数,比如输入85,'8' - '0'就是ASCII表中字符'8'和'0'的距离,就是8,'5' - '0'就是ASCII表中字符'8'和'0'的距离,就是5,所以8 * 10 + 5 = 85。

你若改成int s = a * 10 + b;则是字符a的ASCII值乘以10再加上b的ASCII值,这样你即使输入10结果也是pass

-'0'就是为了把char专成int
char型数字的ascii码比int型的数字多48,刚好等于'0'的ascii码

上面的回答不错!我就不补充喽!