基本程序终止

时间:2013-12-02 21:42:16

标签: java eclipse if-statement

该程序旨在扫描用户他们收到的前两张牌并告诉他们他们拥有的牌。不幸的是,当它询问“这些卡是否合适?:”时,它会在输入之前终止。我也可以学习如何在switch语句中组织条件,而不是所有这些丑陋的if-else语句。

package Test;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {

            // TODO Auto-generated method stub
            int card1 = 0;
            int card2 = 0;
            String fc1, fc2;
            String answer;


            Scanner scan = new Scanner (System.in);
            System.out.println("First card: ");
            if (scan.hasNextInt())
            {
                    card1 = scan.nextInt();
            }
            else
            {
                    fc1 = scan.next();

                    if (fc1 == "A")
                    {
                            card1 = 14;
                    }
                    else if(fc1 == "K")
                    {
                            card1 = 13;
                    }
                    else if (fc1 == "Q")
                    {
                            card1 = 12;
                    }
                    else if (fc1 == "J")
                    {
                            card1 = 11;
                    }
            }

            System.out.println("Second card: ");
            if (scan.hasNextInt())
            {
                    card2 = scan.nextInt();
            }
            else
            {
                    fc2 = scan.next();
                    if (fc2 == "A")
                    {
                            card2 = 14;
                    }
                    else if (fc2 == "K")
                    {
                            card2 = 13;
                    }
                    else if (fc2 == "Q")
                    {
                            card2 = 12;
                    }
                    else if (fc2 == "J")
                    {
                            card2 = 11;
                    }
            }

         if (card1 == card2)

         {
             System.out.println("You have a pair.");
         }
         else 
         {
        System.out.println("Are your cards suited?(y/n): ");
        answer = scan.nextLine();

             if (answer == "y")
             {
                 if (card1 >12 && card2 > 12)
                 {
                     System.out.println("High value suited cards.");
                 }
                 else
                     if (card1 == card2++ || card1++ == card2)
                     {
                         System.out.println("Suited connectors.");
                     }
             }
             else
                 if(answer == "N")
                 {
                     if (card1 >=12 && card2 >= 12)
                     {
                         System.out.println("High value cards, hold.");
                     }
                     else
                         if(card1 > 5 && card2 > 5)
                         {
                             if(card1++ == card2 || card1 == card2++)
                             {
                                 System.out.println("Connectors.");
                             }
                             else
                             {
                                 System.out.println("Fold");
                             }
                        if (answer != "y" || answer != "n")
                        {
                                System.out.println("Invalid entry.");
                        }
                     }
             }



         }
             }                

}

1 个答案:

答案 0 :(得分:1)

程序终止,因为最终用户在最后一个整数之后添加了换行符作为nextLine()的结尾。

假设用户输入123并按 Enter 。此时,缓冲区中的数据如下所示:

[1] [2] [3] [\n]

调用scan.nextInt()后,整数部分被删除,但[\n]仍保留在缓冲区中。当您致电scan.nextLine()时,该行结束标记是扫描程序看到的第一个字符,因此它会将\n视为空字符串,并将其返回给您的程序。您可以在进行全行输入之前调用scan.nextLine()来修复它。

但是,这本身不会修复您的程序,因为您应该使用equals()比较字符串,例如像这样

if (fc1.equals("A"))

不喜欢这个

if (fc1 == "A") // <<== This will not work correctly
相关问题