为什么跳过"控制"分割?

时间:2014-11-09 01:39:33

标签: java

所以我试图让用户在SOP完成后立即输入控制值。但它出于某种原因跳过它。

public static void main(String [] args)
      {
         Scanner keyboard = new Scanner(System.in);

         System.out.println("What's your balance?");
         double initialBalance = keyboard.nextDouble();

         Account chase = new Account(initialBalance);

         System.out.println(chase + "; Would you like to deposit or withdraw?");
         String control = keyboard.nextLine();

         if(control == "deposit")
         {
            double deposit = keyboard.nextDouble();
            System.out.println("How much would you like to deposit? " +
                                 deposit);
            chase.deposit(deposit);
            System.out.println(chase);                     

         }

      }

2 个答案:

答案 0 :(得分:0)

  

新守则:

public static void main(String [] args)
{
   Scanner keyboard = new Scanner(System.in);

   System.out.println("What's your balance?");
   double initialBalance = keyboard.nextDouble();

   Account chase = new Account(initialBalance);

   System.out.println(chase + "; Would you like to deposit or withdraw?");

   String control2 = "deposit";
   boolean control = keyboard.next().equalsIgnoreCase(control2);

   if(control == true)
   {
      double deposit = keyboard.nextDouble();
      System.out.println("How much would you like to deposit? " +
                           deposit);
      chase.deposit(deposit);
      System.out.println(chase);                     

   }

} }

答案 1 :(得分:0)

以下是代码片段,其中包含所需的更改。

         Scanner keyboard = new Scanner(System.in);

        System.out.println("What's your balance?");
        double initialBalance = keyboard.nextDouble();
        keyboard.nextLine();

        Account chase = new Account(initialBalance);

        System.out.println("; Would you like to deposit or withdraw?");
        String control = keyboard.nextLine();

        if (control .equals("deposit") ){

        System.out.println("How much would you like to deposit? " );
        double deposit = keyboard.nextDouble();
        keyboard.nextLine();

评论很清楚。您需要查看他们指向的两个帖子。简而言之,有一个行分隔符和equals方法的重要性及其意义。