猿问

在类中创建字符串,java

我正在寻找一个这样的字符串,所以它可以让我在打印句子中打印 formatArray() 。但我不确定如何制作一个字符串,并阻止它自行打印信息。这就是我想用的句子; system.out.println("your numbers are"+formatArray())



这是我的代码。


 public static void formatArray(int[] tallrekke){

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

         if(i>0 && i < tallrekke.length -1) {

             System.out.print(", ");

         }

         else if (i> tallrekke.length -2) {

             System.out.print(" og ");

         }

         System.out.print(tallrekke[i]);

    }

}


浮云间
浏览 109回答 4
4回答

幕布斯7119047

让你的方法返回一个字符串:public static String formatArray(int[] tallrekke){&nbsp; &nbsp; StringBuilder sb = new StringBuilder();&nbsp; &nbsp; for(int i=0; i<tallrekke.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; if(i>0 && i < tallrekke.length -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(", ");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (i> tallrekke.length -2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(" og ");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; sb.append(tallrekke[i]);&nbsp; &nbsp;}&nbsp; &nbsp; return sb.toString();&nbsp; &nbsp;}public static void main( String[] args) {&nbsp; &nbsp; int[] tab = {1,2,3};&nbsp; &nbsp; System.out.println( formatArray( tab ) );}输出:1, 2 og 3

慕标5832272

public static String formatArray(int[] tallrekke) {&nbsp; &nbsp; String s = "";&nbsp; &nbsp; for (int i = 0; i < tallrekke.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (i > 0 && i < tallrekke.length - 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s += ", ";&nbsp; &nbsp; &nbsp; &nbsp; } else if (i > tallrekke.length - 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s += " og ";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s += tallrekke[i];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; return s;}它将带来您所有的打印报表。然而,我更愿意使用StringBuilder而不是String. 这意味着您的代码将如下所示public static String formatArray(int[] tallrekke) {&nbsp; &nbsp; StringBuilder sb = new StringBuilder();&nbsp; &nbsp; for (int i = 0; i < tallrekke.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (i > 0 && i < tallrekke.length - 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(", ");&nbsp; &nbsp; &nbsp; &nbsp; } else if (i > tallrekke.length - 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(" og ");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(tallrekke[i]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; return sb.toString();}

江户川乱折腾

首先,您需要将方法的返回类型设置为 String 而不是 void。然后你应该使用 StringBuilder 创建一个字符串。public static String formatArray(int[] tallrekke) {&nbsp; &nbsp; StringBuilder stringBuilder = new StringBuilder();&nbsp; &nbsp; for(int tall : tallrekke){&nbsp; &nbsp; &nbsp; &nbsp; stringBuilder.append(tall);&nbsp; &nbsp; }&nbsp; &nbsp; return stringBuilder.toString();}

神不在的星期二

尽管您的预期输出尚不清楚,但这就是您所需要的:public static String formatArray(int[] tallrekke){&nbsp; &nbsp; StringBuilder stringBuilder = new StringBuilder();&nbsp; &nbsp; for(int i=0; i<tallrekke.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; if(i>0 && i < tallrekke.length -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringBuilder.append(", ");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (i> tallrekke.length -2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringBuilder.append(" og ");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; stringBuilder.append(tallrekke[i]);&nbsp; &nbsp; }&nbsp; &nbsp; return stringBuilder.toString();}您应该通过参数调用此方法:int[] tallrekke = {0,1,2};System.out.println("your numbers are"+formatArray(tallrekke));
随时随地看视频慕课网APP

相关分类

Java
我要回答