Я пытаюсь создать простой 20 questions game
, взяв пользовательский ввод, чтобы принять пользовательский ввод, и я довольно новичок в программировании Java.Пользовательский ввод и If-then и еще
Я установил все строки для своих вопросов, и я хочу спросить пользователя, хотят ли они играть. Я пытался настроить оператор 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);
}
}
}
Спасибо за редактирование :). Не понял, что я это испортил. – KCH39
Возможно, вы захотите прочитать [массивы] (http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html). –
Упрощенно может быть класс «Вопрос». Затем в этом случае есть две строки: «q» и «a», на которых задаются вопросы и ответы. Затем добавьте их в массив Вопрос [20]. Это сильно сократило бы повторение. – Skepi