字符串输入有问题吗? [英] String input problems?

查看:80
本文介绍了字符串输入有问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经环顾了一段时间,但找不到完全相同的问题.我正在编写一个程序,用户可以选择要询问的问题类型,然后输入答案.如果他们做对了,他们的分数就会加分.当答案不正确时,我可以使它工作,但是当答案正确时,它就不能工作.我知道这种情况的发生是因为由于某些原因输入的字符串与正确答案存储的内容不匹配,但是我无法弄清楚原因.这是代码的一部分:

I've looked around for a while and I couldn't find an exactly similar question. I'm writing a program that has a user choose what type of question they will be asked, then they input their answer. If they get it right, a point is added to their score. I can get it to work when the answers are incorrect, but not when they're correct. I know this is happening because the inputted strings do not match what the correct answers are stored as for some reason, but I cannot figure out why. Here's a section of the code:

System.out.println("What school do the Badgers belong to?");
mascotQuestion1 = scan.next();
if (mascotQuestion1.equalsIgnoreCase("University of Michigan")) {
    score++;
}
else if (mascotQuestion1.equalsIgnoreCase("don't know")) {
    score = (score + 0);
} 
else {
    score--;
}

基本上,if和else if语句不起作用.每个输入都发送到else语句.有什么问题吗?

Basically, the if and else if statements don't work. Every input is sent to the else statement. What's the problem?

因此,我进入密歇根大学"后尝试打印输入的mascotQuestion1,然后又返回大学".这就是为什么它出错了,但是我不知道如何解决这个问题.

So, I tried printing the inputted mascotQuestion1 after entering "University of Michigan", and it came back "University". This is why it wrong, but I don't know how to fix this.

推荐答案

尝试scan.nextLine().我想scan.next()只会给你下一个单词.

Try scan.nextLine(). I think scan.next() would only give you the next word.

这篇关于字符串输入有问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆