用户输入和If then and else



我正在尝试创建一个简单的20 questions game,通过接受用户输入来接受用户输入,我对java编程还很陌生。

我已经为我的问题设置了所有的字符串,我想问用户是否想玩。我试图用用户输入设置一个if-then语句,其中数字1Yes,数字2No

我该如何设置?我试过使用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)

 if(in.nextInt() == a){
     System.out.println(q1);
    }

=是赋值运算符,==(相等运算符)用于比较。

使用等于运算符(==)而不是赋值(=)。。。。

in.nextInt();
        if (in.nextInt() == a){
            system.out.println(Q1);

        }

另一种选择是先将该值分配给另一个int,然后检查

int b == in.nextInt()
if(b==a)

您的程序抛出以下编译器错误。没有进行比较,而是试图在.nextInt()中为"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);
    }

最新更新