java - 检查用户是否输入特定数据
问题描述
我想检查用户是否输入了特定的单词/字母,以便我可以根据他/她输入的内容运行一些代码。
这是我的代码:
import java.util.concurrent.ThreadLocalRandom;
import java.util.Scanner;
public class EligibilityTest {
public static void main(String[] args) {
Scanner temperature = new Scanner(System.in);
System.out.println("Press the 'T' key to check your temperature. ");
double tempMeasure = ThreadLocalRandom.current().nextDouble(96, 98.9);
// Here I would like to add an if statement to check if the user has input the 'T', and if yes, print the tempMeasure
}
}
如何构造此 if 语句,以及如何将变量“tempMeasure”的十进制值保持为两位数?我发现 Java 输入非常复杂,因为我是在 Python 之后开始学习的,这相对容易得多。
解决方案
需要添加
scan.nextLine()
检索用户的输入。然后你可以写 if 语句:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Press the 'T' key to check your temperature. ");
double tempMeasure = ThreadLocalRandom.current().nextDouble(96, 98.9);
String temperature = scan.nextLine();
if(temperature.equals("T")){
System.out.println(tempMeasure);
}
}
推荐阅读
- tensorflow - 用于 Microsoft Cognitive CNTK/TensorFlow 的 VGA 卡 (GPU)
- jenkins - 将多个节点添加到 Jenkins master
- c# - C# SOAP 响应格式错误
- opencart - 如何在所有页面上添加商店名称?
- jquery - 无法使用 ajax 将复选框值保存到数据库中
- android - 如何在 Android Studio 和 IntelliJ 中安装颤振和飞镖?
- networking - 将 DHT 用于八卦协议?
- spring - Spring ResourceBundleMessageSource 多个属性文件配置在 spring xml 文件中?
- audiokit - 是否可以将输入重置为 AudioKit AKNode?
- angular - 在 Visual Studio 2017 中使用 Angular 2 设置 Angular 材质类型