我想在一个数组中生成6个数字,同时对它进行比较,因此它不会相同或没有重复数字.例如,我想以任何顺序生成1-2-3-4-5-6,最重要的是不重复.所以我想的是逐个比较生成的数组中的当前数组,如果数字重复,它将重新运行该方法并再次随机化一个数字,这样就可以避免重复数字.
这是我的代码:
import javax.swing.*;
public class NonRepeat
{
public static void main(String args[])
{
int Array[] = new int [6];
int login = Integer.parseInt(JOptionPane.showInputDialog("ASD"));
while(login != 0)
{
String output="";
for(int index = 0; index<6; index++)
{
Array[index] = numGen();
for(int loop = 0; loop <6 ; loop++)
{
if(Array[index] == Array[loop])
{
Array[index] = numGen();
}
}
}
for(int index = 0; index<6; index++)
{
output += Array[index] + " ";
}
JOptionPane.showMessageDialog(null, output);
}
}
public static int …Run Code Online (Sandbox Code Playgroud) 我有一个代码,其中有一个v默认值为0 的变量.我也有2个按钮:ok和nope.使用我的代码,当ok按下时,值v应为1,当nope按下时,值v应为2.但在这两种情况下,它打印的值为v2.为什么这样?我该如何纠正?
编辑:我犯了一个小错误,我已经纠正了,多亏了MadProgrammer.
import java.awt.*;
public class chk extends java.applet.Applet
{
Button ok = new Button("OK!");
Button nope = new Button("Nope");
int v = 0;
public void init()
{
setBackground(Color.white);
add(ok);
add(nope);
}
public boolean action(Event evt , Object arg)
{
if(evt.target instanceof Button)
{
check((Button)evt.target);
return true;
}
return false;
}
public void check(Button b)
{
if(b == ok);
{
v= 1;
repaint(); …Run Code Online (Sandbox Code Playgroud)