简单的汇编代码到Java的转换
我想把下面的代码转换成Java代码。我认为这是汇编代码,但不确定。我真正没有得到的部分是简单的汇编代码到Java的转换,java,Java,我想把下面的代码转换成Java代码。我认为这是汇编代码,但不确定。我真正没有得到的部分是y-=m
y-=m<3代码>
int dow(int y, int m, int d)
{
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
intdow(inty,intm,intd)
{
静态int t[]={0,3,2,5,0,3,5,1,4,6,2,4};
y-=m<3;
回报率(y+y/4-y/100+y/400+t[m-1]+d)%7;
}
布尔值m<3
将计算为0
或1
。那么操作y-=
就更有意义了
在java中,它看起来更像:
y-=(m这是C代码,我相信这一点
static final int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4,
6, 2, 4 };
static int dow(int y, int m, int d) {
if (m < 3) {
y--;
}
return (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;
}
static final int t[]={0,3,2,5,0,3,5,1,4,
6, 2, 4 };
静态整数道指(整数y、整数m、整数d){
if(m<3){
y--;
}
回报率(y+y/4-y/100+y/400+t[m-1]+d)%7;
}
因为y-=m<3;
将计算为y-=1;
如果m您在Java中编写的代码是
public static void main(String[] args){
int calculated_value = dow(2014, 7, 31);
}
public static int dow(int y, int m, int d){
int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= (m < 3 ? 1 : 0);
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
publicstaticvoidmain(字符串[]args){
int计算值=道指(2014,7,31);
}
公共静态指数指数(指数y、指数m、指数d){
int t[]={0,3,2,5,0,3,5,1,4,6,2,4};
y-=(m<3?1:0);
回报率(y+y/4-y/100+y/400+t[m-1]+d)%7;
}
我看到您在其他答案中已经有了一些很好的代码文字翻译,但您真正想写的是以下内容,这些内容清晰易读,并且依赖于可信的库:
public static int dow(int y, int m, int d)
{
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, y);
c.set(Calendar.MONTH, m - 1);
c.set(Calendar.DAY_OF_MONTH, d);
return (c.get(Calendar.DAY_OF_WEEK) - 1);
}
最后的-1
只是将Java使用的数字星期日表示法(其中星期日=1,星期六=7)映射到与原始代码相同的映射。与m-1
类似。下面是我对Java的转换:
packagedatecalculator;
导入javax.swing.*;
公共类日期计算器{
公共静态整数计算日期(整数y、整数m、整数d){
//坂本智彦法
int t[]={0,3,2,5,0,3,5,1,4,6,2,4};
y-=(m<3?1:0);
回报率(y+y/4-y/100+y/400+t[m-1]+d)%7;
}
公共静态void main(字符串[]args){
//接收输入
int y=Integer.parseInt(JOptionPane.showInputDialog(“输入年份”);
int m=Integer.parseInt(JOptionPane.showInputDialog(“输入月份”);
intd=Integer.parseInt(JOptionPane.showInputDialog(“输入日期”));
//调用坂本智彦的方法
int-answer=计算的日期(y,m,d);
//输出
if(answer==0){JOptionPane.showMessageDialog(null,“Sunday”);}
if(answer==1){JOptionPane.showMessageDialog(null,“星期一”);}
if(answer==2){JOptionPane.showMessageDialog(null,“星期二”);}
if(answer==3){JOptionPane.showMessageDialog(null,“星期三”);}
if(answer==4){JOptionPane.showMessageDialog(null,“星期四”);}
if(answer==5){JOptionPane.showMessageDialog(null,“星期五”);}
if(answer==6){JOptionPane.showMessageDialog(null,“星期六”);}
}
}
package datecalculator;
import javax.swing.*;
public class DateCalculator {
public static int calculateDay(int y, int m, int d){
//Tomohiko Sakamoto's method
int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= (m < 3 ? 1 : 0);
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
public static void main(String[] args){
//Receiving input
int y = Integer.parseInt(JOptionPane.showInputDialog("Enter the year"));
int m = Integer.parseInt(JOptionPane.showInputDialog("Enter the month"));
int d = Integer.parseInt(JOptionPane.showInputDialog("Enter the day"));
//Calling Tomohiko Sakamoto's method
int answer = calculateDay(y, m, d);
//Output
if (answer == 0){JOptionPane.showMessageDialog(null, "Sunday");}
if (answer == 1){JOptionPane.showMessageDialog(null, "Monday");}
if (answer == 2){JOptionPane.showMessageDialog(null, "Tuesday");}
if (answer == 3){JOptionPane.showMessageDialog(null, "Wednesday");}
if (answer == 4){JOptionPane.showMessageDialog(null, "Thursday");}
if (answer == 5){JOptionPane.showMessageDialog(null, "Friday");}
if (answer == 6){JOptionPane.showMessageDialog(null, "Saturday");}
}
}