java根据年份判断生肖 怎样确定年份的生肖( 二 )


ab1.setSize(50, 20);
ab1.setOpaque(true);
ab1.setForeground(Color.RED);
ab1.setBackground(Color.ORANGE);
ab1.setHorizontalAlignment(0);
ab1.setVerticalAlignment(0);
event();
jp1.add(ab2);
jp1.add(jt);
this.add(jp1);
this.add(ab1);
this.add(jb);
}
private void event() {
jt.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if(e.getKeyChar()48||e.getKeyChar()57) {
e.consume();
}
}
});
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String str=jt.getText();
String src=https://www.scwdwl.com/news/getYear(Integer.parseInt(str));
ab1.setText(str+"\t:"+src);
jt.setText(null);
jt.requestFocus();
}
});
}
public static void main(String[] args) {
new Year();
}
public String getYear(int year) {
if (year1900) {
return "未知";
}
int start = 1900;
String[] years = new String[] { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
return years[(year - start) % years.length];
}
}
用java作那年出生属什么生肖属相import java.util.Scanner;
public class Test {
public static void main(String[] args) {
String[] shengxiaos = { "鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴","鸡", "狗", "猪" };
String shengxiao;
int n;
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份:");
n = sc.nextInt();
int m = Math.abs(n - 2008) % 12;
if (n = 2008) {
shengxiao = shengxiaos[m];
} else {
if (m == 0) {
m = 12;
}
shengxiao = shengxiaos[12 - m];
}
System.out.println(shengxiao);
}
}
传统的12生肖是这样排列的:
鼠、牛、虎、免、龙、蛇、马、羊、猴、鸡、狗、猪
把它们放进一个数组里,找一个是鼠年的年份(如这里是2008年),要求的年份与鼠年的年份相除,其余数就是对应数组的生肖 。如2009 % 2008 = 1;就是在数组1里,就是牛;当然小于2008年的是另一种情况,不过已在程序中体现了 。很简单的 。
编写代码输入年份.计算出该年是12生肖中的哪一年,谁帮忙用java 语言编写一下,感激不尽自己写拉
例如
先使用bufferedreaderinputstreamreader system.in读取键盘
然后string str = bufferedreader.readline();
int index = Integer.parseInt(str);
if*(index =1 | index= 4)
是什么什么 生肖!!! 等等
从1991年至今用十二个生肖分别用java表示出来记住一个开始的生肖和一个年份对应,然后每12一个轮回
1991-羊 --- n=7
1992-猴 --- n=8
1993-鸡 --- n=9
1994-狗 --- n=10
1995-猪 --- n=11
1996-鼠 --- n=0
1997-牛 --- n=1
1998-虎 --- n=2
1999-兔 --- n=3
2000-龙 --- n=4
2001-蛇 --- n=5
2002-马 --- n=6
2003-羊 --- n=7
2004-猴 --- n=8
2005-鸡 --- n=9
2006-狗 --- n=10
2007-猪 --- n=11
2008-鼠 --- n=0
2009-牛 --- n=1
2010-虎 --- n=2
2011-兔 --- n=3
2012-龙 --- n=4
2013-蛇 --- n=5
2014-马 --- n=6
2015-羊 --- n=7
2016-猴 --- n=8
关于java根据年份判断生肖和怎样确定年份的生肖的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。