为了账号安全,请及时绑定邮箱和手机立即绑定

老师,为什么,添加的随机字符中间会有点,怎么去掉

package collection;


import java.util.Random;

import java.util.Scanner;

import java.util.ArrayList;

import java.util.List;


public class randomString {



private static final Exception OutException = new Exception("超出范围");




public static void main(String[] args) {

// TODO Auto-generated method stub

try {

Scanner input = new Scanner(System.in);

System.out.println("请输入数组个数1~29的数字:");

int num = input.nextInt();

List <Integer> size = randSize(num);

List <String> arry = randString(size);

for (int i = 0 ; i < arry.size(); i++) {

System.out.println(arry.get(i));

}

}catch(Exception e) {

System.out.println(e);

}

}



//生产随机长度

public static List<Integer> randSize(int size) throws java.lang.Exception  {

//创建数值类 泛型 Integer

List <Integer> sizeOfStr = new ArrayList<Integer>();

//创建 random 对象 randSize

Random randsize = new Random();

//10以内随机值赋值给sizeOfStr数组,个数为size

int k;

if (size >= 30 || size < 1) {

throw OutException;

}

for (int i = 0 ; i < size ; i++) {

do {

k = randsize.nextInt(30);

}while(sizeOfStr.contains(k)||k==0);

sizeOfStr.add(k);

}

System.out.println(sizeOfStr);

return sizeOfStr;

}

//产生字符串

public static List <String> randString(List randSize ) {

List <String> randStr = new ArrayList <String>();

List <Character> randChr  = new ArrayList <Character>();

Random randChar = new Random();

for(int i = 0 ; i < randSize.size(); i++) {

//每一组赋值

for(int j = 0 ; j < (int)randSize.get(i); j++ ) {

randChr.add((char)(Math.abs(randChar.nextInt())%(127 -32 +1)+32));

}

randStr.add(randChr.toString());

randChr.clear();

}

randStr.add("done");

return randStr;

}


}


正在回答

1 回答

package collection;


import java.util.Random;

import java.util.Scanner;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;


public class randomString {



private static final Exception OutException = new Exception("超出范围");




public static void main(String[] args) {

// TODO Auto-generated method stub

try {

Scanner input = new Scanner(System.in);

System.out.println("请输入数组个数1~29的数字:");

int num = input.nextInt();

List <Integer> size = randSize(num);

List <String> arry = randString(size);

System.out.println("----------排序前------------");

for (String i : arry) {

System.out.println("第"+((int)arry.indexOf(i)+1)+"行:"+i);

}

System.out.println("--------排序后---------");

Collections.sort(arry);

for (String i : arry) {

System.out.println("第"+((int)arry.indexOf(i)+1)+"行:"+i);

}

}catch(Exception e) {

System.out.println(e);

}

}



//生产随机长度

public static List<Integer> randSize(int size) throws java.lang.Exception  {

//创建数值类 泛型 Integer

List <Integer> sizeOfStr = new ArrayList<Integer>();

//创建 random 对象 randSize

Random randsize = new Random();

//10以内随机值赋值给sizeOfStr数组,个数为size

int k;

if (size >= 30 || size < 1) {

throw OutException;

}

for (int i = 0 ; i < size ; i++) {

do {

k = randsize.nextInt(30);

}while(sizeOfStr.contains(k)||k==0);

sizeOfStr.add(k);

}

System.out.println(sizeOfStr);

return sizeOfStr;

}

//产生字符串

public static List <String> randString(List randSize ) {

List <String> randStr = new ArrayList <String>();

Random randChar = new Random();

for(int i = 0 ; i < randSize.size(); i++) {

//每一组赋值

StringBuilder randChr  = new StringBuilder();

for(int j = 0 ; j < (int)randSize.get(i); j++ ) {

randChr.append((char)(Math.abs(randChar.nextInt())%(127 -32 +1)+32));

}

randStr.add(randChr.toString());

}

return randStr;

}


}


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Java入门第三季
  • 参与学习       409792    人
  • 解答问题       4340    个

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

进入课程

老师,为什么,添加的随机字符中间会有点,怎么去掉

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信