随机打印字符串数组的内容

时间:2018-06-28 10:57:47

标签: java

随机打印数组内容的代码,获得的结果是随机打印内容,可以使用Math.Random();

public class Random {
    public static void main(String args[]) throws Exception {             
        String[] Answers = new String[4];
        Answers[0] = "OS";
        Answers[1] = "GOOD";
        Answers[2] = "CS";
        Answers[3] = "Cody";  

        for(int n=0;n<Answers.length;n++) {
            System.out.print(Answers);  
        }
    }
}      

上面的代码打印

[Ljava.lang.String; @ 1db9742 [Ljava.lang.String; @ 1db9742 [Ljava.lang.String; @ 1db9742 [Ljava.lang.String; @ 1db9742

希望它打印 CS,OS,GOOD,CODY或OS,CODY,GOOD,CS等

8 个答案:

答案 0 :(得分:2)

您正在打印阵列的地址或参考。您需要打印存储在数组中的元素,因此需要使用索引进行访问。由于要向其中添加随机功能。你做这样的事情-

import java.util.*;
//Random is itself a class in java inside java.util
class Abc {
    public static void main(String args[]) throws Exception {             
        String[] Answers = new String[4];
        Answers[0] = "OS";
        Answers[1] = "GOOD";
        Answers[2] = "CS";
        Answers[3] = "Cody";  
        Random rn = new Random();
        int freq[]=new int[Answers.length];
        for(int n=0;n<Answers.length;n++) {
            int index = rn.nextInt(Answers.length);
            if(freq[index]==0) {
                freq[index]+=1;
                System.out.println(Answers[index]);       
            } else {
                n-=1;   
            }
        }
    }
}

答案 1 :(得分:2)

简单的解决方案

public static void permuteString(String beginningString, String endingString) {

        String[] answers = new String[4];
        answers[0] = "OS";
        answers[1] = "GOOD";
        answers[2] = "CS";
        answers[3] = "Cody";

        List<String> answersList = Arrays.asList(answers);
        Collections.shuffle(answersList);
        System.out.println(Arrays.toString(answersList.toArray(new String[0])));

    }

答案 2 :(得分:1)

Arrays类具有多个实用程序方法

String[] Answers = new String[4];
    Answers[0] = "OS";
    Answers[1] = "GOOD";
    Answers[2] = "CS";
    Answers[3] = "Cody";
    System.out.println(Arrays.toString(Answers));

答案 3 :(得分:1)

不要使用 Random 作为类名,因为它是预定义的Java类名。使用Random类获取随机整数。

public class RandomStringExample{
    public static void main(String args[]) throws Exception {             
        String[] Answers = new String[4];
        Answers[0] = "OS";
        Answers[1] = "GOOD";
        Answers[2] = "CS";
        Answers[3] = "Cody";
        System.out.println(getRandomString(Answers));
    }

    public static String getRandomString(String[] answers) {
        Random random = new Random();
        int n = random.nextInt(answers.length-1);
        return answers[n];
    }
}  

答案 4 :(得分:1)

使用随机,您可以按照以下步骤进行操作:

+ (void)clearCookiesForURL: (NSString *)url {
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [cookieStorage cookiesForURL:[NSURL URLWithString:url]];
for (NSHTTPCookie *cookie in cookies) {
    NSLog(@"Deleting cookie for domain: %@", [cookie domain]);
    [cookieStorage deleteCookie:cookie];
}

答案 5 :(得分:1)

尝试一下

public static void main(String args[]) throws Exception {
    String[] answers = new String[4];
    int len = answers.length;
    answers[0] = "OS";
    answers[1] = "GOOD";
    answers[2] = "CS";
    answers[3] = "Cody";
    int random = getRandomNum(len);
    System.out.println("random: " + random);
    System.out.println(answers[random]);
}

private static int getRandomNum(int max) {
    int rand = (int) (Math.random() * 100);
    if (rand < max)
        return rand;
    return getRandomNum(max);
}
  

输出

     

运行1

random: 3
Cody
  

运行2

random: 1
GOOD
  

更新   如果必须一次运行随机打印所有元素,那么这将有所帮助

public static void main(String args[]) throws Exception {
    String[] answers = new String[4];
    int len = answers.length;
    answers[0] = "OS";
    answers[1] = "GOOD";
    answers[2] = "CS";
    answers[3] = "Cody";
    for (int i = 0; i < len; i++) {
        int random = getRandomNum(len);
        System.out.println("random: " + random);
        System.out.println(answers[random]);
    }
}

private static int getRandomNum(int max) {
    int rand = (int) (Math.random() * 100);
    if (rand < max)
        return rand;
    return getRandomNum(max);
}
  

输出

random: 1
GOOD
random: 3
Cody
random: 0
OS
random: 2
CS
  

更新如果您不希望在输出中出现任何重复的值,那么请转到此简单而简短的方法

public static void main(String args[]) throws Exception {
    String[] answers = new String[4];
    answers[0] = "OS";
    answers[1] = "GOOD";
    answers[2] = "CS";
    answers[3] = "Cody";

    List<String> list = Arrays.asList(answers);
    System.out.println("Before: " + list.toString());
    Collections.shuffle(list);
    System.out.println("After: " + list.toString());
}
  

输出

Before: [OS, GOOD, CS, Cody]
After: [Cody, OS, GOOD, CS]

希望这会有所帮助:)

答案 6 :(得分:0)

您应该为数组索引提供名称。

像这样:

System.out.print(Answers[n]);

答案 7 :(得分:0)

首先,您需要更改打印声明

String[] Answers = new String[4];
Answers[0] = "OS";
Answers[1] = "GOOD";
Answers[2] = "CS";
Answers[3] = "Cody";  

for(int n=0;n<Answers.length;n++) {
    System.out.print(Answers[n].toString() + " " );              
}

然后,您将要使用随机数生成器来选择1到4之间的随机数以从您的Answers数组中打印