【问题标题】:How do I fix my code? [closed]如何修复我的代码? [关闭]
【发布时间】:2015-10-02 02:43:36
【问题描述】:

您好,我正在做一个项目,我正在尝试找出我的代码有什么问题,以便正确打印。编写一个 Java 程序,提示用户输入他或她生日的日期和月份(均为整数),然后打印相应的占星符号。

public class Birthday {
    public static void main(String args [] ) {
        String birthday = System.console().readline("Enter your birthday, month then day.");
        float value = Float.parseFloat(birthday);
        if (month < 1 || month > 12) {
            System.out.println("Invalid month please try again.");
        }

        else if (day < 1 || day > 31) {
            System.out.println("Invalid day please try again.");
        }

        else if (month = 1 || month = 12) {

        }

        else if (day >= 22 || day <= 19); {
            System.out.println(birthday + "Capricorn");
        }
    }
}

这些是我得到的错误:

Birthday.java:3: readline(boolean) in java.io.Console cannot be applied to (java.lang.String)
    float birthday = System.console().readline("Enter your birthday, month then day.");
                                     ^
Horoscopes.java:4: cannot find symbol
symbol  : variable month
location: class Birthday
        if (month < 1 || month > 12) {  
            ^
Horoscopes.java:4: cannot find symbol
symbol  : variable month
location: class Birthday
        if (month < 1 || month > 12) {  
                         ^
Horoscopes.java:8: cannot find symbol
symbol  : variable day
location: class Birthday
    else if (day < 1 || day > 31) {
             ^
Horoscopes.java:8: cannot find symbol
symbol  : variable day
location: class Birthday
    else if (day < 1 || day > 31) {
                        ^
Horoscopes.java:12: cannot find symbol
symbol  : variable month
location: class Birthday
    else if (month = 1 || month = 12) {
             ^
Horoscopes.java:12: cannot find symbol
symbol  : variable month
location: class Birthday
    else if (month = 1 || month = 12) {
                          ^
Horoscopes.java:16: cannot find symbol
symbol  : variable day
location: class Birthday
    else if (day >= 22 || day <= 19); {
             ^
Horoscopes.java:16: cannot find symbol
symbol  : variable day
location: class Birthday
    else if (day >= 22 || day <= 19); {
                          ^
9 errors

【问题讨论】:

  • 有什么问题?
  • 我看不到你在哪里设置 monthday
  • 我不断收到错误。我找不到符号。
  • 嗨。正确缩进你的代码。具体描述您遇到的问题。为您的问题使用一个合理的标题,以表明您的问题。
  • 请添加您收到的错误消息和回溯。另外,请将标题更改为描述相关问题的内容。

标签: java validation date input user-input


【解决方案1】:

乍一看,您并没有定义 month 是什么 day

另外,你正在做的是Float.parseFloat(birthday),但所有这一切都会给你一个float,看起来像这样:

如果我输入20150713我会得到

float value = 20150713.00

这不会将它们分成几个月和几天。

我建议不要将String birthday 转换为浮点数,而是将其转换为日期结构。

一旦有了日期结构,比如DateLocalDate,您就可以比较monthday

【讨论】:

    【解决方案2】:

    你可能想做这样的事情(只是部分,我不想破坏你的功课):

    public static final void main(String args...) {
        while(true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("please enter the month in which you are born:");
            int month = sc.nextInt();
            System.out.println("please enter the day in which you are born:");
            int day = sc.nextInt();
            if (!checkMonth(month)) {
                System.out.println("Invalid month please try again.");
                continue;
            } else if (!checkDay(day)) {
                System.out.println("Invalid day please try again.");
                continue;
            } else {
                // todo: work with valid birthday
            }
        }
    }
    
    public static boolean checkMonth(int month){
       // todo: check if valid
       return true;
    }
    
    public static boolean checkDay(int day){
       // todo: check if valid
       return true;
    }
    

    【讨论】:

    • 我在我的项目中添加了一些你建议的内容,但我收到一条错误消息,指出解析时已到达文件末尾 } ^ 1 错误
    • 我只是让您了解如何处理在 while 循环中读取用户输入的任务,直到它不包含任何错误。您实际上必须实现方法boolean checkDay(String s)boolean checkMonth(String s)String readBirthday() 也必须由你实现..
    • 给你,我添加了更多细节。另外:您实际上想将两个参数(月份和日期)传递给 checkDay(...) 方法,因为您需要月份来检查日期是否有效(实际上您还需要年份,因为 leep-years ..)
    猜你喜欢
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    • 2021-11-02
    • 2022-11-14
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多