我正在尝试创建一个简单的20 questions game
,通过获取用户输入来获取用户输入,而且我对编程java很新。
我已经为我的问题设置了所有字符串,我想询问用户是否想玩。我尝试使用用户输入设置if-then
语句,数字1
为Yes
,数字2
为No
。
我该如何设置?我尝试了if(in.nextInt() = a)
声明,但我知道这不对。我知道我需要引用以前的用户输入,但我该怎么做?提前感谢你的帮助。
import java.util.*;
public class twentyq {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int a = 1;
int b = 2;
// all the Strings needed for Questions[represented by the Q(1-20) variable] and their Answers[represented by the AQ(1-20) variable]
String Q1;
String Q2;
String Q3;
String Q4;
String Q5;
String Q6;
String Q7;
String Q8;
String Q9;
String Q10;
String Q11;
String Q12;
String Q13;
String Q14;
String Q15;
String Q16;
String Q17;
String Q18;
String Q19;
String Q20;
String AQ1;
String AQ2;
String AQ3;
String AQ4;
String AQ5;
String AQ6;
String AQ7;
String AQ8;
String AQ9;
String AQ10;
String AQ11;
String AQ12;
String AQ13;
String AQ14;
String AQ15;
String AQ16;
String AQ17;
String AQ18;
String AQ19;
String AQ20;
// The questions and their answers in numerical order question first then answer immediately following.
Q1 = "Where would you find the Sea of Tranquility?";
AQ1 = "The Moon.";
Q2 = "What is the Capital of Spain";
AQ2 = "Madrid.";
Q3 = "What is the painting, La Gioconda, more usually known as?";
AQ3 = "The Mona Lisa.";
Q4 = "Which chess piece can only move diagonally?";
AQ4 = "A Bishop.";
Q5 = "What is the oldest surviving printed book in the world?";
AQ5 = "The Diamond Sutra, dated at 868 AD.";
Q6 = "Costing around $2,600 per pound, and made only to order by Knipschildt, what is the name of this chocolate truffle?";
AQ6 = "Chocopologie";
Q7 = "Who invented TV?";
AQ7 = "George Carey, a Boston civil servant, first thought up television in 1876. John Logie Baird is often quoted as its inventor but his ideas didn't come along until the 1920's.";
Q8 = "What is allspice alternatively known as?";
AQ8 = "Pimento.";
Q9 = "In publishing, what does POD mean?";
AQ9 = "Print on demand.";
Q10 = "What is John Leach famous for making?";
AQ10 = "Pottery.";
Q11 = "When was the euro introduced as legal currency on the world market?";
AQ11 = "1st January, 1999.";
Q12 = "How many valves does a trumpet have?";
AQ12 = "3.";
Q13 = "Which kind of bulbs were once exchanged as a form of currency?";
AQ13 = "Tulips.";
Q14 = "Name the director of the Lord of the Rings trilogy.";
AQ14 = "Peter Jackson.";
Q15 = "Name the largest fresh water lake in the world?";
AQ15 = "Lake Superior.";
Q16 = "Name the seventh planet from the sun.";
AQ16 = "Uranus.";
Q17 = "Which country is Prague in?";
AQ17 = "Czech Republic.";
Q18 = "What is the oldest film ever made, and when was it made?";
AQ18 = "Roundhay Garden Scene, made in 1888.";
Q19 = "Name the three primary colors.";
AQ19 = "Red, yellow and blue.";
Q20 = "How old is the world's oldest dictionary?";
AQ20 = "Cuniform tablets with bilingual Sumerian-Akkadian word-lists have been dated to 2300 BC.";
System.out.println("Welcome To KCH39's 20 Questions!");
System.out.println("Would you like to play? If yes, press 1 and enter. If not, press 2 and enter.");
in.nextInt();
if (in.nextInt() = a){
system.out.println(Q1);
}
}
}
答案 0 :(得分:2)
您正在使用赋值运算符而不是equals运算符:
if (in.nextInt() == a){
system.out.println(Q1);
}
答案 1 :(得分:1)
在您当前的代码中,将(in.nextInt() = a)
更改为(in.nextInt() == a)
if(in.nextInt() == a){
System.out.println(q1);
}
=
是赋值运算符,==
(相等运算符)用于比较。
答案 2 :(得分:0)
使用等于运算符(==)而不是赋值(=)....
in.nextInt();
if (in.nextInt() == a){
system.out.println(Q1);
}
其他选项是首先将该值分配给另一个int,然后检查
int b == in.nextInt()
if(b==a)
答案 3 :(得分:0)
您的程序抛出编译器错误。比较你试图将'a'的值分配到in.nextInt()这是不可能的。
twentyq.java:120: error: unexpected type
if (in.nextInt() = a){
^
required: variable
found: value
1 error
所以它必须修正如下(即使用double equals):
if (in.nextInt() == a){
System.out.println(Q1);
}