qq_24644221 2018-07-07 13:12 采纳率: 100%
浏览 1267
已采纳

我要在循环里面把最后的逗号给去掉,但是要报错,谁帮我看看

public static void main(String[] args) {
int[] indexValue = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42};
int count = (int) (Math.random() * 42);
System.out.println("取出数字个数:" + count);
int[] resultIndex = new int[count];
for (int i = 0; i < count; i++) {
int index = (int) (Math.random() * 42);
if (!containValue(resultIndex, index)) {
resultIndex[i] = index;
} else {
i--;
}
}
for (int i = 0; i < resultIndex.length; i++) {
int i1 = indexValue[resultIndex[i]];
String strResult = String.valueOf(i1);
StringBuffer buffer = new StringBuffer(strResult);

            buffer.append(",");
            if (',' == buffer.charAt(buffer.length() - 1))
                buffer = buffer.deleteCharAt(buffer.length() - 1);
            buffer.delete(buffer.lastIndexOf(","),buffer.length());
            System.out.print(buffer);
        }
    }
private static boolean containValue(int[] resultIndex, int index){
    for (int j = 0; j < resultIndex.length; j++){
        if (index == resultIndex[j]){
            return true;
        }
    }
    return false;
}
  • 写回答

5条回答 默认 最新

  • threenewbee 2018-07-08 02:58
    关注
     /* package whatever; // don't place package name! */
    
    import java.util.*;
    import java.lang.*;
    import java.io.*;
    
    /* Name of the class has to be "Main" only if the class is public. */
    class Ideone
    {
    public static void main(String[] args) {
    int[] indexValue = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42};
    int count = (int) (Math.random() * 42);
    System.out.println("取出数字个数:" + count);
    int[] resultIndex = new int[count];
    for (int i = 0; i < count; i++) {
    int index = (int) (Math.random() * 42);
    if (!containValue(resultIndex, index)) {
    resultIndex[i] = index;
    } else {
    i--;
    }
    }
    StringBuffer buffer = new StringBuffer("");
    for (int i = 0; i < resultIndex.length; i++) {
    int i1 = indexValue[resultIndex[i]];
    String strResult = String.valueOf(i1);
    
    buffer.append(strResult);
                buffer.append(",");
    
                //else
                //buffer.delete(buffer.lastIndexOf(","),buffer.length());
    
            }
                        if (',' == buffer.charAt(buffer.length() - 1))
                    buffer = buffer.deleteCharAt(buffer.length() - 1);
                    System.out.print(buffer);
        }
    private static boolean containValue(int[] resultIndex, int index){
        for (int j = 0; j < resultIndex.length; j++){
            if (index == resultIndex[j]){
                return true;
            }
        }
        return false;
    }
    }
    

    https://ideone.com/zxsIhD

    取出数字个数:25
    34,39,5,9,7,35,11,19,32,41,27,6,33,17,42,25,37,3,26,22,21,13,18,8,24

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度