当前位置: 首页 > 知识库问答 >
问题:

随机生成车牌

楚煜
2023-03-14
static String[] checklist = {"FOR", "AXE", "JAM", "JAB", "ZIP", "ARE", "YOU", "JUG", "JAW", "JOY"};
public static void main (String[] args)
{
    int letterRange=26;
    int numberRange=10;

    String  x;
    String letters = new String(Letters(letterRange));
    int numbers = Numbers(numberRange);

    System.out.println(" The randomly generated License Plate is: " +letters + "-" +numbers);
}

public static char[] Letters(int lRange)
{
    char[] letters = new char[3];
    Random r = new Random();
    boolean match = false;
    boolean resultOk = false;
    String result;
    //Generating Random strings (character array)
    for (int x=0; x<letters.length; x++)
    {
        letters[x] = (char)(r.nextInt(26) + 65);
    }

    //Logic for possibility exclusion
    while (true)
    {
        if (match == true)
        {
            for (int x=0; x<letters.length; x++)
            {
                letters[x] = (char)(r.nextInt(26) + 65);
            }
        }
        result = new String(letters);
        for (int i = 0; i < checklist.length; i++)
        {
            if (result == checklist[i])
            {
                match = true;
                break;
            }
            if ((i == checklist.length - 1) && (match == false))
            {
                resultOk = true;
                break;
            }
        }
        if (resultOk == true)
        {
            break;
        }
    }
    return letters;

}

public static int Numbers(int nRange)
{
    int result = 0;
    int[] numbers = new int[3];
    Random r = new Random();
    for (int x = 0; x < numbers.length; x++)
    {
        numbers[x] = r.nextInt(nRange);
    } 
    for (int i = numbers.length; i >= 1; i--)
    {
        result += numbers[i-1] * (10 * (numbers.length - i));
    }
    return result;
}  

基本上,我正在尝试创建一个随机的车牌,其中包含三个大写字母(65,ASCII码)和三个数字。当我运行程序时,我得到了一个异常,即静态int z=Integer。parseInt(y) 。所以基本上我所做的是,我将字符串数组转换为字符串,然后将字符串转换为int,再将int转换为char,然后我做了一个while循环(如果字母不等于b),那么它应该可以工作。

你能帮帮我吗?还有,我应该有两种方法吗?我想用一个盒子把车牌围起来,让它看起来很好看。

共有3个答案

方浩旷
2023-03-14

这就是我为班级做的,直截了当的不。。。混乱

public class LicensePlate {
   public static void main(String[] args) {

    // Generate three random uppercase letters
    int letter1 = 65 + (int)(Math.random() * (90 - 65));
    int letter2 = 65 + (int)(Math.random() * (90 - 65));
    int letter3 = 65 + (int)(Math.random() * (90 - 65));    

    // Generate four random digits
    int number1 = (int)(Math.random() * 10);
    int number2 = (int)(Math.random() * 10);
    int number3 = (int)(Math.random() * 10);

    // Display number plate
    System.out.println("" + (char)(letter1) + ((char)(letter2)) + 
        ((char)(letter3)) + "-" + number1 + number2 + number3);
    }
}
宗政颖逸
2023-03-14

让我们从您的数组开始:

static String[] x = {"FOR", "AXE", "JAM", "JAB", "ZIP", "ARE", "YOU", "JUG", "JAW", "JOY"};

它包含禁止的单词。那很好,您不需要除此之外的任何东西。无需创建包含数组长度的变量z或包含某种的变量b来自此数组的char

现在谈谈主要方法:

public static void main(String args []) {
    String word;
    do {
        word = generateWord();
    } while(!isAllowed(word)); //generate new words until you've found an allowed one

    // print the generated and allowed word
    System.out.print(" - ");
    // generate 3 digits and print them
    System.out.println();
}

此方法的目的是生成您的车牌。赋值提到了禁止的单词(您的数组x)。这意味着您必须生成车牌的第一部分,直到您找到一个允许的单词。此任务在do/time循环中完成。它生成一个3个字符长的单词并测试它是否允许。如果不允许,则循环进行另一次迭代。如果“他”找到了一个允许的单词,循环将被退出,您的单词存储在变量word中。

生成3个字符长单词的方法如下所示:

/** Generated a 3 character long word and returns it. */
public static String generateWord() {
    String result = "";
    // generate the word by adding 3 random chars to string "result".
    // You can append a char by doing: result = result + randomChar;
    return result;
}

您已经知道如何生成这样一个单词,但您需要返回该单词,而不是打印它,这样才能在main方法中使用它。正确填写此方法对您来说应该不难。注释包含将生成的字符添加到现有字符串的“方法”。

此方法:

/**
 * Tests if the given "word" is allowed or not.
 * If the word is in the array "x", then it is not allowed.
 * @return true if the word is allowed
 */
public static boolean isAllowed(String word) {
    /* Create a loop for array "x" that takes every entry of that array
     * and tests if it is the same as "word". If this is the case, then
     * return "false". If the word is not in the array, then this word
     * is allowed. Return "true" in this case.
     */
}

应该检查参数word是否是包含禁止单词的数组x的一部分。此方法需要一个循环来检查数组x的每个条目。您可以将用于loor:

for (int i = 0; i < x.length; i++) { }

或每个循环的一个循环:

for (String entry : x) { }

您可以找到有关这两种循环类型以及如何将其用于数组的大量信息。

如果您有一个x的条目,那么您需要测试这个词是否与给定的word字符串相同。类String对此任务有一个完美的方法。阅读JavaDoc,您会发现它:类String的JavaDoc。

我希望这条指导方针能帮助你完成任务,也希望你能从中学到一些东西。祝你好运:)。

锺霍英
2023-03-14

好的,这一次你可能会从一个好的代码示例中学到更多。请仔细研究,然后自己重写,否则老师不会相信你,你也不会学到任何东西。

public class LicensePlate {
    static String[] INVALID_PLATE_LETTERS = { "FOR", "AXE", "JAM", "JAB", "ZIP", "ARE", "YOU",
            "JUG", "JAW", "JOY" };

    static String generateLetters(int amount) {
        String letters = "";
        int n = 'Z' - 'A' + 1;
        for (int i = 0; i < amount; i++) {
            char c = (char) ('A' + Math.random() * n);
            letters += c;
        }
        return letters;
    }

    static String generateDigits(int amount) {
        String digits = "";
        int n = '9' - '0' + 1;
        for (int i = 0; i < amount; i++) {
            char c = (char) ('0' + Math.random() * n);
            digits += c;
        }
        return digits;
    }

    static String generateLicensePlate() {
        String licensePlate;
        String letters;
        do {
            letters = generateLetters(3);
        } while (illegalWord(letters));

        String digits = generateDigits(3);

        licensePlate = letters + "-" + digits;
        return licensePlate;
    }

    private static boolean illegalWord(String letters) {
        for (int i = 0; i < INVALID_PLATE_LETTERS.length; i++) {
            if (letters.equals(INVALID_PLATE_LETTERS[i])) {
                return true;
            }
        }
        return false;
    }

    public static void main(String args[]) {

        System.out.println(generateLicensePlate());
    }
}
 类似资料:
  • random 生成随机数包 文档:https://www.npmjs.com/package/random 安装:npm install --save random 封装代码: app / extend / context.js // 导入 jwt const jwt = require('jsonwebtoken') // 导入随机数包 const random = require('rando

  • 问题 你需要生成在一定范围内的随机数。 解决方案 使用 JavaScript 的 Math.random() 来获得浮点数,满足 0<=X<1.0 。使用乘法和 Math.floor 得到在一定范围内的数字。 probability = Math.random() 0.0 <= probability < 1.0 # => true # 注意百分位数不会达到 100。从 0 到 100 的范围实

  • 在 Java 中要生成一个指定范围之内的随机数字有两种方法:一种是调用 Math 类的 random() 方法,一种是使用 Random 类。 Random 类提供了丰富的随机数生成方法,可以产生 boolean、int、long、float、byte 数组以及 double 类型的随机数,这是它与 random() 方法最大的不同之处。random() 方法只能产生 double 类型的 0~1

  • 我的任务: 生成1到20之间的随机数,小数点后1位。 然而,我的问题就像mt_rand一样简单。我希望大多数生成的数字较低,大约0.5-4.5,偶尔的数字在4.5-10之间,很少说每12-20小时一次在10-20之间。 我一直在使用以下内容,但不知道从哪里开始。我是一个很基本的自学程序员。 也许如果我简单地解释一下为什么我想要这个,它可能会有帮助… 我拥有一个在线游戏,想要添加3个“银行”与每个银

  • 我需要在我的脚本中生成大约5000个随机数,但是CPU速度太快了,我看到了随机数的趋势。 例如,在第一次迭代的100次中,我用rand(0100)得到了80个介于70和99之间的值;,这真的很不方便。 有没有办法解决这样的问题,或者说,在2012年,随机性已经无法实现了? 我相信有可能从一个执行随机次数的函数中生成随机数。。。但我想不出一个。