如果语句错误地评估字符串

时间:2014-04-17 20:51:33

标签: java string if-statement

编译这个程序时,每个案例中的if语句总是在给出正确答案时评估为false(错误),这里是代码:

import java.util.Scanner;

class GameStarts {
  public static void main(String[] args){
   Scanner sc = new Scanner(System.in);
   String ret;
   byte qnum;
   String ans;

   String correct = "Awesomely correct!";
   String wrong = "Darn it! Almost got it!";

   System.out.println("Do you think you know your stuff?");
   ret = sc.nextLine();

   if (ret.equals("yes") || ret.equals("Yes"))
   {
    System.out.println("Well, then let's test what you know! Choose a number from 1 to 5!");
    qnum = sc.nextByte();
      switch (qnum)
      {
     case 1:
    System.out.println("In what year did the French Revolution start?");
    ans = sc.nextLine();
    sc.nextLine();
      if (ans.equals("1789") || ans.equalsIgnoreCase("seventeen eighty nine"))
      {System.out.println(correct);}
      else
      {System.out.println(wrong);}

     break;

    case 2:
    System.out.println("How many protons does a sodium atom have?");
    ans = sc.nextLine();
    sc.nextLine();
      if (ans.equals("11") || ans.equalsIgnoreCase("eleven"))
      {System.out.println(correct);}
      else
      {System.out.println(wrong);}

     break;

    case 3:
    System.out.println("What is 2^6*0.5-12?");
    ans = sc.nextLine();
    sc.nextLine();
      if (ans.equals("20") || ans.equalsIgnoreCase("twenty"))
      {System.out.println(correct);}
      else
      {System.out.println(wrong);}

     break;

    case 4:
    System.out.println("Which is the lowest numbered element in the periodic table?");
    ans = sc.nextLine();
    sc.nextLine();
      if (ans.equalsIgnoreCase("hydrogen"))
      {System.out.println(correct);}
      else
      {System.out.println(wrong);}

     break;

    case 5:
    System.out.println("Which is the unit that measures Coulombs per second?");
    ans = sc.nextLine();
    sc.nextLine();
      if (ans.equalsIgnoreCase("ampere"))
      {System.out.println(correct);}
      else
      {System.out.println(wrong);}

     break;
    default:
    System.out.println("Stick to the rules! 1-5!");
     }

  }
   else
   {System.out.println("Not liking that attitude, I want to hear a big yes!");}

 }
}

我不确定它是否会跳过ans定义,或者我是否遗漏了某些内容。此外,我还要感谢任何其他建议:)

3 个答案:

答案 0 :(得分:1)

readline最后可能有一个换行符。试试

ans.trim().equalsIgnoreCase...

修剪将摆脱任何虚假空间或你可能有的新行字符

答案 1 :(得分:1)

尝试替换

ret = sc.nextLine();

使用

ret = sc.nextLine();
ret = ret.replace("\n","").replace("\t","").trim();

什么是删除任何换行符(或标签符)并删除所有尾随和前导空格(这些东西经常进入)

编辑:

使用sc.nextLine()在所有行之后插入这样的行,如果我是正确的,它应该排除

编辑: 这不是问题所在。当你执行sc.readByte时,它会读取2个字节 - 字符和换行符。当你接下来做sc.nextLine时,它会得到剩余的换行符。解决方案:

Scanner sc = new Scanner(System.in);
       String ret;
       String qnum;
       String ans;

       String correct = "Awesomely correct!";
       String wrong = "Darn it! Almost got it!";

       System.out.println("Do you think you know your stuff?");
       ret = sc.nextLine();

       if (ret.equals("yes") || ret.equals("Yes"))
       {
        System.out.println("Well, then let's test what you know! Choose a number from 1 to 5!");
        qnum = sc.nextLine();
          switch (qnum)
          {
         case "1":
        System.out.println("In what year did the French Revolution start?");
        ans = sc.nextLine();
          if (ans.equals("1789") || ans.equalsIgnoreCase("seventeen eighty nine"))
          {System.out.println(correct);}
          else
          {System.out.println(wrong);}

         break;

        case "2":
        System.out.println("How many protons does a sodium atom have?");
        ans = sc.nextLine();
        sc.nextLine();
          if (ans.equals("11") || ans.equalsIgnoreCase("eleven"))
          {System.out.println(correct);}
          else
          {System.out.println(wrong);}

         break;

        case "3":
        System.out.println("What is 2^6*0.5-12?");
        ans = sc.nextLine();
        sc.nextLine();
          if (ans.equals("20") || ans.equalsIgnoreCase("twenty"))
          {System.out.println(correct);}
          else
          {System.out.println(wrong);}

         break;

        case "4":
        System.out.println("Which is the lowest numbered element in the periodic table?");
        ans = sc.nextLine();
        sc.nextLine();
          if (ans.equalsIgnoreCase("hydrogen"))
          {System.out.println(correct);}
          else
          {System.out.println(wrong);}

         break;

        case "5":
        System.out.println("Which is the unit that measures Coulombs per second?");
        ans = sc.nextLine();
        sc.nextLine();
          if (ans.equalsIgnoreCase("ampere"))
          {System.out.println(correct);}
          else
          {System.out.println(wrong);}

         break;
        default:
        System.out.println("Stick to the rules! 1-5!");
         }

      }
       else
       {System.out.println("Not liking that attitude, I want to hear a big yes!");}

我在代码中更改的是我将qnum的类型更改为String,更改了case语句以使用字符串,并将readByte更改为readLine。另外,我删除了第二个不需要的新行。

答案 2 :(得分:0)

您还使用sc.nextByte尝试阅读1到5。 那不会起作用。

您必须将其作为字符串读取,然后按上述方式修剪它。

在早期版本的Java中,您可以使用Integer.valueOf()将其转换为int,但从java 7开始,您可以打开一个字符串:

switch(str)
{
    case "1":
    ....