我是java的新手,试图从这本书中学习一门课程并且我坚持制作一个程序。如果没有错误并且无法找到标记为guessdigit1&的符号,请继续告诉我。 2。
我一直说当我将鼠标悬停在符号旁边的错误对话框上时,guessdigit1和2是他们自己的类。任何的想法? 谢谢!
package loterry;
import java.util.Scanner;
public class Loterry {
// This program creates two random numbers, and checks to see if your guess
// makes the lottery win
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter in your guess for loterry, two digits please");
int lottery = (int)(Math.random()*100 /50);
int guess = input.nextInt();
int lotterydigit1= lottery /10;
int lotterydigit2= lottery %10;
// Get digits from guess
int guessdigit1 = guess / 10;
int guessdigit2 = guess % 10;
System.out.println("The lottery number is " + lottery);
if (guess == lottery)
System.out.println("Exact Match: you win 10,000");
else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2);
System.out.println("Match all digits: you win 3,000");
else if (guessdigit1 == lotterydigit1
|| guessdigit1 == lotterydigit2
|| guessdigit2 == lotterydigit2
|| guessdigit2 == lotterydigit2)
System.out.println("match one digit: you win 1,000");
else
System.out.println("sorry no match");
}
}
答案 0 :(得分:11)
这是你的问题:
else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2);
删除分号(;
)
else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2)
注意:建议您始终使用方括号if else
{
撰写}
语句,以避免将来出现任何错误,当然还有在Java code conventions。 (有关括号问题的说明,请参阅this answer)
else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2){
System.out.println("Match all digits: you win 3,000");
}
答案 1 :(得分:3)
删除semicolon (;)
以避免编译错误
使用if else语句中的curly braces "{ }"
块来避免混淆
if (guess == lottery)
System.out.println("Exact Match: you win 10,000");
else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2); //->here
答案 2 :(得分:2)
替换
else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2);
带
else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2)
在撰写{}
时,您应该使用if else
。它使clean code
和clean code
易于调试。
答案 3 :(得分:0)
只需删除您在第一个结尾处放置的分号(;)。
只有语句的末尾应该有分号(;)
工作代码:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter in your guess for loterry, two digits please");
int lottery = (int)(Math.random()*100 /50);
int guess = input.nextInt();
int lotterydigit1= lottery /10;
int lotterydigit2= lottery %10;
// Get digits from guess
int guessdigit1 = guess / 10;
int guessdigit2 = guess % 10;
System.out.println("The lottery number is " + lottery);
if (guess == lottery)
System.out.println("Exact Match: you win 10,000");
else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2)
System.out.println("Match all digits: you win 3,000");
else if (guessdigit1 == lotterydigit1
|| guessdigit1 == lotterydigit2
|| guessdigit2 == lotterydigit2
|| guessdigit2 == lotterydigit2)
System.out.println("match one digit: you win 1,000");
else
System.out.println("sorry no match");
}
答案 4 :(得分:0)
是的那个分号之后,如果声明是搞乱你的话。但严重的是,虽然我建议你在将来使用括号...它使代码看起来更整洁,从而更容易跟踪事物......相信我,它在处理更大的程序或在团队中工作时确实有帮助
答案 5 :(得分:0)
在条件之后只需重新调用;
并使用{}
每个语句
请参阅此处docs
if (guess == lottery){
//code here
}
else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2){
//code here
}
else if (guessdigit1 == lotterydigit1
|| guessdigit1 == lotterydigit2
|| guessdigit2 == lotterydigit2
|| guessdigit2 == lotterydigit2){
//code here
}
else{
//code here
}