【问题标题】:Generating random License PLate生成随机牌照
【发布时间】:2015-01-05 06:17:26
【问题描述】:
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 码)和三个数字的随机车牌。当我运行程序时,static int z=Integer.parseInt(y); 出现异常。所以基本上我所做的是将String数组转换为String,然后将String转换为int,然后将int转换为char,然后我做了一个while循环(说如果字母不等于b),那么它应该可以工作。

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

【问题讨论】:

  • 你能告诉我,这个Integer.parseInt(Arrays.toString(x)) 应该返回什么吗?或者FOR, AXE, JAM, ...的整数是多少?
  • 暂时离开这个框,预期的输出是什么?你能描述一下你在每一行上做了什么吗,因为我在这里看到了一些严重的混乱。
  • @Monty Arrays.toString(x) 调用创建字符串 "[FOR, AXE, JAM, JAB, ZIP, ARE, YOU, JUG, JAW, JOY]"。这看起来不像是一个数字。您要求Integer.parseInt 将其解释为数字并将其放入z。它不知道你想要什么号码,我们也不知道。如果您不想将这组单词解释为数字,请不要调用Integer.parseInt
  • Monly,你没有编写代码示例?
  • @owlstead,谢谢,但如果没有指南,我不会得到它

标签: java


【解决方案1】:

好的,这一次只是因为此时您可能会从一个好的代码示例中学到比其他任何东西更多的东西。请自己学习,然后自己重​​写,否则你的老师不会相信你,你也不会学到任何东西。

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());
    }
}

【讨论】:

  • 除此之外,我不喜欢任何禁止"JOY"的课程。
  • 你不能让 String generateNumbers 更容易吗,像这样 for (int k = 0; k
  • 我做不到,因为我必须在全班面前展示并告诉他们它是如何工作的?我要如何向他们解释一个新概念 ARRAY
  • Owlstead,你能告诉我如何为这个制作一个盒子
  • @Monty 数字不是从 65 开始的。那将是 ASCII 中的字母 A。您已经在问题中自己使用了一个数组。如果您不希望这样,只需分别检查每个字符串。至于盒子,为这样的事情改变你的代码很有趣,但如果你必须被告知如何做,这并不意味着什么。
【解决方案2】:

让我们从你的数组开始:

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

它包含禁用词。这很好,除此之外你不需要任何其他东西。无需创建包含数组长度的变量z 或包含此数组中某种char 的变量b

现在进入主要方法:

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/while 循环中完成。它生成一个 3 个字符长的单词并测试它是否被允许。如果不是,则循环进行另一次迭代。如果 "he" 找到了允许的单词,则退出循环并将您的单词存储在变量 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 方法中使用它。正确填写此方法对您来说应该不难。该评论包含将生成的char 添加到现有String 的“方式”。

这个方法:

/**
 * 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 的每个条目。您可以使用for 地板:

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

for each 循环:

for (String entry : x) { }

您可以找到很多关于这两种循环类型以及如何将其与数组一起使用的信息。

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

我希望本指南可以帮助您完成任务,并希望您从中学到了一些东西。 祝你好运:)。

【讨论】:

【解决方案3】:

这就是我在课堂上构建它的方式,直截了当没有......混乱。

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);
    }
}

【讨论】:

    猜你喜欢
    • 2016-01-24
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多