import java.io.*;
public class IfElseDemo1
{ public static void main(String[] args) throws IOException// throws IOException
{
byte x;
System.out.println("请输入一个数(0~9)");
x=(byte)System.in.read();
x-=48;
if(x>0) System.out.println(x+"的平方根为:"+Math.sqrt(x));
}
}
运行结果:
请输入一个数(0~9)
9
9的平方根为:3.0
请问x-=48为什么没有运行?求解答,谢谢!
实际上是算了的,一开始输入的时候,是输入的字符,9的ascii值实际上是57,对其byte强制转换后他还是57,你用int转换也一样的,这时候你再减去了48,其值变成了9,开方后是3。代码没有问题,你要理解ascii字符值的含义,:)