添加时间程序编译器错误:找不到符号

时间:2017-11-19 23:34:30

标签: java time

我的编程课程被指示制作一个程序,可以增加时间并将其转换为正确的分钟和秒数(不超过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上找不到符号错误。为什么呢?

1 个答案:

答案 0 :(得分:0)

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

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

int totalMinutes = intMinutesFromSeconds + ...

您可以使用以下内容修复它:

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

int totalMinutes = intMinutesFromSeconds + ...