英文:
Java compiler just takes into account the first digit
问题
如果输入为13,则编译器只会考虑第一个数字1。
我会给你Java代码。
class Max {
static void checkFever(double choice) {
// 如果温度低于37度,打印出"你发烧了"
if (choice > 37) {
System.out.println("你发烧了。");
}
else {
System.out.println("你没发烧。");
}
}
public static void main(String[] args) throws IOException {
char choice;
choice = (char) System.in.read();
checkFever(choice);
System.out.println(choice); // 这会显示结果
}
}
输出如下:
38
你发烧了。
3
英文:
If the input is 13 the compiler will just take into account the first digit that is 1.
I'll let you the java code.
class Max {
static void checkFever(double choice) {
// if temperature is less than 37, print "You have fever"
if (choice > 37) {
System.out.println("You have fever.");
}
else {
System.out.println("You don't have fever.");
}
}
public static void main(String[] args) throws IOException {
char choice;
choice = (char) System.in.read();
checkFever(choice);
System.out.println(choice); // this will make appear the result
}
}
the output is the following:
38
You have fever.
3
答案1
得分: 2
使用包装了System.in
(这是一个InputStream
)的Scanner
来读取整行输入,并将其解析为适当的基本数据类型或对象数据类型。在你的情况下,对于温度,int
应该已经足够,但是推荐使用十进制的 float
或者 double
。
Scanner scanner = new Scanner(System.in);
double choice = scanner.nextDouble();
checkFever(choice);
System.out.println(choice);
你的代码不起作用,因为你使用了 InputStream::read
方法来读取单个字节。
> 从输入流中读取下一个字节的数据。返回的值是一个范围在 0 到 255 之间的 int 值。
英文:
Use a Scanner
that wraps the System.in
(which is an InputStream
) to read the whole line parsed into an appropriate primitive or object data type. In your case for the temperature int
would be sufficient but a decimal float
or double
is preferred.
Scanner scanner = new Scanner(System.in);
double choice = scanner.nextDouble();
checkFever(choice);
System.out.println(choice);
Your code doesn't work because you read a single byte using method InputStream::read
> Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255.
答案2
得分: 1
System.in.read()
只读取一个单独字符。你可以通过java.util.Scanner
辅助类来读取整行,因此:
import java.io.IOException;
import java.util.Scanner;
class Max {
static void checkFever(double choice) {
// 如果体温低于37度,打印“你发烧了”
if (choice > 37) {
System.out.println("你发烧了。");
}
else {
System.out.println("你没有发烧。");
}
}
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
String answer = input.nextLine();
int temp = Integer.parseInt(answer);
checkFever(temp);
System.out.println(answer); // 这将显示结果
}
}
如果用户未输入有效的整数,这会产生NumberFormatException
。随意改进代码。
(帮助:try{...} catch(...)
)
英文:
System.in.read()
reads only a single character. You can read a whole line by the java.util.Scanner
helper class, so:
import java.io.IOException;
import java.util.Scanner;
class Max {
static void checkFever(double choice) {
// if temperature is less than 37, print "You have fever"
if (choice > 37) {
System.out.println("You have fever.");
}
else {
System.out.println("You don't have fever.");
}
}
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
String answer = input.nextLine();
int temp = Integer.parseInt(answer);
checkFever(temp);
System.out.println(answer); // this will make appear the result
}
}
This will give a NumberFormatException
if the user did not enter a valid integer. Feel free to improve the code.
<sub>(Help: try{...} catch(...)
)</sub>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论