RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
一些有趣的代码java 一些有趣的代码制作

用Java可以做哪些有趣的事情?

像Industrial Light and Magic这样的公司,就依赖Java来满足各种编程需求。事实上,你经常可以在ILM上发现几乎任何有开发需要的工作。在ILM工作有意思的地方在于,你可以把应用程序一起放到在大屏幕上查看结果。目前,ILM使用Java和Python来处理诸如测序动画场景等任务。

创新互联建站于2013年成立,先为叠彩等服务建站,叠彩等地企业,进行企业商务咨询服务。为叠彩企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

一段有意思的java代码请大家解释~

原理很简单,因为在java中,int是占4个字节大小,double占8个字节的大小,当你把某变量乘以2的时候,在计算机里面的处理方式是左移一位。当使用浮点数进行乘法运算时,若结果很大,会以科学计数法表示。

下面具体分析:

1、表达式0x7FFFFFFF == (int)(0x7FFFFFFF * 2.0)

0x7FFFFFFF 已经占了4个字节,也就是int型的最大范围,以二进制表示出来为01111111 11111111 11111111 11111111

0X7FFFFFFF*2.0 计算出来的结果为double型,那么结果将会以科学计数法来表示,也就是4.294967294E9, 以二进制表示为0 11111111 11111111 11111111 11111110,以16进制表示为0xFF FF FF FE,注意,这里的计算结果并没有超出double的范围8字节。

(int)(0x7FFFFFFF * 2.0) 在上面已经看到0x7FFFFFFFF的二进制表示为01111111 11111111 11111111 11111111乘以2就表示左移一位,结果为0 11111111 11111111 11111111 11111110 (注意,这个数并未超出8字节的范围)然后再把结果强制转换为int型,也就是从最高位开始向下取4个字节,因此最后一位的0被丢弃(取double的最大值,因此丢弃最低位),最后结果以二进制表示为01111111 11111111 11111111 11111111,以16进制表示为0x7F FF FF FF,可以看到与0x7FFFFFFFF的相同,因此第一个表达式0x7FFFFFFF == (int)(0x7FFFFFFF * 2.0)反回true。

2、表达式(int)(0x7FFFFFFF * 2.0) == (int)(0x7FFFFFFF * 2)

(int)(0x7FFFFFFF * 2.0)这部分的结果在上面介绍过了,这里就不用介绍了,结果还是为0x7F FF FF FF。

(int)(0x7FFFFFFF * 2) 其中0x7FFFFFFF*2表示把0x7FFFFFFF左移一位,其二进制结果为0 11111111 11111111 11111111 11111110,因为最后为int型,计算结果超出4个字节,因此最高位的0被丢弃(int型的计算是抛弃最高位),结果为11111111 11111111 11111111 11111110,以16进制表示为0xFF FF FF FE与0x7FFFFFFF不相同,因此结果为false。

要注意,在计算机中数值是以补码的形式表示的(包括以上的计算结果全都是以补码表示的),补码知识不作介绍,这里只要知道,正数的被码就为原来的正数,而负数的补码为符号位不变,其余各位按位取反再加1。因此0xFF FF FF FE除符号位不变(在java中int型最高位为符号位),其余各位取反再加1,结果为10000000 00000000 00000000 00000010最后结果为-2,以16进制表示为0x80 00 00 02,因此使用print输出该数,则为-2,并不为0xFF FF FF FE的十进制数值。

3、表达式0x7FFFFF * 2.0== (int)(0x7FFFFF * 2)

因为两个数字计算的结果都没有出现超出int型的4个字节的情况,因此计算结果相同,这个就不介绍了,相信你应该明白了。

好了,现在相信你应该明白了

求一个简单又有趣的JAVA小游戏代码

具体如下:

连连看的小源码

package Lianliankan;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class lianliankan implements ActionListener

{

JFrame mainFrame; //主面板

Container thisContainer;

JPanel centerPanel,southPanel,northPanel; //子面板

JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组

JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮

JLabel fractionLable=new JLabel("0"); //分数标签

JButton firstButton,secondButton; //

分别记录两次62616964757a686964616fe59b9ee7ad9431333335326239被选中的按钮

int grid[][] = new int[8][7];//储存游戏按钮位置

static boolean pressInformation=false; //判断是否有按钮被选中

int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标

int i,j,k,n;//消除方法控制

代码(code)是程序员用开发工具所支持的语言写出来的源文件,是一组由字符、符号或信号码元以离散形式表示信息的明确的规则体系。

对于字符和Unicode数据的位模式的定义,此模式代表特定字母、数字或符号(例如 0x20 代表一个空格,而 0x74 代表字符“t”)。一些数据类型每个字符使用一个字节;每个字节可以具有 256 个不同的位模式中的一个模式。

在计算机中,字符由不同的位模式(ON 或 OFF)表示。每个字节有 8 位,这 8 位可以有 256 种不同的 ON 和 OFF 组合模式。对于使用 1 个字节存储每个字符的程序,通过给每个位模式指派字符可表示最多 256 个不同的字符。2 个字节有 16 位,这 16 位可以有 65,536 种唯一的 ON 和 OFF 组合模式。使用 2 个字节表示每个字符的程序可表示最多 65,536 个字符。

单字节代码页是字符定义,这些字符映射到每个字节可能有的 256 种位模式中的每一种。代码页定义大小写字符、数字、符号以及 !、@、#、% 等特殊字符的位模式。每种欧洲语言(如德语和西班牙语)都有各自的单字节代码页。

虽然用于表示 A 到 Z 拉丁字母表字符的位模式在所有的代码页中都相同,但用于表示重音字符(如"é"和"á")的位模式在不同的代码页中却不同。如果在运行不同代码页的计算机间交换数据,必须将所有字符数据由发送计算机的代码页转换为接收计算机的代码页。如果源数据中的扩展字符在接收计算机的代码页中未定义,那么数据将丢失。

如果某个数据库为来自许多不同国家的客户端提供服务,则很难为该数据库选择这样一种代码页,使其包括所有客户端计算机所需的全部扩展字符。而且,在代码页间不停地转换需要花费大量的处理时间。

有没有关于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 一些有趣的代码制作
链接地址:http://sczitong.cn/article/hgpojs.html