除以两个长变量然后减去零

时间:2018-03-07 07:28:33

标签: java class long-integer

逗人,

我的Java代码中有一个小问题。代码应该划分两个长数字(这很好)。当从结果中减去时,它给出零,而它应该给出另一个结果!

代码是关于在给定的分钟数内获得年数和天数。

import java.util.Scanner;

public class TestMinsToYearsDays {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MinsToYearsDays minsToYearsDays = new MinsToYearsDays();
		
		Scanner sc = new Scanner(System.in);  
		System.out.print("Enter the number of Minutes: ");
		minsToYearsDays.miniute = sc.nextLong();
		
		long years;
		years = minsToYearsDays.miniute / 525600L;
		
		long remaining;
		remaining = ((  years-(minsToYearsDays.miniute / 525600L) ) * 365L);
		
		System.out.println("\n Minutes are approximatly " + years + " years and " + remaining + " days " + minsToYearsDays.miniute);
	}

}

1 个答案:

答案 0 :(得分:1)

years = minsToYearsDays.miniute / 525600L;

表示

years-(minsToYearsDays.miniute / 525600L) == 0

因此

remaining = ((years-(minsToYearsDays.miniute / 525600L)) * 365L);

是0。

你的公式错了。如果你想计算剩余的分钟数(扣除整年后),它应该是:

remainingMinutes = minsToYearsDays.miniute - years * 525600L;

如果要计算剩余整天的数量,则应该是:

remainingDays = (minsToYearsDays.miniute - years * 525600L) / 1440;