如何从特定字母列表中获取随机字母?

发布于 2024-08-25 16:24:09 字数 63 浏览 7 评论 0原文

我已经开始编写一些 JAVA 代码,现在我想从给定的字母列表中随机获取一个字母,那么我应该使用什么来实现这一点。

I've started writing some JAVA code and now I want to get a random letter from list of letters given so what should I use for this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏天碎花小短裙 2024-09-01 16:24:09

我将从字面上理解您的问题:

Random r = new Random(); // Keep this stored as a field
List<Character> l = ...; // initialize this somewhere
char c = l.get(r.nextInt(l.size()));

根据几个因素(字母是否连续,您是否动态调整列表大小),您可能能够使用数组,或者可能不需要集合。请参阅 Random 类。

例子:

Random r = new Random(); // Keep this stored as a field
List<Character> l = Arrays.asList('A', 'F', 'O', 'W', 'M', 'I', 'C', 'E');
char c = l.get(r.nextInt(l.size()));   

I'll take your question literally:

Random r = new Random(); // Keep this stored as a field
List<Character> l = ...; // initialize this somewhere
char c = l.get(r.nextInt(l.size()));

Depending on several factors (are the letters contiguous, are you resizing the list dynamically), you may be able to use an array, or may not need a collection. See the Random class.

Example:

Random r = new Random(); // Keep this stored as a field
List<Character> l = Arrays.asList('A', 'F', 'O', 'W', 'M', 'I', 'C', 'E');
char c = l.get(r.nextInt(l.size()));   
甜宝宝 2024-09-01 16:24:09

此代码片段应该具有指导意义:

import java.util.Random;

public class RandomLetter {
   static Random r = new Random();  
   static char pickRandom(char... letters) {
      return letters[r.nextInt(letters.length)];
   }
   public static void main(String args[]) {
      for (int i = 0; i < 10; i++) {
         System.out.print(pickRandom('A', 'F', 'O', 'W', 'M', 'I', 'C', 'E'));
      }
   }   
}

另请参阅:


如果你想一次处理 3 个字母,那么你可以这样做:

import java.util.Random;

public class RandomLetter {
   static Random r = new Random();  
   static char pickRandom(char... letters) {
      return letters[r.nextInt(letters.length)];
   }

   public static void main(String args[]) {
      for (int i = 0; i < 10; i++) {
         System.out.println("" +
            pickRandom("ACEGIKMOQSUWY".toCharArray()) +
            pickRandom("BDFHJLNPRVXZ".toCharArray()) +
            pickRandom("ABCDEFGHJKLMOPQRSTVWXYZ".toCharArray())
         );
      }
   }   
}

This snippet should be instructive:

import java.util.Random;

public class RandomLetter {
   static Random r = new Random();  
   static char pickRandom(char... letters) {
      return letters[r.nextInt(letters.length)];
   }
   public static void main(String args[]) {
      for (int i = 0; i < 10; i++) {
         System.out.print(pickRandom('A', 'F', 'O', 'W', 'M', 'I', 'C', 'E'));
      }
   }   
}

See also:


If you want to do 3 letters at a time, then you can do something like this instead:

import java.util.Random;

public class RandomLetter {
   static Random r = new Random();  
   static char pickRandom(char... letters) {
      return letters[r.nextInt(letters.length)];
   }

   public static void main(String args[]) {
      for (int i = 0; i < 10; i++) {
         System.out.println("" +
            pickRandom("ACEGIKMOQSUWY".toCharArray()) +
            pickRandom("BDFHJLNPRVXZ".toCharArray()) +
            pickRandom("ABCDEFGHJKLMOPQRSTVWXYZ".toCharArray())
         );
      }
   }   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文