Java 中的生日猜测器,代码行被无缘无故地跳过
Birthday guesser in Java, code lines are skipped for no obvious reason
我正在做 Y. Daniel Liang 的书 "Introduction to Java Programming" 中的练习。在这个程序中,用户会看到一个 table ,其中包含一个月中的不同日期。如果用户生日在呈现的table中,用户提示'Y'是,'N'不是。根据用户提示的次数 'Y',程序应该猜测用户生日是一个月中的哪一天。当我 运行 程序并显示第一组日期时,我输入 'Y' 或 'N'。然后打印 set2 和 3,但我没有输入任何输入。打印set 4时,我可以再次输入,但是打印set 5后,无法输入。为什么我只能在 set1 和 4 之后输入 input?代码如下:
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 + "!");
}
}
您实际上从未通过调用 System.in.read()
吞噬换行符
您一次只能读取 1 个字符,因为您正在使用 System.in.read()
。 carriage-return (\r) and/or 换行符 (\n) 将计为字符。因此,在您的第一次输入中,例如在 windows 上,您输入了 'Y\r\n',因此 System.in.read() 第一次将得到 'Y',下一次将收到 '\r'。
您可以使用实例化的 Scanner
..
String answer = input.nextLine();
if (answer.equals("Y") {
// blah
}
我想知道同样的事情(使用 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 + "!");
}
}
我正在做 Y. Daniel Liang 的书 "Introduction to Java Programming" 中的练习。在这个程序中,用户会看到一个 table ,其中包含一个月中的不同日期。如果用户生日在呈现的table中,用户提示'Y'是,'N'不是。根据用户提示的次数 'Y',程序应该猜测用户生日是一个月中的哪一天。当我 运行 程序并显示第一组日期时,我输入 'Y' 或 'N'。然后打印 set2 和 3,但我没有输入任何输入。打印set 4时,我可以再次输入,但是打印set 5后,无法输入。为什么我只能在 set1 和 4 之后输入 input?代码如下:
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 + "!");
}
}
您实际上从未通过调用 System.in.read()
吞噬换行符您一次只能读取 1 个字符,因为您正在使用 System.in.read()
。 carriage-return (\r) and/or 换行符 (\n) 将计为字符。因此,在您的第一次输入中,例如在 windows 上,您输入了 'Y\r\n',因此 System.in.read() 第一次将得到 'Y',下一次将收到 '\r'。
您可以使用实例化的 Scanner
..
String answer = input.nextLine();
if (answer.equals("Y") {
// blah
}
我想知道同样的事情(使用 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 + "!");
}
}