Java 用户输入和If-then和else
我试图创建一个简单的Java 用户输入和If-then和else,java,eclipse,Java,Eclipse,我试图创建一个简单的20个问题的游戏,通过用户输入来获取用户输入,我对java编程相当陌生 我已经为我的问题设置了所有的字符串,我想问用户是否想玩。我试图用用户输入设置一个if-then语句,数字1为Yes,数字2为No 我该怎么设置呢?我尝试了我的if(in.nextInt()=a)语句,但我知道这是不对的。我知道我需要引用以前的用户输入,但是我该怎么做呢?提前谢谢你的帮助 import java.util.*; public class twentyq { public stat
20个问题的游戏
,通过用户输入来获取用户输入,我对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);
}
}
}
您正在使用赋值运算符而不是等于运算符:
if (in.nextInt() == a){
system.out.println(Q1);
}
在当前代码中,将
(In.nextInt()=a)
更改为(In.nextInt()=a)
=
是赋值运算符,=
(相等运算符)用于比较
使用等于运算符(=),而不是赋值(=)
另一个选项是先将该值分配给另一个int,然后进行检查
int b == in.nextInt()
if(b==a)
您的程序抛出以下编译器错误。为了避免比较,您正在尝试将“a”的值赋给in.nextInt(),这是不可能的
twentyq.java:120: error: unexpected type
if (in.nextInt() = a){
^
required: variable
found: value
1 error
因此,它必须固定如下(即使用双等于):
感谢您的编辑:)。我不知道我把事情搞砸了。你可能想好好读一读。更简单的可能是在课堂上提出一个“问题”。然后在这个“q”和“a”中有两个字符串,它们包含问题和答案。然后将这些问题添加到数组问题[20]。这将大大减少重复。现在,当我按1并按enter键时,它不会打印下一行。我如何修复它?将system.out.println()更改为system.out.println()?
int b == in.nextInt()
if(b==a)
twentyq.java:120: error: unexpected type
if (in.nextInt() = a){
^
required: variable
found: value
1 error
if (in.nextInt() == a){
System.out.println(Q1);
}