【问题标题】:Adding Time Program Compiler Error: cannot find symbol [duplicate]添加时间程序编译器错误:找不到符号[重复]
【发布时间】:2017-11-19 23:34:30
【问题描述】:

我的编程课被指示制作一个程序,该程序会增加时间并将其转换为正确的分钟和秒数(不大于 59。)

import java.util.Scanner;

public class AddingTime {

    public static void main (String[] args) {

        Scanner kbReader = new Scanner(System.in);

        System.out.println("Welcome to AddingTime!");

        // First Addend

        System.out.println("Enter the days amount for the first addend (if none print 0): ");

        int firstAddendDays = kbReader.nextInt();

        System.out.println("Enter the hours amount for the first addend (in none print 0): ");

        int firstAddendHours = kbReader.nextInt();

        System.out.println("Enter the minutes amount for the first addend (if none print 0): ");

        int firstAddendMinutes = kbReader.nextInt();

        System.out.println("Enter the seconds amount for the first addend (if none print 0): ");

        int firstAddendSeconds = kbReader.nextInt();

        // Second Addend

        System.out.println("Enter the days amount for the second addend (if none print 0): ");

        int secondAddendDays = kbReader.nextInt();

        System.out.println("Enter the hours amount for the second addend (in none print 0): ");

        int secondAddendHours = kbReader.nextInt();

        System.out.println("Enter the minutes amount for the second addend (if none print 0): ");

        int secondAddendMinutes = kbReader.nextInt();

        System.out.println("Enter the seconds amount for the second addend (if none print 0): ");

        int secondAddendSeconds = kbReader.nextInt();

        int totalSeconds = firstAddendSeconds + secondAddendSeconds;

        if (totalSeconds >= 60) {

            // Total Seconds if totalSeconds is larger than 60

            totalSeconds = totalSeconds % 60;

            // Extra minutes from totalSeconds in a decimal form

            double minutesFromSeconds = totalSeconds / 60;

            // Changing the above into a integer form of minutes 

            int intMinutesFromSeconds = (int)minutesFromSeconds;
        }

        int totalMinutes = firstAddendMinutes + secondAddendMinutes + intMinutesFromSeconds;

        if (totalMinutes >= 60) {

            // Total minutes if totalMinutes is larger than 60

            totalMinutes = totalMinutes % 60;

            // Extra hours from totalMinutes in a decimal form

            double hoursFromMinutes = totalMinutes / 60;

            // Changing the above into an integer form of hours

            int intHoursFromMinutes = (int)hoursFromMinutes;
        }

        int totalHours = firstAddendHours + secondAddendHours + intHoursFromMinutes;

        if (totalHours >= 24) {

            // Total hours if totalHours is larger than 24

            totalHours = totalHours % 24;

            // Extra days from totalHours in a decimal form

            double daysFromHours = totalMinutes / 24;

            // Changing the above into an integer form of days

            int intDaysFromHours = (int)daysFromHours;
        }

        int totalDays = firstAddendDays + secondAddendDays + intDaysFromHours;

        System.out.println("Your total calculated time is " + totalDays + "days" + totalHours + "hours" + totalMinutes + "minutes" + totalSeconds + "seconds" );
    }
}

当我编译这段代码时,它告诉我在变量 intMinutesFromSeconds、intHoursFromSeconds 和 intDaysFromHours 上有一个找不到符号错误。为什么?

【问题讨论】:

    标签: java time


    【解决方案1】:

    这可能是范围的问题(括号内声明的内容在括号关闭后可能不可用)。不知道 Java 在做什么。

    if (totalSeconds >= 60) {
        int intMinutesFromSeconds = (int) minutesFromSeconds;
    }
    
    int totalMinutes = intMinutesFromSeconds + ...
    

    您可以使用以下方法修复它:

    int intMinutesFromSeconds = 0;
    if (totalSeconds >= 60) {
        intMinutesFromSeconds = (int) minutesFromSeconds;
    }
    
    int totalMinutes = intMinutesFromSeconds + ...
    

    【讨论】:

    • 它仍然给我变量 intMinutesFromSeconds 可能没有被初始化的错误
    • 现在可以使用了!谢谢!
    猜你喜欢
    • 2013-01-03
    • 2013-03-08
    • 1970-01-01
    • 2012-08-27
    • 2014-07-01
    • 1970-01-01
    • 2018-04-23
    • 2013-09-11
    • 1970-01-01
    相关资源
    最近更新 更多