Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
processing/java-如何检查数组中的值是否不变_Java_Arrays_Processing - Fatal编程技术网

processing/java-如何检查数组中的值是否不变

processing/java-如何检查数组中的值是否不变,java,arrays,processing,Java,Arrays,Processing,所以我制作了一个刽子手游戏,生成了一个随机单词theWord,然后我用 char theWordChars[] =t heWord.toCharArray(); 我使用for循环检查按下的键是否等于单词中的任何字符,并创建了一个名为keyIsFound[]的布尔数组: void keyPressed(){ if(keyCode != 0 || keyCode != UP || keyCode != DOWN || keyCode != LEFT || keyCode != RIGHT)

所以我制作了一个刽子手游戏,生成了一个随机单词theWord,然后我用

char theWordChars[] =t heWord.toCharArray();
我使用for循环检查按下的键是否等于单词中的任何字符,并创建了一个名为keyIsFound[]的布尔数组:

void keyPressed(){
    if(keyCode != 0 || keyCode != UP || keyCode != DOWN || keyCode != LEFT || keyCode != RIGHT){
        lastKey = char(keyCode);
}
    for (int z = 0; z< theWordChars.length; z++) {
        if(lastKey == theWordChars[z]){
keyIsFound[z] = true;
        }
    } 
}
void键按下(){
如果(键码!=0 | |键码!=向上| |键码!=向下| |键码!=左| |键码!=右){
lastKey=char(keyCode);
}
for(intz=0;z

现在我要检查的是,当按下一个键,但数组键中的值没有改变,即按下一个假字符,然后我可以增加计数器来显示身体部位。我该怎么做?开放思想,彻底改变它。

你通常会为此使用
标志,通常是
布尔值:

    // Did we find the key in the word?
    boolean found = false;
    // Look at all of the characters.
    for (int z = 0; z < theWordChars.length; z++) {
        // Did they press this one?
        if (lastKey == theWordChars[z]) {
            // YES! Mark it as found.
            keyIsFound[z] = true;
            // Remember we found one so we don't add a body part.
            found = true;
        }
    }
    if ( !found ) {
        // Not found the key they pressed - add a body part.
    }
//我们在单词中找到键了吗?
布尔值=false;
//看看所有的角色。
for(intz=0;z
您可以检查字符的大小是否大于等于0。 如果您将字符转换为字符串,则可以检查您的单词是否符合此键

我还注意到您使用了
keyCode
,但您可以直接使用。 此外,您还可以使用Character类确定是否按下了

没有任何奇特的UI,下面是一个使用上述概念的示例:

String keyword = "stackoverflow";
int lives = 6;

void setup(){
}
void draw(){
}

void keyPressed(){
  //check if the key pressed is a letter
  if(Character.isLetter(key)){
    //check if the word contains the letter
    if(keyword.contains(""+key)){
      println(key + " is in " + keyword);
      //remove the found letter from the word
      keyword = keyword.replaceAll(""+key,"");
      println("letters left: " + keyword);
      //if all letters have been found, we have a winner
      if(keyword.isEmpty()) {
        println("you win!");
        exit();
      }
    }else{
      lives--;
      if(lives == 0){
        println("game over!");
        exit();
      }
    }
  }
}
更新 为了好玩,这里有一个注释版本,可以在屏幕上绘制一些东西 以及使用和

//要解决的单词
String关键字=“stackoverflow”;
int lettersLeft=关键字.length();
//已解单词以从中删除猜测的字母
解串;
int生命;
int MaxLifes;
//到目前为止猜到了什么
串解;
//查找所有已按下的字母
HashMap usedLetters=新HashMap();
//游戏状态
字符串状态;
布尔配子;
//棒形图
假象刽子手//空组
PShape[]刽子手成员//每一部分
无效设置(){
尺寸(100100,P2D);
//初始化组
hangman=createShape(组);
hangman.addChild(createShape(LINE,20100,20,30));
hangman.addChild(createShape(LINE,20,30,50,30));
hangman.addChild(createShape(LINE,50,30,50,35));
hangmanMembers=新PShape[]{createShape(椭圆,50,35,10,10),//头部
createShape(线,55,45,55,50),//颈部
createShape(线,55,50,55,70),//主体
createShape(直线,55,50,40,45),//左臂
createShape(直线,55,50,80,45),//右臂
createShape(直线,55,70,40,90),//左腿
createShape(直线,55,70,60,90)//右腿
};
//有些人画一个没有脖子的刽子手,其他人用更多的零件,使生命的数量依赖于此
maxlifes=hangmanMembers.length;
lives=maxLives;//设置剩余的生命数
//添加破折号
已解决=”“+关键字;
解决方案=”;
地位=”;
对于(int i=0;i=0){
println(键+”在“+关键字”中);
//从单词中删除找到的字母
已解决=已解决。replaceAll(“+key,”);
println(“左字母:+已解决);
//更新要显示的文本
对于(int i=0;i如果(感谢您的回复,并给出了很好的评论!这段代码对我来说很有意义,也很有效,但什么是标志?实际上,这段代码会给我带来问题。如果我按下单词中的键,找到的布尔值将变为true,但决不会返回false。因此,当我以后按下单词中没有的键时,它不会增加计数器。我尝试了用else{found=false}解决此问题但是当我这样做的时候,find变为false,因为数组中有一些值仍然是false。嗯,这样做很有趣!我已经使用了其他commenters方法,保持了所有内容不变,但这很有趣!不用担心,另一个答案是直截了当的。这只是涵盖了一种不同的方法,并查看了其他过程你认为你能看看我对另一个答案的评论并帮我解决吗?没关系!你的答案
//word to solve
String keyword = "stackoverflow";
int lettersLeft = keyword.length();
//solved word to remove guessed letters from
String solved;

int lives;
int maxLives;

//what has been guessed so far
String solution;
//a lookup of all the letters already pressed
HashMap<Character,Integer> usedLetters = new HashMap<Character,Integer>();

//game status
String status;
boolean gameOn;

//stick figure drawing
PShape hangman;//the empty group
PShape[] hangmanMembers;//each part


void setup(){
  size(100,100,P2D);
  //intialize the group
  hangman = createShape(GROUP);
  hangman.addChild(createShape(LINE,20,100,20,30));
  hangman.addChild(createShape(LINE,20,30,50,30));
  hangman.addChild(createShape(LINE,50,30,50,35));
  hangmanMembers = new PShape[]{createShape(ELLIPSE,50,35,10,10),//head
                               createShape(LINE    ,55,45,55,50),//neck
                               createShape(LINE    ,55,50,55,70),//body
                               createShape(LINE    ,55,50,40,45),//left arm
                               createShape(LINE    ,55,50,80,45),//right arm
                               createShape(LINE    ,55,70,40,90),//left leg
                               createShape(LINE    ,55,70,60,90)//right leg
                              };
  //some people draw a hangman with no neck, other use more parts, make the number of lives dependant on that
  maxLives = hangmanMembers.length;
  lives = maxLives;//set the number of lives left
  //add the dashes
  solved = ""+keyword;
  solution = "";
  status = "";
  for(int i = 0 ; i < lettersLeft; i++) solution += "_";
  usedLetters.clear();
  gameOn = true;
}
//render
void draw(){
  background(127);
  text(solution,10,15);
  text(status,10,25);
  shape(hangman);
}

void keyPressed(){
  //if the game was not won or lost yet
  if(gameOn){
    //check if the key pressed is a letter
    if(Character.isLetter(key)){
      //check if the letter hasn't been used before
      if(!usedLetters.containsKey(key)){
        //if so, add to the list of used letters
        usedLetters.put(key,keyCode);

        //check if the word contains the letter
        int index = keyword.indexOf(key);
        if(index >= 0){
          println(key + " is in " + keyword);

          //remove the found letter from the word
          solved = solved.replaceAll(""+key,""); 
          println("letters left: " + solved);
          //update text for display
          for(int i = 0 ; i < solution.length(); i++){
            if(keyword.charAt(i) == key){
              solution = solution.substring(0,i)+key+solution.substring(i+1);
            }
          }
          //if all letters have been found, we have a winner
          if(solved.isEmpty()) {
            status = "you win!";
            gameOn = false;
          }
        }else{//wrong letter, lose a part
          int livesDiff = maxLives-lives;//work out the number of lives lost (maximum value - current)
          if(livesDiff < maxLives) {//if there still is a shape to display
            hangman.addChild(hangmanMembers[livesDiff]);
          }
          println(livesDiff);
          lives--;
          if(lives <= 0){
            status = "game over!";
            gameOn = false;
          }
        }
      }else{
        println("you've used " + key + " before");
      }
    }
  }else{
    //reset
    setup();
  }
}