更改子手的字母 [英] Changing letters for hangman

查看:55
本文介绍了更改子手的字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我到目前为止的内容:

Hi

This is what I have so far:

public void checkWord(String checkLetter)
 {
     int pos = 0, tries = 0;
     while(guesNme.indexOf('_') != -1)
     {
         pos = ranWrd.indexOf(checkLetter);

         if(pos == -1)
         {
             System.out.println(pos);

             help objHelp = new help();
             objHelp.repaint();
             tries += 1;
         }
         else
         {
             guesNme = ranWrd.substring(0, pos) + checkLetter guesNme.substring(pos + 1, guesNme.length());
             lblWrd.setText(guesNme);
         }



我正在执行绞刑,我需要检查单词中的正确字母并禁用单击的字母,并用正确的字母替换破折号,然后将图像重新绘制为新图像.

谢谢.



I am doing hang man and I need to check the correct letters in the word and disable clicked letter and substitute the dash with correct letter and repaint the image to a new image.

Thank you.

推荐答案

我在那里说过一个非常漂亮的无限循环.

您是否仔细查看了自己的逻辑或完全了解了代码?

但老实说,您的变量名并不能说明很多.我不知道guesNme是什么.

您的逻辑应该是这样的:

You''ve got a pretty nice infinite loop there I''d say.

Have you looked closely at your logic or stepped through the code at all?

But honestly, your variable names don''t tell a whole lot. I have no idea what guesNme is.

Here''s the way your logic should be:

1. Get the letter that was pressed.
2. See if the letter is in the word.
3. If it is, show the letter.
4. If not, add something to the person.
5. Disable the letter from being selected.



但是严重的是,您的代码是一团糟.

在:



But seriously, your code is a mess.

In:

guesNme = ranWrd.substring(0, pos) + checkLetter guesNme.substring(pos + 1, guesNme.length());


有错字吗?怎么编译?在checkLetter和guesNme.substring之间查看.

而且每次去检查字母时,都会重设尝试次数.

我假设ranWrd是完整的单词,而guesNme是正在显示的内容.正确吗?

因此,在上面的示例中,起始值将是:


is there a typo? how does this even compile? Look between checkLetter and guesNme.substring.

And every time you go to check a letter, you reset the number of tries.

I assume that ranWrd is the full word and guesNme is what is being displayed. Is that correct?

So, in the example above the starting values would be:

ranWrd = "flawed";
guesNme = "______";



因此,您得到第一个要检查的字母,说"w".

因此,您设置pos = 0tries = 0.

然后开始无限循环.只要guesNme中有单个"_",它将继续检查同一字母.

因此,您设置pos = 3(ranWrd.IndexOf("w")).

pos != -1开始,然后设置guesNme = "flaw",然后我假设您打算在checkLetter之后有一个+,因此您尝试添加其余的guesNme.Substring(4,6),这应该会引发错误,因为guesNme在位置上没有任何内容6.

但是,让我们说它确实在底线上加上了下划线,然后返回到while循环,循环中说如果有一个"_",则继续进行下去.

因此,总而言之,您已重置尝试,开始了无限循环,公开了所选字母之前的所有字母并引发了错误.

是的,我想说您的逻辑有些问题.



So, you get the first letter to check, say "w".

So, you set pos = 0 and tries = 0.

Then you start your infinite loop. As long as guesNme has a single "_" in it, it will keep going checking the same letter.

So, you set pos = 3 (ranWrd.IndexOf("w")).

Since pos != -1, you then set guesNme = "flaw" and then I assume you meant to have a + after checkLetter so you try to add on the rest of guesNme.Substring(4,6) which should throw an error since guesNme has nothing at position 6.

But, let''s say it did actually add on underscores, you then get back to your while loop which says if there is a single "_" then keep going.

So, all in all, you''ve reset tries, started an infinite loop, exposed any letters that were before the letter selected and threw an error.

Yeah, I''d say you have some problems with your logic.


这篇关于更改子手的字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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