<tfoot id='y4t7H'></tfoot>

        <legend id='y4t7H'><style id='y4t7H'><dir id='y4t7H'><q id='y4t7H'></q></dir></style></legend>
          <bdo id='y4t7H'></bdo><ul id='y4t7H'></ul>
      1. <small id='y4t7H'></small><noframes id='y4t7H'>

      2. <i id='y4t7H'><tr id='y4t7H'><dt id='y4t7H'><q id='y4t7H'><span id='y4t7H'><b id='y4t7H'><form id='y4t7H'><ins id='y4t7H'></ins><ul id='y4t7H'></ul><sub id='y4t7H'></sub></form><legend id='y4t7H'></legend><bdo id='y4t7H'><pre id='y4t7H'><center id='y4t7H'></center></pre></bdo></b><th id='y4t7H'></th></span></q></dt></tr></i><div id='y4t7H'><tfoot id='y4t7H'></tfoot><dl id='y4t7H'><fieldset id='y4t7H'></fieldset></dl></div>
      3. 用某种方法在java中的字符串中选择一个随机字符

        时间:2023-07-26

            <bdo id='MIBcE'></bdo><ul id='MIBcE'></ul>
            <legend id='MIBcE'><style id='MIBcE'><dir id='MIBcE'><q id='MIBcE'></q></dir></style></legend>

                <tbody id='MIBcE'></tbody>
              <i id='MIBcE'><tr id='MIBcE'><dt id='MIBcE'><q id='MIBcE'><span id='MIBcE'><b id='MIBcE'><form id='MIBcE'><ins id='MIBcE'></ins><ul id='MIBcE'></ul><sub id='MIBcE'></sub></form><legend id='MIBcE'></legend><bdo id='MIBcE'><pre id='MIBcE'><center id='MIBcE'></center></pre></bdo></b><th id='MIBcE'></th></span></q></dt></tr></i><div id='MIBcE'><tfoot id='MIBcE'></tfoot><dl id='MIBcE'><fieldset id='MIBcE'></fieldset></dl></div>

              <small id='MIBcE'></small><noframes id='MIBcE'>

              <tfoot id='MIBcE'></tfoot>
                • 本文介绍了用某种方法在java中的字符串中选择一个随机字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我必须使用此方法从字符串中选择并返回 1 个随机字符(与 main 方法分开):

                  I have to select and return 1 random character out of a string using this method (separate from main method):

                  public static char selectAChar(String s)

                  public static char selectAChar(String s)

                  我不确定如何选择随机变量,也不确定是否应该使用 for 循环.我尝试过的所有方法都无法返回正确的变量类型.

                  I'm not sure how to select the random variable, and not sure if i should use a for loop. everything I've tried I couldn't get it to return the right variable type.

                  这是我到目前为止的编码

                  heres the coding i have so far

                  public static void main(String args[])
                  {
                      Scanner kbd = new Scanner (System.in);
                  
                      System.out.println("Enter a string: ");
                      String s = kbd.next();
                  
                      selectAChar(s);
                  }
                  
                  
                  
                  public  static  char selectAChar(String s)
                  {
                  
                  
                  }
                  

                  我用这个 for 循环尝试了一些东西for(int i = 0; i

                  i tried something using this for loop for(int i = 0; i < s.length(); i++) but i can't figure out how to choose a random character and return it.

                  推荐答案

                  public static char selectAChar(String s){
                  
                      Random random = new Random();
                      int index = random.nextInt(s.length());
                      return s.charAt(index);
                  
                  }
                  

                  这篇关于用某种方法在java中的字符串中选择一个随机字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么我可以将字符添加到字符串但不能将字符 下一篇:你如何比较java中的字符?

                  相关文章

                  最新文章

                  <legend id='5IIpV'><style id='5IIpV'><dir id='5IIpV'><q id='5IIpV'></q></dir></style></legend>

                    <i id='5IIpV'><tr id='5IIpV'><dt id='5IIpV'><q id='5IIpV'><span id='5IIpV'><b id='5IIpV'><form id='5IIpV'><ins id='5IIpV'></ins><ul id='5IIpV'></ul><sub id='5IIpV'></sub></form><legend id='5IIpV'></legend><bdo id='5IIpV'><pre id='5IIpV'><center id='5IIpV'></center></pre></bdo></b><th id='5IIpV'></th></span></q></dt></tr></i><div id='5IIpV'><tfoot id='5IIpV'></tfoot><dl id='5IIpV'><fieldset id='5IIpV'></fieldset></dl></div>
                  1. <tfoot id='5IIpV'></tfoot>
                  2. <small id='5IIpV'></small><noframes id='5IIpV'>

                      • <bdo id='5IIpV'></bdo><ul id='5IIpV'></ul>