我如何在java中执行此代码 [英] How do I do this code in java
问题描述
我需要创建一个随机生成100万个不同车辆登记牌的代码。
盘子需要:
- 包含3个数字
- 由3个字母组成,不包括元音
- 字母和数字需要用破折号分隔
- 随机生成的国家代码需要添加到盘子的末尾,它需要是以下之一(CA,ZN, MP,EC,L,GP,NC,FS,NW)
- 所有内容都需要在GUI中显示,当您按下生成按钮时,它会生成所有内容
我尝试了什么:
我试过创建数组和for循环而我的代码不能正常工作
I need to create a code which randomly generates 1 million different vehicle registration plates.
The plates need:
- to consist of 3 numbers
- to consist of 3 letters excluding vowels
-the letters and numbers need to be separated by a dash
- a randomly generated country code needs to be added to the end of the plate, it needs to be one of the following ( CA, ZN, MP, EC, L, GP, NC, FS, NW)
- everything needs to be displayed in a GUI and when you press the generate button it generates everything
What I have tried:
I have tried creating arrays and for loops and my code is not working
推荐答案
跟踪:使用JFC创建GUI Swing(The Java™Tutorials) [ ^ ],随机(Java平台)表格SE 8) [ ^ ]。
尽管在GUI上一次显示一百万个项目会让你的用户感到烦恼。
Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)[^], Random (Java Platform SE 8 )[^].
Although, showing a million items on the GUI in one go will really annoy your users.
>
假设有五个元音(即考虑'Y
'一个辅音),你可以有
Assuming five vowels (that is considering 'Y
' a consonant), you could have
10 ^ 3 * 21 ^ 3 * 9 = 83349000
不同的盘子。
所以你可以在<$中选择一个随机数c $ c> [0..83348999] 范围并使用它生成板块字符串。我将向您展示代码:
different plates.
So you can choose a random number in the [0..83348999]
range and generate the plate string using it. I am going to show you the code:
import java.util.Random;
class Plate
{
static final int PLATES = 83349000;
static String plate(int n)
{
char [] dgt = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
char [] letter = { 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z' };
String [] cc = { "CA", "ZN", "MP", "EC", "L", "GP", "NC", "FS", "NW" };
String p = "";
if ( n<0 || n >= PLATES) return p;
int rem;
for ( int i = 0; i<3; i++)
{
rem = n % 10;
p += dgt[rem];
n = (n - rem) / 10;
}
p += "-";
for ( int i = 0; i<3; i++)
{
rem = n % 21;
p += letter[rem];
n = (n - rem) / 21;
}
p += "-";
rem = n % 9;
p += cc[rem];
return p;
}
public static void main(String arg[])
{
int n;
Random rand = new Random();
// generate and show 10 random plates (repetition is allowed)
for (n=0; n<10; ++n)
{
int r = rand.nextInt(PLATES);
System.out.printf( "%3d %s\n", (n+1), plate( r) );
}
}
}
现在你必须生成1000000个这样的板块(我假设)避免重复。
你可以使用(如果你的编程盒有足够的内存)这里显示的算法:从甲板上随机抽取5张卡片[ ^ ]来制作技巧。
最后你必须创建一个 GUI
,可能正在使用,如Swing Framework所示。这个艺术步骤非常适合您。
Now you have to generate 1000000 of such plates (and I assume) avoiding repetitions.
You could use (if your programming box has enough memory) the algorithm shown here: Random extraction of 5 cards from a deck[^] to make the trick.
Finally you have to create a GUI
for it, possibly using, as suggested the Swing Framework. This artistic step is very up to you.
这篇关于我如何在java中执行此代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!