在无法解决和MasterMind

时间:2014-12-12 19:56:52

标签: java java.util.scanner

好的,我在接受一个价值时遇到了问题...我已经研究了好几天了,但没有什么能满足我的需要。这是一个策划游戏。我正在为我的高中编程课中的最终项目创建这个。 Eclipse编译器告诉我,无法解决。我该如何解决这个问题并完成我的目标。这不是在applet中运行。

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();

我的编码知识非常有限,所以请保持简单并解释

3 个答案:

答案 0 :(得分:1)

对于nextInt方法,您应该从Scanner对象

中调用它

更改此

int num = in.nextInt();

int num = UserGuess.nextInt();

答案 1 :(得分:0)

System.in是系统的InputStream(就像windows的cmd一样),为了从中读取你使用Scanner或InputStreamReader,就像你试图做的那样...而不是

in.nextInt();

你需要

userGuess.nextInt(); 

并且btw学会正确使用大写字母,因为它会在以后帮助你,就像userGuess不应该以资本开头,因为它的实例不是类。

无论如何,对于你的游戏,你必须猜测10次,这意味着你必须重复相同的猜测动作10次,或者直到用户猜出所有的数字,那就是你应该使用while循环这样的...... / p>

 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++;
}

现在我几乎给了你完成这项功课所需的所有算法,但是在你尝试之前我不会为你解决它,否则你什么都不会学到;) 在你尝试了一些......祝你好运之后,你可以求助于评论

答案 2 :(得分:0)

您在启动课程时也从未关闭括号,也未启动主方法。将每个{}匹配。

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 By randomly generating the number

    //After generating the code, print the blank board to start the game
    System.out.println("__ __ __ __");

    //Take in the user's guess (limit is 10)
    int limit = 10
    for(int i = 0; i < limit; ++i) {
        Scanner UserGuess = new Scanner(System.in);
        int num = in.nextInt();
    //Other logic..Leaving it for you to attempt


    }
}

你想制作一个程序,用户必须猜测每个数字在4位数代码中的位置..那你怎么做?

我们需要用户能够输入数字,通常这个游戏的规则是:

  1. 有6种可能的数字或颜色
  2. 代码长度为4个数字或颜色
  3. 用户有十次尝试正确猜测代码
  4. 这意味着我们必须从做这样的事情开始。

    1. 以某种方式生成4位数代码(使用0000-6666的可能组合)并将随机数拆分成数组
    2. 要求用户输入数字猜测以及该数字的位置
    3. 继续检查用户对代码的猜测,每次显示当前猜测以及哪些是正确的