gpt4 book ai didi

java - 图像不断重复,尽管它已经是随机的

转载 作者:太空宇宙 更新时间:2023-11-04 12:08:47 24 4
gpt4 key购买 nike

我正在开发一个简单的游戏,其中所有图像都是随机的。我注意到尽管我输入了随机代码,但有些图像是重复的。我对 Java 还是个新手。我希望有人能帮助我解决我的问题。下面是我的编码。

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Random;
import javax.swing.*;

public class Task1 extends JFrame implements KeyListener,ActionListener {
JFrame frame = new JFrame("FYP");
JTextField textField = new JTextField();
JButton btnNext = new JButton("NEXT");

int sum=0;
int Error=0;int total_test = 0;
static String inputID;
static int index;
String[] imgFileHP = {"1.jpg","3.jpg","4.jpg","7.jpg","9.jpg","10.jpg","12.jpg","16.jpg","17.jpg","18.jpg"};
String[] imgNo = {"5","4","6","3","5","3","4","4","6","6"};
int randomNo;
Random rand = new Random();

public Task1(String inputID)
{
frame.setSize(2200,2500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.getContentPane().setLayout(new BorderLayout(0, 0));

Task(inputID);
}

public void Task(String inputID)
{
JPanel panel = new JPanel();
JLabel labelUsername = new JLabel("");

frame.getContentPane().add(panel, BorderLayout.CENTER);
panel.setBackground(Color.WHITE);

Collections.shuffle(Arrays.asList(imgFileHP));
Set<Integer> uniqueList = new HashSet<Integer>();//This would create list with the number 0 to 9
for(int count=0;count<imgFileHP.length;count++){
uniqueList.add(count);
labelUsername.setIcon(new ImageIcon(getClass().getResource("/image/" + imgFileHP[count])));
if(!uniqueList.isEmpty()){
index = (int) (Math.random() * (upper - lower)) + lower;

if(uniqueList.contains(index)){

uniqueList.remove(index);//particular number is delete from the list so that duplicate images doesnt show up
System.out.println(imgFileHP[r]);//This printf statement is just for your reference
}
}


}
textField.setText("");
textField.setColumns(10);
textField.addKeyListener(this);

btnNext.addActionListener(this);

panel.add(labelUsername);
panel.add(textField);
panel.add(btnNext);
frame.setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{

if(!textField.getText().equals("")){
total_test += 1;
if(isNumeric(textField.getText())){
//********************Correct Integer**********************
if(Integer.valueOf(imgNo[randomNo])==Integer.valueOf(textField.getText())){
//********************Correct Answer**********************
System.out.println("Correct");
sum+=1;
}else{
//********************Incorrect Answer**********************
System.out.println("Incorrect");
Error+=1;
}
refreshFrame();
}else{
//********************Incorrect Integer/Alphabet**********************
System.out.println("Invalid");
Error+=1;
refreshFrame();
}
}else{
System.out.println("Null Input");
}
//System.out.println(Integer.valueOf(imgNo[randomNo]));
}
public void refreshFrame(){
if(total_test>=10){
// add result page to see how many score
//Task2(sum, Error);
System.out.println("Correct: "+sum+" Incorrect: "+Error);
frame.dispose();
}else{
btnNext.removeActionListener(this);
frame.getContentPane().removeAll();
getContentPane().removeAll();
Task(inputID);
}

}

public static void main(String args[]) {
Task1 a = new Task1(inputID);
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub

}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}

public static boolean isNumeric(String str)
{
try
{
Integer.valueOf(str);
}
catch(NumberFormatException nfe)
{
return false;
}
return true;
}

}

编辑部分(但有些图像仍然重复。)

Collections.shuffle(Arrays.asList(imgFileHP));
Set<Integer> uniqueList = new HashSet<Integer>();//This would create list with the number 0 to 9
for(int count=0;count<imgFileHP.length;count++){
uniqueList.add(count);
}

if(!uniqueList.isEmpty()){
index = (int) (Math.random() * (upper - lower)) + lower;
randomNo = index;
if(uniqueList.contains(index)){
labelUsername.setIcon(new ImageIcon(getClass().getResource("/image/" + imgFileHP[index])));
uniqueList.remove(index);//particular number is delete from the list so that duplicate images doesnt show up
System.out.println(imgFileHP[r]);//This printf statement is just for your reference
}
}

最佳答案

这是随机的工作原理。即使您提到了rand.nextInt(10);,但每次调用它时,java中都没有预定义的规则,它必须生成唯一的数字,而不是与先前生成的数字重复。

例如:你打电话时 -int 索引 = rand.nextInt(10);//索引可以是2索引 = rand.nextInt(10);//现在该值也可以是 2

所以你需要做的是,你必须添加一段额外的代码来检查每次调用 random 时的唯一性。

既然你想生成0到10之间的数字,试试下面的代码,它可能会帮助你理解。

String[] imgFileHP = {"1.jpg","3.jpg","4.jpg","7.jpg","9.jpg","10.jpg","12.jpg","16.jpg","17.jpg","18.jpg"};
int upper = imgFileHP.length;
int lower = 0;
int r=0;
int index=0;

Set<Integer> uniqueList = new HashSet<Integer>();//This would create list with the number 0 to 9
for(int count=0;count<imgFileHP.length;count++){
uniqueList.add(count);
}



if(!uniqueList.isEmpty()){
index = (int) (Math.random() * (upper - lower)) + lower;

if(uniqueList.contains(index)){

uniqueList.remove(index);//particular number is delete from the list so that duplicate images doesnt show up
System.out.println(imgFileHP[r]);//This printf statement is just for your reference
}
}

另一种更简单的方法是使用 Collections.Shuffle,如下所示:

Collections.shuffle(Arrays.asList(imgFileHP));

Collection.shuffle() 示例。

假设您有一个数组String[] arr = {"abc","def","xyz","bla"}

当你这样做时 Collections.shuffle(Arrays.asList(arr));

然后从索引 0 到 3 打印数组。数组可能会被打乱,如下所示:{"def","bla","abc","xyz"}

**编辑2:**基于您在主代码中编辑的解决方案:

  for(int count=0;count<imgFileHP.length;count++){//first add the counters into List 
uniqueList.add(count);
}


if(!uniqueList.isEmpty()){
index = (int) (Math.random() * (upper - lower)) + lower;

if(uniqueList.contains(index)){
labelUsername.setIcon(new ImageIcon(getClass().getResource("/image/" + imgFileHP[index])));
uniqueList.remove(index);//particular number is delete from the list so that duplicate images doesnt show up
System.out.println(imgFileHP[r]);//This printf statement is just for your reference
}
}

关于java - 图像不断重复,尽管它已经是随机的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40072158/

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