好的,我在接受一个价值时遇到了问题...我已经研究了好几天了,但没有什么能满足我的需要。这是一个策划游戏。我正在为我的高中编程课中的最终项目创建这个。 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();
我的编码知识非常有限,所以请保持简单并解释
答案 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位数代码中的位置..那你怎么做?
我们需要用户能够输入数字,通常这个游戏的规则是:
这意味着我们必须从做这样的事情开始。