【问题标题】:Birthday guesser in Java, code lines are skipped for no obvious reasonJava 中的生日猜测者,无明显原因跳过代码行
【发布时间】:2015-09-10 15:05:36
【问题描述】:

我正在练习 Y. Daniel Liang 所著的“Java 编程简介”一书中的练习。在这个程序中,用户会看到一个包含一个月内不同日期的表格。如果用户的生日在显示的表格中,则用户提示“Y”表示是,“N”表示否。根据用户提示“Y”的次数,程序应该猜测用户生日是一个月中的哪一天。当我运行程序并显示第一组日期时,我输入“Y”或“N”。然后打印 set2 和 3,但我没有输入任何输入。打印set 4时,我可以再次输入输入,但是打印set 5后,无法输入任何输入。为什么我只能在 set1 和 4 之后输入输入?代码如下:

import java.util.Scanner;


public class GuessBirthday {

public static void main(String[] args)
        throws java.io.IOException {

String set1 = 
    "  1  3  5  7\n" +
    "  9 11 13 15\n" +
    " 17 19 21 23\n" + 
    " 25 27 29 31";

    String set2 = 
    "  2  3  6  7\n" +
    " 10 11 14 15\n" +
    " 18 19 22 23\n" + 
    " 26 27 30 31";

    String set3 = 
    "  4  5  6  7\n" +
    " 12 13 14 15\n" +
    " 20 21 22 23\n" + 
    " 28 29 30 31";

    String set4 = 
    "  8  9 10 11\n" +
    " 12 13 14 15\n" +
    " 24 25 26 27\n" + 
    " 28 29 30 31";

    String set5 = 
    " 16 17 18 19\n" +
    " 20 21 22 23\n" +
    " 24 25 26 27\n" + 
    " 28 29 30 31";

    int day = 0;

    // Create a scanner

    Scanner input = new Scanner(System.in);

    // Prompt the user to answer questions

    System.out.print("Is your birthday in set1?\n");
    System.out.print(set1);
    System.out.print("\nEnter (Y) for yes or (N) for no: ");
    char answer = (char) System.in.read();

    if (answer == 'Y')
        day += 1;

    System.out.print("\nIs your birthday in set2?\n");
    System.out.print(set2);
    System.out.print("\nEnter (Y) for yes or (N) for no: ");
    answer = (char) System.in.read();

    if (answer == 'Y')
        day += 2;

    System.out.print("\nIs your birthday in set3?\n");
    System.out.print(set3);
    System.out.print("\nEnter (Y) for yes or (N) for no: ");
    answer = (char) System.in.read();

    if (answer == 'Y')
        day += 4;

    System.out.print("\nIs your birthday in set4?\n");
    System.out.print(set4);
    System.out.print("\nEnter (Y) for yes or (N) for no: ");
    answer = (char) System.in.read();

    if (answer == 'Y')
        day += 8;

    System.out.print("\nIs your birthday in set5?\n");
    System.out.print(set5);
    System.out.print("\nEnter (Y) for yes or (N) for no: ");
    answer = (char) System.in.read();

    if (answer == 'Y')
        day += 16;

    System.out.println("\nYour birthday is " + day + "!");

    }
}

【问题讨论】:

  • 如果不使用Scanner input,为什么要创建它?
  • 练习是重写程序,其中整数 0 和 1 用于“否”和“是”,而是将字符“Y”和“N”作为输入。我实际上忘记了在使用 System.in.read(); 时我不需要它; .但是您提出了一个有效的观点,感谢您指出!

标签: java string input character


【解决方案1】:

在调用 System.in.read() 时,您永远不会真正吞噬换行符

【讨论】:

    【解决方案2】:

    您一次只能读取 1 个字符,因为您使用的是 System.in.read()。回车 (\r) 和/或换行 (\n) 将计为字符。所以在你第一次输入时,例如在你输入了'Y\r\n'的窗口上,所以 System.in.read() 第一次会得到'Y',下一次会收到'\r'。

    你可以使用你实例化的Scanner..

    String answer = input.nextLine();
    if (answer.equals("Y") {
        // blah
    }
    

    【讨论】:

      【解决方案3】:

      我想知道同样的事情(使用 0 和 1 很奇怪)所以对于在这里偶然发现的任何其他人,我得到了它的工作(注意 - 你也可以用 'char' 来做到这一点,只需将单词 'String' 替换为'char'):

      import java.util.Scanner;
      
      
      public class GuessBirthday
      {
      public static void main( String [] args )
      {
      
      String set1 = 
      "  1  3  5  7\n" +
      "  9 11 13 15\n" +
      " 17 19 21 23\n" + 
      " 25 27 29 31";
      
      String set2 = 
      "  2  3  6  7\n" +
      " 10 11 14 15\n" +
      " 18 19 22 23\n" + 
      " 26 27 30 31";
      
      String set3 = 
      "  4  5  6  7\n" +
      " 12 13 14 15\n" +
      " 20 21 22 23\n" + 
      " 28 29 30 31";
      
      String set4 = 
      "  8  9 10 11\n" +
      " 12 13 14 15\n" +
      " 24 25 26 27\n" + 
      " 28 29 30 31";
      
      String set5 = 
      " 16 17 18 19\n" +
      " 20 21 22 23\n" +
      " 24 25 26 27\n" + 
      " 28 29 30 31";
      
      int day = 0;
      
      // Create a scanner
      
      Scanner input = new Scanner(System.in);
      
      // Prompt the user to answer questions
      
      System.out.print("Is your birthday in set1?\n");
      System.out.print(set1);
      System.out.print("\nEnter (Y) for yes or (N) for no: ");
      String answer = input.next();
      
      if (answer.equals("Y"))
          day += 1;
      
      System.out.print("\nIs your birthday in set2?\n");
      System.out.print(set2);
      System.out.print("\nEnter (Y) for yes or (N) for no: ");
      answer = input.next();
      
      if (answer.equals("Y"))
          day += 2;
      
      System.out.print("\nIs your birthday in set3?\n");
      System.out.print(set3);
      System.out.print("\nEnter (Y) for yes or (N) for no: ");
      answer = input.next();
      
      if (answer.equals("Y"))
          day += 4;
      
      System.out.print("\nIs your birthday in set4?\n");
      System.out.print(set4);
      System.out.print("\nEnter (Y) for yes or (N) for no: ");
      answer = input.next();
      
      if (answer.equals("Y"))
          day += 8;
      
      System.out.print("\nIs your birthday in set5?\n");
      System.out.print(set5);
      System.out.print("\nEnter (Y) for yes or (N) for no: ");
      answer = input.next();
      
      if (answer.equals("Y"))
          day += 16;
      
      System.out.println("\nYour birthday is " + day + "!");
      
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2018-09-06
        • 1970-01-01
        • 2022-01-25
        • 1970-01-01
        • 2020-06-07
        • 2023-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多