gpt4 book ai didi

java - 声称不相等的值,即使它们是相等的

转载 作者:行者123 更新时间:2023-12-02 04:39:23 24 4
gpt4 key购买 nike

我正在制作一个简单的乐透游戏,但是当我输入一些数字并将骰子设置为只有 1(100% 获胜机会)时,我仍然得到失败的结果。另外我怎样才能让厄运声明只打印一次?我知道你可以创建一个 boolean 值等,但是有更简单的方法吗?

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

public class Main {

public static void main(String[] args) {
int num = 0;
int randnum = 0;
Random gen = new Random();
Scanner scan = new Scanner(System.in);
System.out.println("Enter desired numbers to be drawn");
int print = scan.nextInt();
System.out.println("Enter desired numbers on dice");
int dice = scan.nextInt();
System.out.println("Enter your lucky numbers");
int[] numbers = new int[print];
for(int i=0; i<print;i++){
numbers[i] = scan.nextInt();
}
for(int counter=1; counter<=print;counter++){
randnum = 1+gen.nextInt(dice);
System.out.println(randnum + " ");
System.out.println();
if (randnum == num){
System.out.println("Winner winner chicken dinner!");
} else
System.out.println("Bad luck!");
}
}
}

最佳答案

问题很简单。您没有将 randnum 与正确的变量进行比较。您要做的是将您的条件替换为:

       if (randnum == numbers[counter - 1]){
System.out.println("Winner winner chicken dinner!");
} else
System.out.println("Bad luck!");
}

关于java - 声称不相等的值,即使它们是相等的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30356046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com