有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Make if==带有扫描程序nextInt的语句

我想做一个配对游戏,游戏机上会显示随机数,然后你必须输入相同的数字。我对if语句有一个问题,即使我输入了正确的数字,它也显示不正确。以下是我目前的代码:

package MatchGame;

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

public class match2 {
    int a;
    int b;
    int c;
    int d;

    String countdown[] = {
        "3...",
        "2...",
        "1..."
    };

    public match2() throws InterruptedException {
        set1();
    }

    public void set1() throws InterruptedException {
        Scanner s = new Scanner(System.in);
        System.out.println("press ENTER for your first set...");
        s.nextLine();

        for (int i = 0; i < countdown.length; i++) {
            Thread.sleep(1000);
            System.out.println(countdown[i]);
        }

        a = number();
        b = number();
        c = number();
        d = number();

        System.out.print(a);
        System.out.print(b);
        System.out.print(c);
        System.out.print(d);
        int set = a + b + c + d;
        int guess = s.nextInt();
        {
            if(set == guess) {
                System.out.println("Nice bruh +1");
            }
            else {
                System.out.println("Nope");
            }
        }   
    }

    public static int number(){
        Random r = new Random();
        int match = r.nextInt(9) + 1;
        //rv = rv + match;
        return match;
    }
}

此外,变量“set”似乎不包括变量a、b、c和d


共 (1) 个答案

  1. # 1 楼答案

    一个简单的方法是将你的值和输入存储到两个数组,两个单独的数组中,然后进行比较