Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用带有while和嵌套if/else语句的扫描仪测试输入精度_Java_If Statement_While Loop - Fatal编程技术网

Java 使用带有while和嵌套if/else语句的扫描仪测试输入精度

Java 使用带有while和嵌套if/else语句的扫描仪测试输入精度,java,if-statement,while-loop,Java,If Statement,While Loop,因此,我尝试使用while循环继续请求输入,而用户对随机数的输入不等于随机数生成器的输出。但是,当我输入数字时,较高/较低的输出不起作用。它要么总是说它的高,要么总是说它的低,不管实际的数值是多少。帮忙 import java.util.Random; import java.util.Scanner; public class GuessingGame { public static final int MAX = 100; public static void main(S

因此,我尝试使用while循环继续请求输入,而用户对随机数的输入不等于随机数生成器的输出。但是,当我输入数字时,较高/较低的输出不起作用。它要么总是说它的高,要么总是说它的低,不管实际的数值是多少。帮忙

import java.util.Random;
import java.util.Scanner;

public class GuessingGame {
    public static final int MAX = 100;
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Random rand = new Random();
        int rand1 = rand.nextInt(MAX);
        introduction();
        game(scan, rand1);
    }
    public static void introduction() {
        System.out.println("This program allows you to play a guessing game."     
            " I will think of a number between 1 and" +
            " 100 and will allow you to guess until" +
            " you get it.  For each guess, I will tell you" +
            " whether the right answer is higher or lower" +
            " than your guess");
    }
    public static void game(Scanner scan, int rand1) {
        System.out.println("I'm thinking of a number between 1 and 100...");
        System.out.println(rand1);
        System.out.println("Your guess?");
        int input = scan.nextInt();
        while(input != rand1) {
            if (input < rand1) {
                System.out.println("It's higher");
                scan.nextInt(input);
            }
            else if (input > rand1) {
                System.out.println("It's lower");
                scan.nextInt(input);
            }
         }
    }
}
import java.util.Random;
导入java.util.Scanner;
公开课猜谜游戏{
公共静态最终整数最大值=100;
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
Random rand=新的Random();
int rand1=rand.nextInt(最大值);
导言();
游戏(扫描,随机1);
}
公共静态无效介绍(){
这个程序允许你玩猜谜游戏
“我会想到一个介于1和之间的数字”+
“100并允许您猜测,直到”+
“你明白了。每猜一次,我都会告诉你”+
“正确答案是高还是低”+
“比你猜的要多”);
}
公共静态无效游戏(扫描仪扫描,int rand1){
System.out.println(“我想的是一个介于1和100之间的数字…”);
系统输出打印项次(rand1);
System.out.println(“你的猜测?”);
int input=scan.nextInt();
while(输入!=rand1){
如果(输入<随机数1){
System.out.println(“更高”);
scan.nextInt(输入);
}
否则如果(输入>随机1){
System.out.println(“较低”);
scan.nextInt(输入);
}
}
}
}

开始时键入的输入值不会改变以后键入的内容。 解决方案: 您需要使用
input=scan.nextInt()
而不是
scan.nextInt(输入)


希望我能帮忙!:)

下面是用于读取下一个int并更改循环内输入值的方法的javadoc:。这看起来像你想做的吗?你第一次做对了。你为什么不在循环中做同样的事情呢?