Java有趣程序代码,Java有趣代码
求java经典小程序代码
代码如下:
我们提供的服务有:做网站、网站制作、微信公众号开发、网站优化、网站认证、易门ssl等。为成百上千企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的易门网站制作公司
public class HelloWorld {
public static void main(String []args) {
int a = 3, b = 7 ;
System.out.println("Hello World!");
}
public static int f(int a, int b){
return a*a + a*b + b*b;
}
}
结果如下:
给段最简单的java代码 让我新手看一下
最简单的java代码肯定就是这个了,如下:
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print("Hello world");
}
}
“hello world”就是应该是所有学java的新手看的第一个代码了。如果是零基础的新手朋友们可以来我们的java实验班试听,有免费的试听课程帮助学习java必备基础知识,有助教老师为零基础的人提供个人学习方案,学习完成后有考评团进行专业测试,帮助测评学员是否适合继续学习java,15天内免费帮助来报名体验实验班的新手快速入门java,更好的学习java!
用JAVA编写一个小程序,不限类型好玩点的。
控制台小游戏 class YZDEL
{
public static void main(String[] args)
{
java.util.Random Shiji = new java.util.Random();
java.util.Scanner Shuru = new java.util.Scanner(System.in);
java.util.Scanner Nandu = new java.util.Scanner(System.in);
int YingxHP = 20;
int YingxMP = 0;
int MogHP = 20;
int MogMP = 0;
int MogDo = 0;
int fangyu = 0;
System.out.println("┏━━━━╮┏━━┣┣┓ ╮ ┓┏━┳━┳┓┓━┓ ");
System.out.println("┏┣━━━┓┏━━┣┣┓ ┃┏ ┃ ┃┓┃ ╯ ");
System.out.println("┣┣━━━┫╭━━┻╯╮ ┃╰┓┃ ┃╯┏━┣━━┓");
System.out.println("┣┣━━━┫┏━━━━┓╭━━┣━┛┗━┻━┻┛┃┃");
System.out.println("┏┣━━━┓┣━━━━┫ ┃┃┃┗╮╮┃┃┣━╯");
System.out.println(" ┛┗╯┗━━━━╯┗╯╰╰━━┛┛┗━╯╰━┛");
System.out.println();
System.out.println(" 作者:天狐空幻");
System.out.println();
System.out.println("说明:勇者的操作方式为以下所示:");
System.out.println(" 使用攻击需消耗1MP 伤害1HP");
System.out.println(" 使用蓄力可增加1MP 伤害0HP");
System.out.println(" 使用躲闪需消耗0MP 伤害0HP 躲避攻击");
System.out.println(" 使用重击需消耗3MP 伤害2HP 防御无效");
System.out.println(" 恶龙攻击力高, 注意防御是取胜的关键");
System.out.println(" 恶龙MP达到4时可能会放出火焰无法躲避");
System.out.println(" 准备说明完毕,那么让我们来挑战恶龙吧!");
System.out.println("==================================");
//难度选择
System.out.println("请选择难度");
System.out.println("1.娱乐 2.挑战 3.噩梦");
int ND = Nandu.nextInt();
System.out.println("==================================");
while(true)
{
//HP,MP的显示
System.out.print("勇者: ");
System.out.print(" HP ");
for(int x=YingxHP;x0;x--) System.out.print("*");
System.out.print(" "+YingxHP);
System.out.println();
System.out.print(" ");
System.out.print(" MP ");
for(int x=YingxMP;x0;x--) System.out.print("*");
System.out.print(" "+YingxMP);
System.out.println();
System.out.print("恶龙: ");
System.out.print(" HP ");
for(int y=MogHP;y0;y--) System.out.print("*");
System.out.print(" "+MogHP);
System.out.println();
System.out.print(" ");
System.out.print(" MP ");
for(int y=MogMP;y0;y--) System.out.print("*");
System.out.print(" "+MogMP);
System.out.println();
System.out.println("==================================");
//胜利判定
if(YingxHP1) {System.out.println();System.out.println("勇者HP为0! 不..不可能..我怎么会..勇者倒下了。再接再厉吧!~");System.out.println();break;}
if(MogHP1) {System.out.println();System.out.println("恶龙HP为0! 恶龙绝望的哀鸣中倒了下去。勇者胜利了。恭喜你挑战成功!!");System.out.println();break;}
//角色输入判定
System.out.println("你要做什么:");
System.out.println("1.攻击 2.蓄力");
System.out.println("3.躲闪 4.重击");
System.out.println("____________________");
int Do = Shuru.nextInt();
//敌人输入判定
//娱乐难度
if(ND==1){
for(int i=1;i3;i++) MogDo = Shiji.nextInt(4);}
//挑战难度
if(ND==2){
if(YingxMP==0MogMP==0) {MogDo = 0;}
else if(MogMP==0){for(int i=1;i3;i++) MogDo = Shiji.nextInt(2);}
else if(YingxMP2MogMP2) MogDo = 2;
else if(MogMP4) {for(int i=1;i4;i++) MogDo = Shiji.nextInt(3);}
else MogDo = 3;}
//噩梦难度
if(ND==3){
if(Do==1) MogDo=1;
if(Do==2MogMP0) MogDo=2;
if(Do==2MogMP==0) MogDo=0;
if(Do==3MogMP4) MogDo=0;
if(Do==3MogMP=4) MogDo=3;}//变态判定。。
//战斗分析
//防御
if(Do==3) {fangyu=1;System.out.println("你灵巧的躲避攻击!");}
if(MogDo==1) {fangyu=1;System.out.println("恶龙进行防御!");}
//角色判定
if(Do==1YingxMP==0) {System.out.println("MP不足!");}
if(Do==1YingxMP0) {
if(fangyu==0) {MogHP=MogHP-1;YingxMP=YingxMP-1;System.out.println("你发动攻击!");}
if(fangyu==1) {YingxMP=YingxMP-1;System.out.println("你的攻击被格挡!");}}
if(Do==2) {YingxMP=YingxMP+1;System.out.println("你进行蓄力!");}
if(Do==4YingxMP3) {System.out.println("MP不足!");}
if(Do==4YingxMP2) {MogHP=MogHP-2;YingxMP=YingxMP-3;System.out.println("你发动重击!");}
if(Do 4) System.out.println("你不知所措...");
//敌人判定
if(MogDo==2MogMP==0) {System.out.println("恶龙在发呆!");}
if(MogDo==2MogMP0) {
if(fangyu==0) {YingxHP=YingxHP-2;MogMP=MogMP-1;System.out.println("恶龙发动攻击!");}
if(fangyu==1) {MogMP=MogMP-1;System.out.println("恶龙的攻击被躲开了!");}}
if(MogDo==0) {MogMP=MogMP+1;System.out.println("恶龙进行蓄力!");}
if(MogDo==3MogMP4) {System.out.println("恶龙在发呆!");}
if(MogDo==3MogMP3) {YingxHP=YingxHP-4;MogMP=MogMP-4;System.out.println("恶龙发动火焰吐吸!躲避不能!");}
//结束
fangyu = 0;
MogDo = 0;
System.out.println("____________________");
System.out.println();
System.out.println("=================================="); }
}
}
Java编程(写出程序代码)
写了一个代码,代码如下,可以进行参考
public class sum {
public static void main(String[] args) {
//创建一个Scanner的对象input
Scanner input = new Scanner(System.in);
//提示用户输入数据
System.out.print("请输入一个整数");
//将输入的值赋给n
int n = input.nextInt();
//定义变量接收计算后的和
int sum = 0;
//利用循环进行求和
for (int i = 0; i = n; i++) {
sum+=i;
}
//输出最后的和
System.out.println("从0一直到"+n+"的所有整数的和是:"+sum);
}
}
有没有关于JAVA的一些简单又有趣的程序
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Date;
public class Game extends JFrame {
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(478, 361, 164, 51));
jButton1.setText("重新开始");
jButton1.setVisible(false);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jButton1.setVisible(false);
jLabel.setVisible(false);
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
reset();
}
});
}
return jButton1;
}
public static void main(String[] args) {
Game game = new Game();
game.start();
game.reset();
game.gogo();
}
public void reset() {
kup = false;
kdown = false;
kleft = false;
kright = false;
int chushihua = 0;
while (chushihua zidanshu) {
((JButton) buttonal.get(chushihua)).setBounds(new Rectangle(-50,
-50, 10, 10));
chushihua++;
}
gamexunhuan = true;
jButton.setIcon(new ImageIcon(fileLoc));
jButton.setLocation(320, 320);
p = jButton.getLocation();
x=p.getX();
y=p.getY();
firsttime=new Date().getTime();
}
public void start() {
int chushihua = 0;
while (chushihua zidanshu) {
JButton jb = new JButton();
jb.setBounds(new Rectangle(-50, -50, 10, 10));
jb.setEnabled(false);
Threads ths = new Threads(jb);
Thread th = new Thread(ths);
buttonal.add(jb);
threadal.add(th);
chushihua++;
}
Game.Move move = new Move();
Thread tm = new Thread(move);
tm.start();
}
public void gogo() {
int chushihua = 0;
while (chushihua zidanshu) {
((Thread) threadal.get(chushihua)).start();
chushihua++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
// private static Game game;
private long firsttime;
private long lasttime;
private static final long serialVersionUID = 1L;
private JPanel jPanel = null;
private JButton jButton = null;
private boolean kup ;
private boolean kdown ;
private boolean kleft ;
private boolean kright ;
// 定义玩家的行走步伐,数值越大,移动速度越快 private int step = 3;
Point p; // @jve:decl-index=0:
double x = 0.0;
double y = 0.0;
// 定义了子弹的个数 int zidanshu = 70;
// 定义子弹初始值,这个是不变的
// int chushihua = 0;
// 定义控制子弹行走的循环false就不走了
private boolean gamexunhuan = true;
private JLabel jLabel = null;
private JButton jButton1 = null;
private ArrayList buttonal = new ArrayList();
private ArrayList threadal = new ArrayList();
URLClassLoader urlLoader = (URLClassLoader)this.getClass().getClassLoader();
URL fileLoc = urlLoader.findResource("MyGameIcons/gwl1.gif"); // @jve:decl-index=0:
URL fileLoc1 = urlLoader.findResource("MyGameIcons/gwls1.gif");
/**
* This is the default constructor
*/
public Game() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(700, 700);
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(1);
}
});
this.setResizable(false);
this.setContentPane(getJPanel());
this.setTitle("范传奇的小游戏!(模拟撑过30秒的小DEMO)");
this.setVisible(true);
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(42, -33, 595, 308));
jLabel.setFont(new Font("Dialog", Font.BOLD, 24));
jLabel.setForeground(new Color(250, 2, 2));
jLabel.setEnabled(true);
jLabel.setVisible(false);
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.add(getJButton(), null);
jPanel.setForeground(new Color(1, 1, 1));
jPanel.setBackground(new Color(1, 1, 1));
jPanel.setVisible(true);
jPanel.add(jLabel, null);
jPanel.add(getJButton1(), null);
}
return jPanel;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
class Move implements Runnable {
public void run() {
while(true){
while (gamexunhuan) {
p = jButton.getLocation();
if (kup) {
if (kleft) {
x = p.getX();
y = p.getY();
if (x 0 y 0) {
jButton.setLocation((int) x - step, (int) y
- step);
}
} else if (kright) {
x = p.getX();
y = p.getY();
if (x + 40 700 y 0) {
jButton.setLocation((int) x + step, (int) y
- step);
}
} else {
x = p.getX();
y = p.getY();
if (y 0) {
jButton.setLocation((int) x, (int) y - step);
}
}
}
if (kdown) {
if (kleft) {
x = p.getX();
y = p.getY();
if (y + 60 700 x 0) {
jButton.setLocation((int) x - step, (int) y
+ step);
}
} else if (kright) {
x = p.getX();
y = p.getY();
if (x + 40 700 y + 60 700) {
jButton.setLocation((int) x + step, (int) y
+ step);
}
} else {
x = p.getX();
y = p.getY();
if (y + 60 700) {
jButton.setLocation((int) x, (int) y + step);
}
}
}
if (kleft) {
if (kup) {
x = p.getX();
y = p.getY();
if (x 0 y 0) {
jButton.setLocation((int) x - step, (int) y
- step);
}
} else if (kdown) {
x = p.getX();
y = p.getY();
if (y + 60 700 x 0) {
jButton.setLocation((int) x - step, (int) y
+ step);
}
} else {
x = p.getX();
y = p.getY();
if (x 0) {
jButton.setLocation((int) x - step, (int) y);
}
}
}
if (kright) {
if (kup) {
x = p.getX();
y = p.getY();
if (x + 40 700 y 0) {
jButton.setLocation((int) x + step, (int) y
- step);
}
} else if (kdown) {
x = p.getX();
y = p.getY();
if (x + 40 700 y + 60 700) {
jButton.setLocation((int) x + step, (int) y
+ step);
}
} else {
x = p.getX();
y = p.getY();
if (x + 40 700) {
jButton.setLocation((int) x + step, (int) y);
}
}
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(320, 320, 30, 30));
jButton.setBackground(new Color(1, 1, 1));
p = jButton.getLocation();
x = p.getX();
y = p.getY();
jButton.setIcon(new ImageIcon(fileLoc));
jButton.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent e) {
if(e.getKeyCode()==10){
if(!gamexunhuan){
jButton1.setVisible(false);
jLabel.setVisible(false);
reset();
}
}
if (e.getKeyCode() == 37) {
kleft = false;
}
if (e.getKeyCode() == 38) {
kup = false;
}
if (e.getKeyCode() == 39) {
kright = false;
}
if (e.getKeyCode() == 40) {
kdown = false;
}
}
public void keyPressed(java.awt.event.KeyEvent e) {
if (e.getKeyCode() == 37) {
kleft = true;
}
if (e.getKeyCode() == 38) {
kup = true;
}
// 触发按右键
if (e.getKeyCode() == 39) {
kright = true;
}
if (e.getKeyCode() == 40) {
kdown = true;
}
}
});
}
return jButton;
}
class Threads implements Runnable {
public Threads(JButton jjb) {
jb = jjb;
}
JButton jb = null;
private boolean first = true;
public void run() {
while (gamexunhuan) {
go();
}
}
public void go() {
int zzx = 0;
int zzy = 0;
int zx = 0;
int zy = 0;
while (true) {
if(gamexunhuan){
int fangxiang = (int) (Math.random() * 4 + 1);
// 四个if随即从四个边发射子弹
if (fangxiang == 1) {
zx = 0;
zy = (int) (Math.random() * 701);
}
if (fangxiang == 2) {
zx = (int) (Math.random() * 701);
zy = 0;
}
if (fangxiang == 3) {
zx = 700;
zy = (int) (Math.random() * 701);
}
if (fangxiang == 4) {
zx = (int) (Math.random() * 701);
zy = 700;
}
// 初始化子弹,有了就不在加了
if (first) {
jPanel.add(jb, null);
first = false;
}
jb.setBounds(new Rectangle(zx, zy, 10, 10));
// 定义子弹与物体之间的步长
zzx = (int) (((x + 15) - zx) / 30);
zzy = (int) (((y + 15) - zy) / 30);
}
while (gamexunhuan) {
try {
zx += zzx;
zy += zzy;
jb.setLocation(zx, zy);
if (zx + 5 x zx + 5 x + 30 zy + 5 y
zy + 5 y + 30) {
jButton.setIcon(new ImageIcon(fileLoc1));
gamexunhuan = false;
first = true;
jButton1.setVisible(true);
jLabel.setVisible(true);
lasttime = new Date().getTime();
Date gametime = new Date(lasttime-firsttime);
int min =0;
int sec =0;
min = gametime.getMinutes();
sec = gametime.getSeconds();
String endtime = "";
if(min!=0){
endtime=min + "分 " + sec + "秒";
}else{
endtime=sec + "秒";
}
jLabel.setText(" GAME OVER!!! \n用时:" + endtime);
break;
}
// 超出边线停止循环
if (zx 700 | zy 700 | zx 0 | zy 0) {
break;
}
Thread.sleep(60);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
}
} // @jve:decl-index=0:visual-constraint="10,10"
这是一个以前写过的“是男人就撑过30秒的小游戏源码”
如果想要执行程序,麻烦留个邮箱。
急求大神写个java程序代码,在线等
import java.util.Scanner;public class CORE_C02_011Main { public static void main(String[] args) { System.out.println("请输入第一个整数:"); Scanner s1 = new Scanner(System.in); int num1 = s1.nextInt(); System.out.println("请输入第二个整数:"); int num2 = s1.nextInt(); if(num1=num2){ System.out.println(num1); System.out.println(num2); }else{ System.out.println(num2); System.out.println(num1); } }}
文章题目:Java有趣程序代码,Java有趣代码
文章链接:http://ybzwz.com/article/hsioei.html