繁体   English   中英

字符串对象返回

[英]String Object Return

我想确保此代码返回一个字符串对象。

class ShoutBox extends Clone{

  public static String ShoutOutRandomMessage(){
      //holds the words to be generated.

      String[] subject= {" Jim is", " Henry is", " Carter is"};
      String[] verb= {" eating", " catching", " studying", " caughing"};
      String[] adjective= {" funny", " hard", " good", " polite"};
      String[] object= {" course", " homework", " books", " dog"};
      String[] adverb= {" quickly. ", " everywhere. ", " accordingly. ", " awfully. "};

       Random r = new Random(); //intialize a Random
       int selectedElement = r.nextInt(subject.length);
     {


         //This Method will grab a word from the above list.
            String randomSentence = subject[selectedElement]
                    + verb[selectedElement]
                    + adjective[selectedElement]
                    + object[selectedElement]
                    + adverb[selectedElement];
            System.out.println("Here is your Random Generated Sentence"+"\n");
            System.out.println(randomSentence + "\n\n");
            //This method will print the random sentence.

            return String.format(randomSentence);
     } 
           //Is this code returning a string object.

     }

没有理由返回String.format(randomSentence) 只需返回randomSentence String.format(randomSentence)确实返回一个字符串,但它将等于randomSentence ,因为您没有向format方法传递任何其他参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM