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

如果输入是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 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