简体   繁体   English

不能更改字符

[英]Can't change Char

When I'm trying to change char in my word when I click on for example button A. I want a set char on A and change in my invisible word from (--) set A there where is his position. 当我尝试例如单击按钮A来更改单词中的char时。我想要在A上设置一个char并从(-)设置A中更改我的不可见单词,那么他的位置在哪里。 I'm generating code in NetBeans. 我正在NetBeans中生成代码。

if(hadanka.contains(pismeno))
{
    for(int i=0;i!=hadanka.length();i++)
    if(pismeno.equals(Character.toString(hadanka.charAt(i))))
    {
        prepsani[i]=hadanka.charAt(i);

    }

    System.out.println(prepsani);;
}

Everything is ok. 一切都好。 Hadanka is a guessing word and pismeno is the letter that I chose. Hadanka是一个猜测的单词,而pismeno是我选择的字母。 When I use it in my application for Android errors occur. 在我的Android应用程序中使用它时,会发生错误。

Here is my Android code: 这是我的Android代码:

char zvolenePismeno;
public void OnStart (View v)

{
    if(odpoved.contains(zvolenePismeno))
    {
        for(int i=0;i!=odpoved.length();i++)
        if(zvolenePismeno.equals(Character.toString(odpoved.charAt(i))))
        {
            prepsani[i]=odpoved.charAt(i);

        }
        labOdpoved.setText(String.valueOf(prepsani));
    }

}

public void OnA(View v)
{
    zvolenePismeno = 'A';
    kliknuti(btn_A);
}

The errors I get is the following: 我得到的错误如下:

the method contains(charsequence) in the type string is not applicable for the arguments (char) 类型字符串中的contains(charsequence)方法不适用于参数(char)

at the line if(odpoved.contains(zvolenePismeno)) and if(odpoved.contains(zvolenePismeno))

cannot invoke equals (String) on the primitive type char 无法在原始类型char上调用equals(String)

at the line if(zvolenePismeno.equals(Character.toString(odpoved.charAt(i)))) . if(zvolenePismeno.equals(Character.toString(odpoved.charAt(i))))

UPDATE 1 更新1

it is doesn't work :/ Im change my code on 这是行不通的:/我更改了我的代码

 if(odpoved!=null)
         {
               for(int i=0;i!=odpoved.length();i++)
                    if(zvolenePismeno==odpoved.charAt(i))
                      {
                           prepsani[i]=odpoved.charAt(i);

                      } 
               labOdpoved.setText(String.valueOf(prepsani));
         }

Here is my whole code (with error )¨ 这是我的完整代码(有错误)¨

public class MainActivity extends Activity implements OnClickListener {
    int lvl1;
    String[] Level1 = {"APPLE", "SAMSUNG", "NOKIA", "HTC"};
    String odpoved, pokus;
    char zvolenePismeno;

    public void OnStart(View v) {
        btnStart = (Button) findViewById(R.id.btnStart);
        labOdpoved = (TextView) findViewById(R.id.labOdpoved);
        Random rand = new Random();
        lvl1 = Math.abs(rand.nextInt() % 3);
        odpoved = Level1[lvl1];

        char[] prepsani = new char[odpoved.length()];
        for (int i = 0; i != odpoved.length(); i++) {
            prepsani[i] = '-';
        }
        labOdpoved.setText(String.valueOf(prepsani));
        if (odpoved.contains(zvolenePismeno)) {
            for (int i = 0; i != odpoved.length(); i++)
                if (zvolenePismeno.equals(Character.toString(odpoved.charAt(i)))) {
                    prepsani[i] = odpoved.charAt(i);
                }
            labOdpoved.setText(String.valueOf(prepsani));
        }
    }

    public void kliknuti(Button btn) {
        btn.setClickable(false);
        btn.setVisibility(View.INVISIBLE);
    }

    public void OnA(View v) {
        zvolenePismeno = 'A';
        btn_A = (Button) findViewById(R.id.btn_A);
        kliknuti(btn_A);
    }
}

char != Char 字符!=字符
Class Char has method equals but not primitive type char . Char类具有等于的方法,但没有原始类型char

if (odpoved!=null) 
 for(int i=0;i!=odpoved.length();i++) 
   if (zvolenePismeno==odpoved.charAt(i))

Should work 应该管用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM