好吧,我在接收值时遇到问题...我已经研究了几天,但没有任何内容符合我需要的主题。这是一个策划者游戏。我正在为高中编程课的期末项目创建这个项目。 Eclipse 编译器告诉我无法解析。我该如何解决这个问题并实现我的目标。这不是在小程序中运行。
package masterMind;
import java.util.Scanner;
public class MasterMind {
public static void main(String[] args) {
System.out.println("This is MasterMind, a logic game");
System.out.println("To win you must guess correctly where each number is");
System.out.println("You will be told if you get one correct");
System.out.println("You will only get 10 tries, then you lose");
System.out.println("Lets begin");
//Change this value to change the game
int m1=2;
int m2 =3;
int m3=2;
int m4=1;
//Create Board
System.out.println("__ __ __ __");
Scanner UserGuess = new Scanner(System.in);
int num = in.nextInt();
我的编码知识非常有限,所以请保持简单并解释
最佳答案
System.in 是系统的InputStream(如Windows 的cmd),为了从中读取数据,您可以使用Scanner 或InputStreamReader,就像您尝试做的那样......所以而不是
in.nextInt();
你需要
userGuess.nextInt();
顺便说一句,学习正确使用大写字母,因为它稍后会对您有所帮助,例如 userGuess 不应该以大写字母开头,因为它是一个实例而不是一个类。
无论如何,对于你的游戏,你必须猜测 10 次,这意味着你必须重复相同的猜测 Action 10 次,或者直到用户猜出所有数字,那就是你应该使用 while 循环的时候,就像这样......
boolean guessedAll = false;
int guessedCount=0;
int tryCounter=0;
while(tryCounter<9 || !guessedAll){
//read the number from the user and test it ...
//if number equals one of the numbers above then guessedCount++ ...
//if guessedCount==4 then guessedAll=true
tryCounter++;
}
现在我几乎给了你完成这项作业所需的所有算法,但在你尝试之前我不会为你解决它,否则你什么也学不到;) 你当然可以在尝试过一些之后以评论的形式寻求帮助......祝你好运
关于java - 在无法解决和MasterMind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27451179/