好的,所以我的原始解决方案的代码完全正常:
package test;
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 N for No and Y for Yes: ");
int answer = input.nextInt();
if (answer == 1)
day += 1;
// Prompt the user to answer questions
System.out.print("\nIs your birthday in Set2?\n");
System.out.print(set2);
System.out.print("\nEnter 0 for No and 1 for Yes: ");
answer = input.nextInt();
if (answer == 1)
day += 2;
// Prompt the user to answer questions
System.out.print("Is your birthday in Set3?\n");
System.out.print(set3);
System.out.print("\nEnter 0 for No and 1 for Yes: ");
answer = input.nextInt();
if (answer == 1)
day += 4;
// Prompt the user to answer questions
System.out.print("\nIs your birthday in Set4?\n");
System.out.print(set4);
System.out.print("\nEnter 0 for No and 1 for Yes: ");
answer = input.nextInt();
if (answer == 1)
day += 8;
// Prompt the user to answer questions
System.out.print("\nIs your birthday in Set5?\n");
System.out.print(set5);
System.out.print("\nEnter 0 for No and 1 for Yes: ");
answer = input.nextInt();
if (answer == 1)
day += 16;
System.out.println("\nYour birthday is " + day + "!");
} }
我的问题是我需要将它从1更改为Y和0更改为N但是我的第二个解决方案不起作用我感到难过我不知道该怎么办我在互联网上看了但是没有解决方案。我能想到的就是这个,它没有正常运作:
package test;
import java.util.Scanner;
public class GuessBirthday2 {
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 N for No and Y for Yes: ");
char answer = (char) input.nextInt();
System.out.print(answer);
if (answer == 'Y')
day += 1;
// Prompt the user to answer questions
System.out.print("\nIs your birthday in Set2?\n");
System.out.print(set2);
System.out.print("\nEnter 0 for No and 1 for Yes: ");
answer = (char) input.nextInt();
if (answer == 'Y')
day += 2;
// Prompt the user to answer questions
System.out.print("Is your birthday in Set3?\n");
System.out.print(set3);
System.out.print("\nEnter 0 for No and 1 for Yes: ");
answer = (char) input.nextInt();
if (answer == 'Y')
day += 4;
// Prompt the user to answer questions
System.out.print("\nIs your birthday in Set4?\n");
System.out.print(set4);
System.out.print("\nEnter 0 for No and 1 for Yes: ");
answer = (char) input.nextInt();
if (answer == 'Y')
day += 8;
// Prompt the user to answer questions
System.out.print("\nIs your birthday in Set5?\n");
System.out.print(set5);
System.out.print("\nEnter 0 for No and 1 for Yes: ");
answer = (char) input.nextInt();
if (answer == 'Y')
day += 16;
System.out.println("\nYour birthday is " + day + "!");
} }
非常感谢任何帮助或提示。
答案 0 :(得分:0)
您应该使用input.next()
或其他内容。然后if(answer.equals("Y") || answer.equals("1")
答案 1 :(得分:0)
如果您希望将字符作为输入,那么您就不应该调用nextInt
。改为调用nextLine
,提取输入的第一个字符。
char answer = input.nextLine().charAt(0);
然后,您可以将answer
与'Y'
和'y'
进行比较;如果是true
,那么您可以添加到day
当前正在进行的操作。
另外,你的第一个问题是
System.out.print("\nEnter N for No and Y for Yes: ");
但你提出的其他问题是:
System.out.print("\nEnter 0 for No and 1 for Yes: ");
每次都应该是一样的。