2

我遇到的问题是从特定列表中随机生成名称。我希望我的程序只能从以下名称中进行选择:Bob、Jill、TomBrandon。我尝试学习数组,但我认为这对我来说有点太远了。到目前为止,我想我有一个大致的想法,但我不确定。

import java.util.Random;

public class NameGenerator
{

        public static void main(String[] args)
        {

           System.out.println("This is a program that generates random names from a list!");

           int Bob = 0;
           int Jill = 0;
           int Tom = 0;
           int Brandon = 0;
           Random r = new Random();

在那之后,我有点纠结于如何让发电机运转起来。

更新:好吧,我接受了您的建议并尝试学习数组。到目前为止,这就是我所拥有的。

ArrayList<String> names = new ArrayList<String>();
names.add("Bob");
names.add("Jill");
names.add("Tom");
names.add("Brandon");

char index = randomGenerator.nextChar(names.size());
String anynames = names.get(index);
System.out.println("Your random name is" + anynames + "now!");

但是现在它说 randomGenerator 无法解析,并且 void 方法无法返回值。关于我哪里出错的任何想法?

4

6 回答 6

4
import java.util.*; 

public class characters 
{
    public static void main(String[] args)
    {
       Random generate = new Random();
       String[] name = {"John", "Marcus", "Susan", "Henry"};

       System.out.println("Customer: " + name[generate.nextInt(4)]);      

    }
}

看看有多容易?我给了 4 个简单的名字,可以用单词等代替。代码中的“4”代表String中名字的个数。这很简单。对于那些想要更短的人(我所做的只是减少间距):

import java.util.*; 
public class characters{ 
public static void main(String[] args){
Random generate = new Random(); 
String[] name = {"John", "Marcus", "Susan", "Henry"};
System.out.println("Customer: " + name[generate.nextInt(4)]); }}
于 2016-02-16T19:55:21.187 回答
3

您可以打乱ArrayList并获取第一个元素,或者迭代并以不同的顺序获取所有元素。

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class NameGenerator {
    public static void main(String[] args) {
        String[] peoples = {"Bob","Jill","Tom","Brandon"};
        List<String> names = Arrays.asList(peoples);
        Collections.shuffle(names);
        for (String name : names) {
            System.out.print(name + " ");
        }
    }
}

否则,您可以每次创建一个随机数并采用不同的名称。

        int index = new Random().nextInt(names.size());
        String anynames = names.get(index);
        System.out.println("Your random name is" + anynames + "now!");
于 2012-09-23T07:08:17.440 回答
1

你可以用更少的行来表达它:

String[] names = {"Bob", "Jill", "Tom", "Brandon"};
int index = Math.random() * names.length;
String name = names[index];
System.out.println("Your random name is" + name + "now!");
于 2012-09-23T07:21:39.350 回答
1
import java.util.*;
import java.io.*;

public class Main {
  public static void main(String[] args) throws Exception {

    // To generate random information
    Random random = new Random(); // necessary for this project

    // To get the names and surnames .txt files in local directory
    Scanner male = new Scanner(
        new File("/storage/emulated/0/Download/male.txt")); // male names .txt

    Scanner female = new Scanner(
        new File("/storage/emulated/0/Download/female.txt")); // female names .txt

    Scanner surname = new Scanner(
        new File("/storage/emulated/0/Download/surnames.txt")); // surname lists .txt


    // Each name and surname will be stored here for easier use
    LinkedList<String> maleNames = new LinkedList<String>(); // male names
    LinkedList<String> femaleNames = new LinkedList<String>(); // female names
    LinkedList<String> surnameLists = new LinkedList<String>(); // surname lists

    // Loops are used to automatically store each name and surname into the list
    while (male.hasNext()) {
        maleNames.add(male.next()); // auto store each male name into the list
    }
    while (female.hasNext()) {
        femaleNames.add(female.next()); // auto store each female name into the list
    }
    while (surname.hasNext()) {
        surnameLists.add(surname.next()); // auto store each surname into the list
    }

    // Sorting Elements is optional
    surnameLists.sort(Comparator.naturalOrder()); // optional
    femaleNames.sort(Comparator.naturalOrder()); // optional
    maleNames.sort(Comparator.naturalOrder()); // optional

    // Closing these objects is necessary
    surname.close(); // necessary
    female.close(); // necessary
    male.close(); // necessary


    // For loop is used to generate multiple information
    for (int index = 0; index < 10000; index++) { // optional

        String firstName = "", lastName = "", completeName = "", gender = ""; // temporary storage

        byte age = (byte)(random.nextInt(99 - 18 + 1) + 18); // generates random age between 18 to 99 years old

        // If boolean value is true, it is male
        if (random.nextBoolean()) {
            firstName += maleNames.get(random.nextInt(maleNames.size())); // generates random name
            lastName += surnameLists.get(random.nextInt(surnameLists.size())); // generates random surname

            firstName = (firstName.substring(0, 1).toUpperCase() + firstName.substring(1)); // uppercase first letter
            lastName = (lastName.substring(0, 1).toUpperCase() + lastName.substring(1)); // uppercase first letter

            completeName = firstName + " " + lastName; // creates a complete name
            gender = "Male"; // sets the gender into male

            // Otherwise, it is female
        } else {
            firstName += femaleNames.get(random.nextInt(femaleNames.size())); // generates random name
            lastName += surnameLists.get(random.nextInt(surnameLists.size())); // generates random surname

            firstName = (firstName.substring(0, 1).toUpperCase() + firstName.substring(1)); // uppercase first letter
            lastName = (lastName.substring(0, 1).toUpperCase() + lastName.substring(1)); // uppercase first letter

            completeName = firstName + " " + lastName; // creates a complete name
            gender = "Female"; // sets the gender into female
        }

        // Finally, printing out the results
        System.out.printf("Name: %s%nAge:  %d%nSex:  %s%n%n", completeName, age, gender);
    }
  }
}
于 2021-11-20T03:09:05.130 回答
0
public static void main(String[] args) {
    String[] peoples = {"Bob", "Jill", "Tom", "Brandon"};
    List<String> names = Arrays.asList(peoples);
    int index = new Random().nextInt(names.size());
    String name = names.get(index);
    System.out.print(name+ " ");
于 2014-10-19T15:15:30.567 回答
0

一个小提示 -Random r = new Random创建了一个新的“随机”实例。由于您使用的是 Eclipse,因此您将拥有 AutoComplete。声明 Random "r" 的实例后,如果使用,r.<somemethod>您可能会发现一些有用的东西。

此外,如果您将“名称”存储在一个数组中,您可以以一种相当简单的方式访问它们。我会让你弄清楚其余的。

良好的编码:)

于 2012-09-23T03:28:28.687 回答