简体   繁体   English

不在if / else语句中打印我的代码

[英]not printing my code in if /else statement

So im creating jeapordy in java, and i dont care that i spelt it wrong(if i did) but i only have one question coded so far with only one answer, and it asks the question but only prints out you are wrong even if the answer is right. 所以我在java中创建jeapordy,我不在乎我拼写错误(如果我这样做)但我只有一个问题到目前为止只有一个答案编码,它问问题但只打印出你错了即使答案是对的。

it is asking the first history question and the answer is george, but it is printing out that the answer is wrong. 它问的是第一个历史问题,答案是乔治,但它打印出的答案是错误的。 the first history question is also worth 100. i have not began to code the math part yet. 第一个历史问题也值100。我还没有开始编码数学部分。

thanks if you can help fin my problem! 谢谢,如果你能帮我解决问题! its probably really simple as i am a beginner. 它可能非常简单,因为我是初学者。

import java.util.Random;
import java.util.Scanner;

public class game {
public static void main (String[] args){
    //Utilites
    Scanner s = new Scanner(System.in);
    Random r = new Random();

    //Variables
    String[] mathQuestions;
    mathQuestions = new String[3];
    mathQuestions[0] = ("What is the sum of 2 + 2");
    mathQuestions[1] = ("What is 100 * 0");
    mathQuestions[2] = ("What is 5 + 5");

    String[] historyQuestions;
    historyQuestions = new String[3];
    historyQuestions[0] = ("What is General Washingtons first name?");
    historyQuestions[1] = ("Who won WWII, Japan, or USA?");
    historyQuestions[2] = ("How many states are in the USA?");


    //Intro
    System.out.println("Welome to Jeapordy!");
    System.out.println("There are two categories!\nMath and History");
    System.out.println("Math       History");
    System.out.println("100          100");
    System.out.println("200          200");
    System.out.println("300          300");


    System.out.println("Which category would you like?");
        String categoryChoice = s.nextLine();
    System.out.println("For how much money?");
        int moneyChoice = s.nextInt();
            if (categoryChoice.equalsIgnoreCase("history")){
                if (moneyChoice == 100){
                    System.out.println(historyQuestions[0]);
                    String userAnswer = s.nextLine();
                    s.nextLine();
                    if (userAnswer.equalsIgnoreCase("george")){
                        System.out.println("Congratulations! You were right");
                    }
                    else{
                        System.out.println("Ah! Wrong answer!");
                    }

                }
            }

        }
}

When you call nextInt() , a newline character is left unread, so a subsequent call to nextLine() will return an empty string (since it reads up to the end of the line). 当你调用nextInt() ,换行符未被读取,因此对nextLine()的后续调用将返回一个空字符串(因为它读取到行的末尾)。 Call newLine() once prior to read/discard this trailing newline: 在读取/丢弃此尾随换行符之前调用newLine()一次:

if (moneyChoice == 100) {
    System.out.println(historyQuestions[0]);
    s.nextLine();  // <--
    String userAnswer = s.nextLine();
    System.out.println(userAnswer);
    ...

As an aside, don't forget to close your Scanner when you're finished with it: s.close() . 另外,当你完成它时,不要忘记关闭你的Scanners.close()

int moneyChoice = s.nextInt(); reads only the integer. 只读整数。 It leaves a newline pending reading. 它留下了一条待换行。 Then String userAnswer = s.nextLine() ; 然后String userAnswer = s.nextLine() ; reads an empty line that is obviously different from "george". 读取与“乔治”明显不同的空行。 Solution: read the newline immediately after the int, and do so in the whole program. 解决方案:在int之后立即读取换行符,并在整个程序中执行。 You could prefer to create your own method nextIntAndLine() . 您可能更喜欢创建自己的方法nextIntAndLine()

int moneyChoice= s.nextInt() ;
s.nextLine();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM