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

交作业啦,ASICII转字符

package com.imooc_collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class TestRandom {
    List<String> StringList = new ArrayList<String>();
    Random random = new Random(); 
    
    public void TestAdd() {        
        for (int i = 0; i < 10;) {            
            int length = random.nextInt(10);            
            StringBuilder str = new StringBuilder("");            
            for (int j = 0; j < length;) {                
                int temp = random.nextInt(123);                
                if ((temp >= 48) && (temp <= 57)) {                    
                    char ch = (char) temp;                    
                    str.append(ch);                    
                    j++;                
                } else if ((temp >= 65) && (temp <= 90)) {                    
                     char ch = (char) temp;                    
                     str.append(ch);                    
                     j++;                
                 } else if (temp >= 97) {                    
                     char ch = (char) temp;                    
                     str.append(ch);                    
                     j++;                
                 } else {                    
                 continue;                
                 }            
             }                       
             if (StringList.contains(str.toString())) {                
                 continue;            
             } else {               
                 StringList.add(str.toString());                
                 i++;            
             }        
         }   
     }   
     public void TestForEach() {       
         for (String string : StringList) {            
             System.out.println(string);        
         }    
     }    
     
     public void TestSort() {       
         System.out.println("--------排序前--------");        
         TestForEach();        
         System.out.println("--------排序后--------");        
         Collections.sort(StringList);        
         TestForEach();    }    
         
     public static void main(String[] args) {        
         // TODO Auto-generated method stub        
         TestRandom tr = new TestRandom();        
         tr.TestAdd();        
         tr.TestSort();    
     }
 }


正在回答

举报

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

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

进入课程

交作业啦,ASICII转字符

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