java - 我试着做一个猜谜游戏,让用户决定猜的最大数字是多少
问题描述
为了检查用户输入的值并查看计算出的随机数,我想让这些数字投影在控制台(eclipse)中。但是什么勾勒了我的惊讶?输入数字后,最后两个 system.out.println(就在 invoermax.close()) 上方)没有出现在控制台屏幕中????这就像java甚至没有阅读或注意到这些代码行,怎么会???
这是我的代码:
package Package1;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
public class test6KOPIE1 {
public static void main(String[] args)
{
Scanner Invoermax = new Scanner(System.in);
System.out.println("Under which number you want to guess");
int Invoer = Invoermax.nextInt();
int Hoogte = ThreadLocalRandom.current().nextInt(1,Invoermax.nextInt());
System.out.println("So a guess under max: " + Invoer);
System.out.println("The random number has become " + Hoogte);
Invoermax.close();
}
}
解决方案
每次您调用Scanner.nextInt()
它都会等待您的输入。
问题是你调用它两次,替换:
int Hoogte = ThreadLocalRandom.current().nextInt(1,Invoermax.nextInt());
使用您已经获得的变量:
int Hoogte = ThreadLocalRandom.current().nextInt(1,Invoer);
顺便说一句,通常在 java 中,字段/变量名称以小写字母开头,所以应该是hoogte
,inoverMax
等inover
。
推荐阅读
- android - 在 ListView 中重复项目?它不能正常工作,第一项显示在第三个位置,并再次从第一项重复
- php - php session.save_path 没有价值
- android - 如何使用 FirebaseRecyclerAdapter 显示来自 firebase 实时数据库的所有数据
- java - if循环内的表达式评估
- sql - 格式化数据以在 SQL Server 中生成线性回归
- scala - UDF 取两个参数的 spark 数据帧
- css - CSS Flexbox - 具有可滚动内容的浮动可变高度页脚
- android - gradle 无法从 artifactory 解析 .aar
- c# - 在存储过程中插入 DataTable 时重复条目插入
- python - 如何在python中实现适用于N维特征向量的GMM聚类EM算法(期望最大化算法)