Java可以写游戏的,但是一般来说不建议用Java开发游戏,因为Java这个语言是面向对象的语言对代码进行了大量的封装来达到模块组件可复用的目的,这也导致了它相对于面向过程的C,C++这种更偏向于底层的语言来说运行速度是偏慢的,Java更偏向于做服务端方面的工作,做电商网站,公司企业级的后台系统,因为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 exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮
JLabel fractionLable=new JLabel("0"); //分数标签
JButton firstButton,secondButton; //
static boolean pressInformation=false; //判断是否有按钮被选中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标
int i,j,k,n;//消除方法控制
代码(code)是程序员用开发工具所支持的语言写出来的源文件,是一组由字符、符号或信号码元以离散形式表示信息的明确的规则体系.
虽然用于表示 A 到 Z 拉丁字母表字符的位模式在所有的代码页中都相同,但用于表示重音字符(如"é"和"á")的位模式在不同的代码页中却不同.如果在运行不同代码页的计算机间交换数据,必须将所有字符数据由发送计算机的代码页转换为接收计算机的代码页.如果源数据中的扩展字符在接收计算机的代码页中未定义,那么数据将丢失.
import java.util.*;
import java.io.*;
public class CaiShu{
public static void main(String[] args) throws IOException{
Random a=new Random();
int num=a.nextInt(100);
System.out.println("请输入一个100以内的整数:");
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String str=bf.readLine();
int shu=Integer.parseInt(str);
if (shunum)
System.out.println("输入的数大了,输小点的!");
else if (shunum)
System.out.println("输入的数小了,输大点的!");
else {
System.out.println("恭喜你,猜对了!");
System.out.println("你真是个天才!");
System.out.println("还将就,你过关了!");
System.out.println("但是你还......真笨!");
else
System.out.println("你和猪没有两样了!"); break;}
}
} }
为什么用AWT不用 swing?
算法思想很简单
是取胜原理
代码调试可用
自己用GUI搭个界面 二十分钟的事
import java.util.Scanner;
public class MatchGame {
private static Scanner scanner = new Scanner(System.in);;
private int total;
private Computer com;
private static int exit = 1;
public MatchGame(int from, int to, String level) {
if (from = to) {
throw new IllegalArgumentException();
total = (int)( Math.random() * (to - from)) + from;
com = new Computer(level);
public void start() {
System.out.println("The number of matches is " + total);
System.out.println("~Start~");
System.out.println("----------------------------------------");
while (true) {
int u = scanner.nextInt();
System.out.println("You entered " + u);
if (total - u = 0) {
endGame();
total = total - u;
System.out.println("Total : " + total);
int c = com.play(u, total);
System.out.println("Computer selected " + c + " matches~");
if (total - c = 0) {
exit = 0;
total = total - c;
}else if (u == 0) {
System.out.println("You entered Wrong number~");
} else {
restart();
public static void restart() {
MatchGame game;
System.out
int level = scanner.nextInt();
if (level == 1) {
game.start();
public static void endGame() {
if (exit == 0) {
System.out.println("YOU WIN!!!");
System.out.println("YOU LOSE!!!");
System.exit(exit);
public static class Computer {
private static String EASY = "EASY";
private static String NORMAL = "NORMAL";
private static String HARD = "HARD";
private static String LEVEL;
private int com;
public Computer(String level) {
LEVEL = level;
public int play(int user, int total) {
if (LEVEL.equals(EASY)) {
com = 1;
} else if (LEVEL.equals(NORMAL)) {
int i;
if (com == 0) {
return com;
public static void main(String[] args) {
以上就是土嘎嘎小编为大家整理的java代码编写小游戏相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!