我创造了一个刽子手游戏。
我想问一下如何检查用户是否输入相同的字母。一切正常,但没有检查数字是否已经全部输入。
if (word.contains(input)) {
i = i - 1;
char[] charOfGameBoard = gameBoard.toCharArray();
for (int x = 0; x < word.length(); x++) {
if (word.charAt(x) != input.charAt(0)) {
continue;
}
int j = x * 2;
charOfGameBoard[j] = input.charAt(0);
}
gameBoard = String.valueOf(charOfGameBoard);
}
我不能过多地发帖。
word =“house”
charOfGameBoard = _ _ _ _ _
userinput =“h”//正确
charOfGameBoard = h _ _ _ _
userinput =“h”//此时不正确
charOfGameBoard = h _ _ _ _
答案 0 :(得分:2)
您可以将猜测存储在2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
ERROR (version 7.0.0.0-25, build 1 from 2016-11-05 15.35.36 by buildguy) : Unable to connect to repository
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
ERROR (version 7.0.0.0-25, build 1 from 2016-11-05 15.35.36 by buildguy) :
org.pentaho.di.core.exception.KettleException:
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
Error connecting to the repository!
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
No repository exists on the specified connection.
To create a repository, select the Edit' button followed by the 'Create or upgra
de' button.
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.pentaho.di.repository.kdr.delegates.KettleDatabaseRepositoryConne
ctionDelegate.connect(KettleDatabaseRepositoryConnectionDelegate.java:165)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.pentaho.di.repository.kdr.KettleDatabaseRepository.connect(Kettle
DatabaseRepository.java:193)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.pentaho.di.repository.kdr.KettleDatabaseRepository.connect(Kettle
DatabaseRepository.java:182)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.pentaho.di.ui.repo.RepositoryConnectController.connectToRepositor
y(RepositoryConnectController.java:217)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.pentaho.di.ui.repo.RepositoryConnectController.connectToRepositor
y(RepositoryConnectController.java:201)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.pentaho.di.ui.repo.RepositoryDialog$12.function(RepositoryDialog.
java:179)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.eclipse.swt.browser.WebSite.Invoke(Unknown Source)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.eclipse.swt.browser.WebSite$7.method6(Unknown Source)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.eclipse.swt.internal.ole.win32.COMObject.callback6(Unknown Source
)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.eclipse.swt.internal.win32.OS.DispatchMessage(Unknown Source)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.pentaho.di.ui.repo.RepositoryDialog.open(RepositoryDialog.java:25
4)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.pentaho.di.ui.repo.RepositoryDialog.open(RepositoryDialog.java:85
)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.pentaho.di.ui.repo.RepositoryDialog.open(RepositoryDialog.java:81
)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.pentaho.di.ui.repo.RepositoryDialog.openManager(RepositoryDialog.
java:263)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.pentaho.di.ui.repo.RepositoryConnectMenu$3$2.widgetSelected(Repos
itoryConnectMenu.java:176)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.eclipse.swt.widgets.Display.sendEvent(Unknown Source)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.pentaho.di.ui.spoon.Spoon.readAndDispatch(Spoon.java:1359)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.pentaho.di.ui.spoon.Spoon.waitForDispose(Spoon.java:7990)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.pentaho.di.ui.spoon.Spoon.start(Spoon.java:9290)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.pentaho.di.ui.spoon.Spoon.main(Spoon.java:685)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at java.lang.reflect.Method.invoke(Unknown Source)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.pentaho.commons.launcher.Launcher.main(Launcher.java:92)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
Caused by: org.pentaho.di.core.exception.KettleException:
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
No repository exists on the specified connection.
To create a repository, select the Edit' button followed by the 'Create or upgra
de' button.
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.pentaho.di.repository.kdr.delegates.KettleDatabaseRepositoryConne
ctionDelegate.verifyVersion(KettleDatabaseRepositoryConnectionDelegate.java:213)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
at org.pentaho.di.repository.kdr.delegates.KettleDatabaseRepositoryConne
ctionDelegate.connect(KettleDatabaseRepositoryConnectionDelegate.java:151)
2017/07/04 13:48:00 - class org.pentaho.di.ui.repo.RepositoryConnectController -
... 31 more
HashSet
HashSet<char> guessedLetters = new HashSet();
...
boolean success = guessedLetters.Add(input)
只能包含每个值中的一个,如果该集合已包含要添加的元素,则HashSet
方法将返回false。
答案 1 :(得分:0)
您可以在输入时将字符添加到数组列表中,并检查您之前是否看过该字母:
ArrayList<Character> usedLetters = new ArrayList<>();
public boolean UserTypedLetterBefore(char letter) {
if(usedLetters.contains(letter))
return true;
usedLetters.add(letter);
return false;
}
然后当游戏结束时,您只需致电usedLetters.clear()
清空列表并重新开始。此方法还可以通过执行usedLetters.size()
来跟踪猜测次数,因为每个猜测都是一个字母
如果您想提高效率,可以使用HashSet
代替(虽然在您的情况下不需要):
HashSet<Character> usedLetters = new HashSet<>();
public boolean UserTypedLetterBefore(char letter) {
if(usedLetters.contains(letter))
return true;
usedLetters.add(letter);
return false;
}
对于较大的数据集,这将是理想的选择,但在您的情况下,因为您只有26个可能的字母,所以您不会注意到速度差异。
第三种选择是使用原始数组,如下:
boolean[] usedLetters = new boolean[26];
public boolean UserTypedLetterBefore(char letter) {
letter = Character.toLowerCase(letter);
if(usedLetters[letter - 'a'])
return true;
usedLetters[letter - 'a'] = true;
return false;
}
每个索引都是使用该字母的标志。通过对字符letter - 'a'
执行数学运算,我们可以生成'a' = 0
,这样我们就可以将它用作我们所需数组中的索引。 Character.toLowerCase()
作为安全添加。
答案 2 :(得分:0)
您可以进行List
猜测,并在if
语句中添加其他条件,如下所示:
List<String> guesses = new ArrayList<>();
if (word.contains(input) && !guesses.contains(input)) {
guesses.add(input);
//...
//all of your other code
//...
}