「java猜生肖」java猜猜游戏
本篇文章给大家谈谈java猜生肖,以及java猜猜游戏对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、用java作那年出生属什么生肖属相
- 2、编写代码输入年份.计算出该年是12生肖中的哪一年,谁帮忙用java 语言编写一下,感激不尽
- 3、Java生肖查询
- 4、java判断生肖题 并输出其前后两年的生肖
- 5、从1991年至今用十二个生肖分别用java表示出来
用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 语言编写一下,感激不尽
自己写拉
例如
先使用bufferedreader inputstreamreader system.in读取键盘
然后string str = bufferedreader.readline();
int index = Integer.parseInt(str);
if*(index =1 | index = 4)
是什么什么 生肖!!! 等等
Java生肖查询
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.Serializable;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Year extends JFrame implements Serializable{
private static final long serialVersionUID=21L;
private JTextField jt;
private JButton jb;
private JLabel ab1,ab2;
private JPanel jp1;
Year(){
this.setTitle("生肖查询小工具");
this.setBounds(400,300,300,200);
this.setLayout(new GridLayout(3,1));
init();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
private void init() {
jp1=new JPanel();
jt=new JTextField(10);
jb=new JButton("查询:");
ab1=new JLabel();
ab2=new JLabel("输入:");
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=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 (year 1900) {
return "未知";
}
int start = 1900;
String[] years = new String[] { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
return years[(year - start) % years.length];
}
}
java判断生肖题 并输出其前后两年的生肖
package BaseExec;
import java.util.Scanner;
public class PrintAnimal {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);// 声明扫描仪变量
System.out.println("请输入年份");// 系统提示输入
int birth = input.nextInt() % 12;
PrintAnimal(birth);
PrintAnimal(birth + 1);
PrintAnimal(birth + 2);
}
public static void PrintAnimal(int year){
switch (year) {
// switch语句的判断条件只接受int,byte,char,short
case 0:
System.out.println("猴");
break;
case 1:
System.out.println("鸡");
break;
case 2:
System.out.println("狗");
break;
case 3:
System.out.println("猪");
break;
case 4:
System.out.println("鼠");
break;
case 5:
System.out.println("牛");
break;
case 6:
System.out.println("虎");
break;
case 7:
System.out.println("兔");
break;
case 8:
System.out.println("龙");
break;
case 9:
System.out.println("蛇");
break;
case 10:
System.out.println("马");
break;
case 11:
System.out.println("羊");
break;
default:
System.out.println("错误!请输入大于0的数");
}
}
}
从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猜生肖的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java猜猜游戏、java猜生肖的信息别忘了在本站进行查找喔。
发布于:2022-12-17,除非注明,否则均为
原创文章,转载请注明出处。