Java编译器只考虑第一个数字
如果输入是13,编译器将只考虑第一个数字1。 我将向您介绍java代码Java编译器只考虑第一个数字,java,methods,Java,Methods,如果输入是13,编译器将只考虑第一个数字1。 我将向您介绍java代码 class Max { static void checkFever(double choice) { // if temperature is less than 37, print "You have fever" if (choice > 37) { System.out.println("You ha
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
}
}
输出如下:
38
You have fever.
3
使用包装
系统的扫描器
(这是一个输入流
)读取解析为适当原语或对象数据类型的整行数据。在您的情况下,温度int
就足够了,但最好是十进制float
或double
Scanner scanner = new Scanner(System.in);
double choice = scanner.nextDouble();
checkFever(choice);
System.out.println(choice);
您的代码无法工作,因为您使用方法读取单个字节
从输入流读取数据的下一个字节。值字节作为0到255范围内的整数返回
使用包装系统的扫描器
(这是一个输入流
)读取解析为适当原语或对象数据类型的整行数据。在您的情况下,温度int
就足够了,但最好是十进制float
或double
Scanner scanner = new Scanner(System.in);
double choice = scanner.nextDouble();
checkFever(choice);
System.out.println(choice);
您的代码无法工作,因为您使用方法读取单个字节
从输入流读取数据的下一个字节。值字节作为0到255范围内的整数返回
System.in.read()
只读取单个字符。您可以通过java.util.Scanner
helper类读取整行内容,因此:
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
}
}
如果用户没有输入有效的整数,则会出现NumberFormatException
。请随意改进代码。:-)
(帮助:尝试{…}catch(…)
)System.in.read()
只读取单个字符。您可以通过java.util.Scanner
helper类读取整行内容,因此:
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
}
}
如果用户没有输入有效的整数,则会出现NumberFormatException
。请随意改进代码。:-)
(帮助:try{…}catch(…)
)char
是一个单个字符,您可能希望读取整个字符串,或者更可取的是双精度或浮点编译器没有这样做,程序没有这样做。程序之所以这样做是因为read()
读取一个字节;对于文本输入,3
是一个字节,7
是另一个字节。你能在checkFever方法的开头做一个System.out.println
吗?char
是一个单个字符,你可能想读一个完整的字符串
或者更可取的是一个双精度或者浮点值编译器没有这么做,这个节目很有趣。程序之所以这样做是因为read()
读取一个字节;对于文本输入,3
是一个字节,7
是另一个字节。你能在checkFever方法的开头做一个System.out.println
吗?为了补充这个答案,你的输入被转换为char
,所以38
被解读为'3'
。将其转换为double时,它是51.0
。要添加到此答案,您的输入将转换为char
,因此38
将被读取为'3'
。将其转换为双精度时,它是51.0
。