随机字符串数组,并显示所有值 [英] Randomize String Array and display all of the values

查看:116
本文介绍了随机字符串数组,并显示所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个纸牌游戏,我已经在shufflin时间到了。

我已经重新洗牌几张卡(即从用户选择之前,所以他们并不总是相同金额),然后由其中一人显示给用户的。

由于我还在开发游戏的逻辑,我改变一个按钮上的文字显示卡的名字。

不过,我被卡住,当我试图让卡的名称和它们设置为按钮的文本。

会发生什么是我刚开了一个空白按钮或只是与共济会或乡下人的字符串。 INFACT如果我检查日志我看到所有其他卡(字符)得到显示为空。

这是我如何努力要实现的目标(是的,我是新手):

这是头:

  INT邪恶的;
INT后卫;
INT泥瓦匠;
INT介质;
INT mythomaniac;
INT猫头鹰;
INT村民;
INT werehamster;
INT所有;
INT I;
INT吨;
的String []字符=新的String [24];
按钮randomButton;

我的方法的addAll卡(字符):

 公共无效的addAll(){
对于(i = 0; I<所有;我++){
    加(邪恶的恶魔);
    加(后卫,锦衣卫);
    加(中,中);
    加(mythomaniacMythomaniac);
    加(猫头鹰,猫头鹰);
    加(werehamsterWerehamster);
    加(村民,乡下人);
    加(泥瓦匠,共济会);
   }}

我的方法来添加和管理各类卡(字符):

 公众诠释添加(INT字符,字符串名称){
    如果(字符= 0&安培;!&放大器;名称==乡下人){
        对于(t = 0; T<性格;吨++){
            我+ = T;
            字符由[i] =名称;}
    }
    否则如果(字符== 2和;&放大器;名称==泥瓦匠){
        字符由[i] =名称;
        我++;
        字符由[i] =名称;
        Toast.makeText(randomSelection.this,工程,Toast.LENGTH_SHORT).show();
    }否则如果(字符!= 0){
        字符由[i] =名称;
    }
    返回我;
}

要随机:

 公共无效随机化(){
    Collections.shuffle(Arrays.asList(字));
    对于(INT S = 1; S< characters.length; S ++)
    {
        的System.out.println(字符[S]);
    }}

以显示不同的卡(字符)每次用户点击该按钮时,方法:

 公共无效秀(查看视图){
    对于(INT S = 1; S< characters.length; S ++)
    {
        randomButton.setText(字符[S]);
    }
}

编辑:

我注意到在无感 循环我已经做了,顺便说一句您应该知道虽然大部分人物是他们的那种(邪恶的,门卫等。)有2个泥瓦匠和1名5至12个村民 ,所以我们需要检索这些 INT 取值并添加尽可能多字符串 S到阵列尽可能多的我们从这些告诉 INT 秒。

例如:如果我得到6 村民 S,我已经添加字符串村民的6次进入字符串数组

然后我设置取值 1 '事业的我已经显示前
字符串 [0] )的尽快 活动 得到启动,所以在onCreate()方法。

也许我错了,如果让我请你指正!


解决方案

  

获得一个空白按钮或只是与共济会或乡下人字符串


这是因为你只用列表的最后一个元素设置按钮的文本。这要么是泥瓦匠(没有看到它怎么会乡下人 )。

 为(int类型= 1; S< characters.length; S ++)
{
    randomButton.setText(字符[S]);
}


  

如果我检查日志我看到所有其他卡(字符)得到显示为空


您只设置你的阵列的位置0。例如,你不初始化的位置,所以这些 INT 值默认为0。

  INT邪恶的;
INT后卫;
INT所有;

然后

 为(i = 0; I<所有;我++){
    加(邪恶的恶魔);
    加(后卫,锦衣卫);

真的,不应该被输入的循环,因为所有等于0。

此外

集合的索引从零开始,所以这不打印元素0您需要设置 int类型= 0;

 为(int类型= 1; S< characters.length; S ++)


这是我不明白什么添加(INT字符,字符串名称)方法返回,但如果你解释,我会更新这个答案。

我相信这code满足大部分你想达到什么

  //当字符存储
私人的ArrayList<串GT;字符;公共无效initDeck(){
    如果(字符== NULL)
        字符=新的ArrayList<串GT;();
    //提取的数字,如果你确实需要他们,否则,他们只是为常数
    addCharacter(恶魔,1个字符);
    addCharacter(锦衣卫,1个字符);
    addCharacter(中等,1个字符);
    addCharacter(Mythomaniac,1个字符);
    addCharacter(猫头鹰,1个字符);
    addCharacter(Werehamster,1个字符);
    addCharacter(乡下人,5个字符);
    addCharacter(泥瓦匠,1个字符);
}公共无效addCharacter(字符串名称,诠释量的ArrayList<串GT;卡){
    如果(度< 0){
        抛出新抛出:IllegalArgumentException(必须添加字符的非负数+姓名);
    }    //不要使用'=='弦乐
    如果(name.equals(乡下人)){
        如果(量!= 5 ||量!= 12){
            抛出新抛出:IllegalArgumentException(只能有5个或12+姓名);
        }
    }    的for(int i = 0; I<金额;我++){
        cards.add(名);
    }
}公众诠释searchCharacters(串字符,ArrayList的<串GT;卡){
    返回cards.indexOf(字符);
}公共地图<字符串,整数> getAllCharacterPositions(){
    地图<字符串,整数> allPositions =新的LinkedHashMap<字符串,整数>();
    的for(int i = 0; I< characters.size();我++){
        allPositions.put(characters.get(ⅰ),ⅰ);
    }
    返回allPositions;
}无效的run(){
    //初始化字符
    initDeck();    //他们洗牌
    Collections.shuffle(字);    //全部打印出来
    的for(int i = 0; I< characters.size();我++){
        System.out.printf(%d个:%S \\ n,我,characters.get(I));
    }    //查找字符的位置
    的System.out.println();
    字符串findCharacter =猫头鹰
    //选择1 - 总是线性搜索查找
    System.out.printf(%d个:%S \\ n,searchCharacters(findCharacter,字符),findCharacter);
    //选择2 - 一次性线性扫描,恒查找
    地图<字符串,整数>位置= getAllCharacterPositions();
    System.out.printf(%d个:%S \\ n,positions.get(findCharacter),findCharacter);    //获取一个随机字符
    的System.out.println();
    随机兰特=新的随机(System.currentTimeMillis的());
    INT randPos = rand.nextInt(characters.size());
    System.out.printf(%d个:%S \\ n,randPos,characters.get(randPos));    // randomButton.setText(characters.get(randPos));
}

I'm making a card game, and I've arrived at the shufflin' time.

I've to shuffle a few cards (that are chosen before from the user, so they're not always the same amount) and then display them to the user one by one.

As I'm still developing the game's logic I'm displaying cards' name by changing a button text.

But I get stuck when I try to get the cards' name and set them as the button's text.

What happens is me gettin' a blank button or just with "Masons" or "Villager" String. Infact if I check the log I see that all the others cards(characters) get displayed as "null".

This is how I tried to achieve the goal (Yes I'm a newbie):

This is the head:

int demoniac;
int guard;
int masons;
int medium;
int mythomaniac;
int owl;
int villager;
int werehamster;
int all;
int i;
int t;
String[] characters = new String[24];
Button randomButton;

My method to addAll the cards(characters):

public void addAll(){
for(i = 0; i < all; i++){
    add(demoniac, "Demoniac");
    add(guard, "Guard");
    add(medium, "Medium");
    add(mythomaniac, "Mythomaniac");
    add(owl, "Owl");
    add(werehamster, "Werehamster");
    add(villager, "Villager");
    add(masons, "Masons");
   }

}

My method to add and manage the various types of cards(characters):

public int add(int character, String name){
    if(character != 0 && name == "Villager"){
        for(t = 0; t < character; t++){
            i+=t;
            characters[i] = name;}
    }
    else if(character == 2 && name == "Masons"){
        characters[i] = name;
        i++;
        characters[i] = name;
        Toast.makeText(randomSelection.this, "works", Toast.LENGTH_SHORT).show();
    }else if(character != 0){
        characters[i] = name;
    }
    return i;
}

To randomize:

 public void randomize(){
    Collections.shuffle(Arrays.asList(characters));
    for (int s = 1; s < characters.length; s++)
    {
        System.out.println(characters[s]);
    }

}

The method to display a different card(character) each time the user clicks the button:

public void show(View view){
    for (int s = 1; s < characters.length; s++)
    {
        randomButton.setText(characters[s]);
    }
}

EDIT:

I've noticed the no sense for loop I've done, by the way you should know although most of the characters are only 1 of their kind (demoniac, guard, etc..) there are 2 Masons and from 5 to 12 Villagers, so We need to retrieve these ints and add as much Strings to the Array as much we're told from those ints.

Example: If I get 6 Villagers, I've to add the String "Villager" 6 times into the String Array.

Then I've set that s value to 1 'cause I've to display the first String ([0]) as soon as the Activity gets started, so on the OnCreate() method.

Maybe I'm wrong, if so I please you to correct me!

解决方案

Getting a blank button or just with "Masons" or "Villager" String

That is because you only set the Button's text with the last element of the list. Which is either null or "Masons" (not seeing how it could be "Villager").

for (int s = 1; s < characters.length; s++)
{
    randomButton.setText(characters[s]);
}

If I check the log I see that all the others cards(characters) get displayed as "null"

You only set position 0 of your array. For example, you don't initialize the positions, so these int values default to 0.

int demoniac;
int guard;
int all;

Then

for(i = 0; i < all; i++){
    add(demoniac, "Demoniac");
    add(guard, "Guard");

Really, that loop shouldn't be entered because all equals 0.

Additionally

Collections are zero-indexed, so this doesn't print element 0. You need to set int s = 0;.

for (int s = 1; s < characters.length; s++)


It isn't clear to me what the add(int character, String name) method is returning, but if you explain it, I will update this answer.

I believe this code fulfills most of what you are trying to achieve

// Where the characters are stored
private ArrayList<String> characters;

public void initDeck() {
    if (characters == null)
        characters = new ArrayList<String>();
    // Extract the numbers if you actually need them, otherwise, they just are constants
    addCharacter("Demoniac", 1, characters);
    addCharacter("Guard", 1, characters);
    addCharacter("Medium", 1, characters);
    addCharacter("Mythomaniac", 1, characters);
    addCharacter("Owl", 1, characters);
    addCharacter("Werehamster", 1, characters);
    addCharacter("Villager", 5, characters);
    addCharacter("Masons", 1, characters);
}

public void addCharacter(String name, int amount, ArrayList<String> cards) {
    if (amount < 0) {
        throw new IllegalArgumentException("Must add a non-negative number of characters for " + name);
    }

    // Don't use '==' for Strings
    if (name.equals("Villager")) {
        if (amount != 5 || amount != 12) {
            throw new IllegalArgumentException("There can only be 5 or 12 " + name);
        }
    }

    for (int i = 0; i < amount; i++) {
        cards.add(name);
    }
}

public int searchCharacters(String character, ArrayList<String> cards) {
    return cards.indexOf(character);
}

public Map<String, Integer> getAllCharacterPositions() {
    Map<String, Integer> allPositions = new LinkedHashMap<String, Integer>();
    for (int i = 0; i < characters.size(); i++) {
        allPositions.put(characters.get(i), i);
    }
    return allPositions;
}

void run() {
    // initialize the characters
    initDeck();

    // shuffle them
    Collections.shuffle(characters);

    // print them all out
    for (int i = 0; i < characters.size(); i++) {
        System.out.printf("%d: %s\n", i, characters.get(i));
    }

    // Find the position of a character
    System.out.println();
    String findCharacter = "Owl";
    // Option 1 -- always linear search lookup
    System.out.printf("%d: %s\n", searchCharacters(findCharacter, characters), findCharacter);
    // Option 2 -- one-time linear scan, constant lookup
    Map<String, Integer> positions = getAllCharacterPositions();
    System.out.printf("%d: %s\n", positions.get(findCharacter), findCharacter);

    // Get a random character
    System.out.println();
    Random rand = new Random(System.currentTimeMillis());
    int randPos = rand.nextInt(characters.size());
    System.out.printf("%d: %s\n", randPos, characters.get(randPos));

    // randomButton.setText(characters.get(randPos));
}

这篇关于随机字符串数组,并显示所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆