如何让我的扫描仪识别我的文字

How to get my scanner to recognize the words i have for it

我对 java 和一般编码还很陌生,我正在努力弄清楚如何让这个游戏在学校的项目中发挥作用。它意味着你输入一个月然后它会要求你选择一天,但是当我输入一个月它总是只是说它是一个无效的输入,这是我想要它在无效时做的月。我做错了什么?

import java.util.*;

class Main {
    public static void main(String[] args) {
        boolean game = true;
        do {
            System.out.println("Welcome to the Famous Date game!");
            System.out.println("Please choose a month");
            Scanner Month = new Scanner(System.in);
            String  Choice = Month.nextLine();
            String[] Months = {"January", "February", "March", "April", "May", "June","July",                    
"August","September","October","November", "December"};
            List<String> mylist = Arrays.asList(Months);
            if (Choice.equals(mylist)) {
                System.out.println("Please choose a day");
            }
            else
                System.out.println("That is not a valid month");
        }
        while (game=true);
    }
}

尝试测试是否包含月份 list.contains() 而其他方法中的日子只是称之为

如果允许您在 java.time package then you can use Month (which is an enum 中使用 classes。这使您不必创建自己的列表。

此外,用户 select 一个月后,她需要 select 一天。我假设用户应该输入一个有效的日期,例如当输入的月份是二月时输入数字 30 将构成无效日期。要检查输入的日期是否有效,可以使用 class YearMonth which has a method for returning the number of days in each month and takes into consideration leap years. Refer to this SO question: Number of days in particular month of particular year?

这是一个SSCCE
(代码后的注释。)

import java.time.LocalDate;
import java.time.Month;
import java.time.YearMonth;
import java.time.temporal.ChronoField;
import java.util.InputMismatchException;
import java.util.Scanner;

public class GameMain {

    public static void main(String[] args) {
        int year = LocalDate.now().get(ChronoField.YEAR);
        Scanner keyboard = new Scanner(System.in);
        boolean game = true;
        do {
            System.out.print("Please choose a month: ");
            String month = keyboard.nextLine();
            try {
                Month theMonth = Month.valueOf(month.toUpperCase());
                YearMonth daysInMonth = YearMonth.of(year, theMonth);
                do {
                    System.out.print("Please choose a day: ");
                    try {
                        String val = keyboard.nextLine();
                        int day = Integer.parseInt(val);
                        if (day < 0 || day > daysInMonth.lengthOfMonth()) {
                            System.out.printf("%s does not have %d days.%n", theMonth, day);
                        }
                        else {
                            game = false;
                        }
                    }
                    catch (NumberFormatException xNumberFormat) {
                        System.out.println("Please enter a number.");
                    }
                } while (game);
            }
            catch (IllegalArgumentException xIllegalArgument) {
                System.out.println("That is not a valid month");
            }
        } while (game);
    }
}

请注意,您只需要创建一次 Scanner。在您问题的代码中,您在 do-while 循环的每次迭代中都创建了一个 Scanner

此外,如果您输入的值在 enum 中不存在,则会引发 IllegalArgumentException
如果输入的值不是整数,方法 parseInt 将抛出 NumberFormatException