Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的复活节计算器程序中的问题在哪里?_Java - Fatal编程技术网

Java 我的复活节计算器程序中的问题在哪里?

Java 我的复活节计算器程序中的问题在哪里?,java,Java,在我的复活节计算器程序中找不到计算问题。如果输入为2019,则月输出为4,而由于某种原因,日为-2。4月是4月,这是正确的,但这一天是错误的。如何提高代码的效率和解决方案 import java.util.*; import java.lang.Math; class Main { public static void main(String[] args) { Scanner userInput = new Scanner(System.in); Sy

在我的复活节计算器程序中找不到计算问题。如果输入为2019,则月输出为4,而由于某种原因,日为-2。4月是4月,这是正确的,但这一天是错误的。如何提高代码的效率和解决方案

    import java.util.*;
    import java.lang.Math;

class Main {
  public static void main(String[] args) {
    Scanner userInput = new Scanner(System.in);

    System.out.println("\nWelcome to the Easter Calculator. Please enter the current year below.");
    double y = userInput.nextInt();

    double p = y/100;

    double q = y - (19*(y/19));

    double r = (p-17)/25;

    double s = p - (p/4) - ((p-r)/3) + (19*q) + 15;

    s = s - (30*(s/30));

    s = s - ((s/28)*(1-((s/28)*(29/(2+1))*((21-q)/11))));

    double t = y + (y/4) + s + 2 - p + (p/4);

    t = t - (7*(t/7));

    double u = s - t;

    double m = 3 + ((u+40)/44);

    double d = u + 28 - (31*(m/4));

    System.out.println("Year = "+Math.round(y));
    System.out.println("Month = "+Math.round(m));
    System.out.println("Day = "+Math.round(d));
  }
}

s
计算的第三行

s = s - ((s / 28) * (1 - ((s / 28) * (29 / (2 + 1)) * ((21 - q) / 11))));
你有短语
(29/(2+1)
,它本身令人怀疑,与你所附的注释不符。应该有
(29/(s+1)

类似的结构

y-(19*(y/19))

t-(7*(t/7))

s-(30*(s/30))

将始终产生实用的
0
始终以双精度计算。 括号中应该有一个整数除法

你引用的那一页上有注释吗

如果我们在这里假设,我们计算一种按比例计算的运营利润率,并像这样使用整数除法

y-(19*((int)y)/19))
y-(19*(int)(y/19))
(提供较小的增量)


在所有这些地方,我们将得到2019年4月17日。看起来数据有效,但这一年的复活节日期无效(实际上不确定)。

结果表明,解决方案是将所有变量类型从双精度转换为整数。感谢您的帮助

import java.util.*;
import java.lang.Math;

class Main {
  public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);

System.out.println("\nWelcome to the Easter Calculator. Please enter the current year below.");
int y = userInput.nextInt();

int p = y/100;

int q = y - (19*(y/19));

int r = (p-17)/25;

int s = p - (p/4) - ((p-r)/3) + (19*q) + 15;

s = s - (30*(s/30));

s = s - ((s/28)*1-((s/28)*(29/(s+1))*((21-q)/11)));

int t = y + (y/4) + s + 2 - p + (p/4);

t = t - (7*(t/7));

int u = s - t;

int m = 3 + ((u+40)/44);

int d = u + 28 - (31*(m/4));

System.out.println("Year = "+Math.round(y));
System.out.println("Month = "+Math.round(m));
System.out.println("Day = "+Math.round(d));
  }
}

s=s-((s/28)*(1-((s/28)*(29/(2+1))*((21-q)/11));这里是错误……应该是(29/(s+1))而不是(29/(2+1))。为了验证,请检查您所附的图像。它包含正确的公式谢谢您的反馈,我尝试了此方法,但不幸的是没有任何区别,输出仍然是:月=4,天=-2如果输入为2019,这有帮助吗?如果目的是将此设置为整数除法,则将所有变量从double更改为int可能会失败ice.Int-variabled解决方案也不能得到有效的答案。我的观点是,我们必须更多地了解算法,而不仅仅是一个算术脚本。编程部分还可以,但计算逻辑还不清楚。这是作者的调查触发点。感谢反馈,我实际上已经通过更改所有变量类型从double到int。出于某种原因,整数可以工作,即使在整个计算过程中,我认为整数会被转换为double。谢谢你的帮助!