
正文
java记忆翻牌代码 java翻牌游戏
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
有没有java代码翻译软件?
没有翻译软件,但是能编写翻译程序。
java 代码翻译实例:
1.输入一个以’@’结束的字符串,从左至右翻译。若下一个字符是数字n(0≤n≤9),表示后一个字符重复n+1 次,不论后一个字符是否为数字;若下一个字符非数字,则表示自己。
2.翻译后,以3 个字符为一组输出,组与组之间用空格分开。
例如’A2B5E34FG0ZYWPQ59R@’,翻成’ABB_BEE_EEE_E44_44F_GZY_WPQ_999_999_R@ ’。
3.分析:首先直接遍历数组把字符串按要求进行翻译,然后将翻译后的字符串进行分组形成字符串数组,最后把字符串数组用下划线连接输出。
java翻译源代码:
import java.util.Scanner;
public class Main5{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
int length = s.length();
String result = "";
char[] str = new char[length]
for (int i = 0; i length; i++) {
str[i] = s.charAt(i);
}
result += str[0];
if (str[length - 1] != '@') {
System.out.println("输入有误!");
} else {
for (int index = 0; index length - 1;) {
if ('0' == str[index + 1] || '1' == str[index + 1] || '2' == str[index + 1] || '3' == str[index + 1]
|| '4' == str[index + 1] || '5' == str[index + 1] || '6' == str[index + 1]
|| '7' == str[index + 1] || '8' == str[index + 1] || '9' == str[index + 1]) {
for (int i = 0; i ((Integer.parseInt(str[index + 1]+"")) + 1); i++) {
result += str[index + 2];
}
index += 2;
} else {
result += str[index + 1];
index++;
}
}
}
System.out.println(getGroup(result));
}
//每3个分一组
public static String getGroup(String s){
String[] r;
if(s.length()%3 == 0){
r = new String[s.length()/3];
}else{
r = new String[s.length()/3+1];
}
String result = "";
int j = 0;
for(int i=0;is.length();){
if(i+3 = s.length()){
r[j]=s.substring(i, i+3);
j++;
i += 3;
}else{
r[j] = s.substring(i);
j++;
i += 3;
}
}
for(int i=0;ir.length-1;i++){
result += (r[i]+"_");
}
result += r[r.length-1];
return result;
}
}
相关问答
Q1: 要做一个记忆翻牌游戏 应该用Java还是Flash做比较好?
Flash是动漫设计,如果你想要华丽的话就使用Flash吧。
这个软件完成后还想加做一个手机版的需要学多久?
如果想转换成手机游戏,我建议你用JAVA
J2ME是专门开发手机游戏的。
开发手机游戏的速度是按个人技术和熟练程度
Q2: 求java小游戏源代码
表1. CheckerDrag.java
// CheckerDrag.javaimport java.awt.*;import java.awt.event.*;public class CheckerDrag extends java.applet.Applet{ // Dimension of checkerboard square. // 棋盘上每个小方格的尺寸 final static int SQUAREDIM = 40; // Dimension of checkerboard -- includes black outline. // 棋盘的尺寸 – 包括黑色的轮廓线 final static int BOARDDIM = 8 * SQUAREDIM + 2; // Dimension of checker -- 3/4 the dimension of a square. // 棋子的尺寸 – 方格尺寸的3/4 final static int CHECKERDIM = 3 * SQUAREDIM / 4; // Square colors are dark green or white. // 方格的颜色为深绿色或者白色 final static Color darkGreen = new Color (0, 128, 0); // Dragging flag -- set to true when user presses mouse button over checker // and cleared to false when user releases mouse button. // 拖动标记 --当用户在棋子上按下鼠标按键时设为true, // 释放鼠标按键时设为false boolean inDrag = false; // Left coordinate of checkerboard's upper-left corner. // 棋盘左上角的左方向坐标 int boardx; // Top coordinate of checkerboard's upper-left corner. //棋盘左上角的上方向坐标 int boardy; // Left coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原点(左上角)的左方向坐标 int ox; // Top coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原点(左上角)的上方向坐标 int oy; // Left displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按键时的鼠标坐标与棋子矩形原点之间的左方向位移 int relx; // Top displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按键时的鼠标坐标与棋子矩形原点之间的上方向位移 int rely; // Width of applet drawing area. // applet绘图区域的宽度 int width; // Height of applet drawing area. // applet绘图区域的高度 int height; // Image buffer. // 图像缓冲 Image imBuffer; // Graphics context associated with image buffer. // 图像缓冲相关联的图形背景 Graphics imG; public void init () { // Obtain the size of the applet's drawing area. // 获取applet绘图区域的尺寸 width = getSize ().width; height = getSize ().height; // Create image buffer. // 创建图像缓冲 imBuffer = createImage (width, height); // Retrieve graphics context associated with image buffer. // 取出图像缓冲相关联的图形背景 imG = imBuffer.getGraphics (); // Initialize checkerboard's origin, so that board is centered. // 初始化棋盘的原点,使棋盘在屏幕上居中 boardx = (width - BOARDDIM) / 2 + 1; boardy = (height - BOARDDIM) / 2 + 1; // Initialize checker's rectangle's starting origin so that checker is // centered in the square located in the top row and second column from // the left. // 初始化棋子矩形的起始原点,使得棋子在第一行左数第二列的方格里居中 ox = boardx + SQUAREDIM + (SQUAREDIM - CHECKERDIM) / 2 + 1; oy = boardy + (SQUAREDIM - CHECKERDIM) / 2 + 1; // Attach a mouse listener to the applet. That listener listens for // mouse-button press and mouse-button release events. // 向applet添加一个用来监听鼠标按键的按下和释放事件的鼠标监听器 addMouseListener (new MouseAdapter () { public void mousePressed (MouseEvent e) { // Obtain mouse coordinates at time of press. // 获取按键时的鼠标坐标 int x = e.getX (); int y = e.getY (); // If mouse is over draggable checker at time // of press (i.e., contains (x, y) returns // true), save distance between current mouse // coordinates and draggable checker origin // (which will always be positive) and set drag // flag to true (to indicate drag in progress). // 在按键时如果鼠标位于可拖动的棋子上方 // (也就是contains (x, y)返回true),则保存当前 // 鼠标坐标与棋子的原点之间的距离(始终为正值)并且 // 将拖动标志设为true(用来表明正处在拖动过程中) if (contains (x, y)) { relx = x - ox; rely = y - oy; inDrag = true; } } boolean contains (int x, int y) { // Calculate center of draggable checker. // 计算棋子的中心位置 int cox = ox + CHECKERDIM / 2; int coy = oy + CHECKERDIM / 2; // Return true if (x, y) locates with bounds // of draggable checker. CHECKERDIM / 2 is the // radius. // 如果(x, y)仍处于棋子范围内则返回true // CHECKERDIM / 2为半径 return (cox - x) * (cox - x) + (coy - y) * (coy - y) CHECKERDIM / 2 * CHECKERDIM / 2; } public void mouseReleased (MouseEvent e) { // When mouse is released, clear inDrag (to // indicate no drag in progress) if inDrag is // already set. // 当鼠标按键被释放时,如果inDrag已经为true, // 则将其置为false(用来表明不在拖动过程中) if (inDrag) inDrag = false; } }); // Attach a mouse motion listener to the applet. That listener listens // for mouse drag events. //向applet添加一个用来监听鼠标拖动事件的鼠标运动监听器 addMouseMotionListener (new MouseMotionAdapter () { public void mouseDragged (MouseEvent e) { if (inDrag) { // Calculate draggable checker's new // origin (the upper-left corner of // the checker rectangle). // 计算棋子新的原点(棋子矩形的左上角) int tmpox = e.getX () - relx; int tmpoy = e.getY () - rely; // If the checker is not being moved // (at least partly) off board, // assign the previously calculated // origin (tmpox, tmpoy) as the // permanent origin (ox, oy), and // redraw the display area (with the // draggable checker at the new // coordinates). // 如果棋子(至少是棋子的一部分)没有被 // 移出棋盘,则将之前计算的原点 // (tmpox, tmpoy)赋值给永久性的原点(ox, oy), // 并且刷新显示区域(此时的棋子已经位于新坐标上) if (tmpox boardx tmpoy boardy tmpox + CHECKERDIM boardx + BOARDDIM tmpoy + CHECKERDIM boardy + BOARDDIM) { ox = tmpox; oy = tmpoy; repaint (); } } } }); } public void paint (Graphics g) { // Paint the checkerboard over which the checker will be dragged. // 在棋子将要被拖动的位置上绘制棋盘 paintCheckerBoard (imG, boardx, boardy); // Paint the checker that will be dragged. // 绘制即将被拖动的棋子 paintChecker (imG, ox, oy); // Draw contents of image buffer. // 绘制图像缓冲的内容 g.drawImage (imBuffer, 0, 0, this); } void paintChecker (Graphics g, int x, int y) { // Set checker shadow color. // 设置棋子阴影的颜色 g.setColor (Color.black); // Paint checker shadow. // 绘制棋子的阴影 g.fillOval (x, y, CHECKERDIM, CHECKERDIM); // Set checker color. // 设置棋子颜色 g.setColor (Color.red); // Paint checker. // 绘制棋子 g.fillOval (x, y, CHECKERDIM - CHECKERDIM / 13, CHECKERDIM - CHECKERDIM / 13); } void paintCheckerBoard (Graphics g, int x, int y) { // Paint checkerboard outline. // 绘制棋盘轮廓线 g.setColor (Color.black); g.drawRect (x, y, 8 * SQUAREDIM + 1, 8 * SQUAREDIM + 1); // Paint checkerboard. // 绘制棋盘 for (int row = 0; row 8; row++) { g.setColor (((row 1) != 0) ? darkGreen : Color.white); for (int col = 0; col 8; col++) { g.fillRect (x + 1 + col * SQUAREDIM, y + 1 + row * SQUAREDIM, SQUAREDIM, SQUAREDIM); g.setColor ((g.getColor () == darkGreen) ? Color.white : darkGreen); } } } // The AWT invokes the update() method in response to the repaint() method // calls that are made as a checker is dragged. The default implementation // of this method, which is inherited from the Container class, clears the // applet's drawing area to the background color prior to calling paint(). // This clearing followed by drawing causes flicker. CheckerDrag overrides // update() to prevent the background from being cleared, which eliminates // the flicker. // AWT调用了update()方法来响应拖动棋子时所调用的repaint()方法。该方法从 // Container类继承的默认实现会在调用paint()之前,将applet的绘图区域清除 // 为背景色,这种绘制之后的清除就导致了闪烁。CheckerDrag重写了update()来 // 防止背景被清除,从而消除了闪烁。 public void update (Graphics g) { paint (g); }}
Q3: 给段最简单的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!
Q4: 浅谈Java中用动态代理类实现记忆功能
记忆是衍生自Lisp Python和Perl等过程性语言的一种设计模式 它可以对前次的计算结果进行记忆 一个实现了记忆功能的函数 带有显式的cache 所以 已经计算过的结果就能直接从cache中获得 而不用每次都进行计算
记忆能显著的提升大计算量代码的效率 而且是一种可重用的方案
本文阐述了在Java中使用这一模式的方法 并提供了一个可以提供上述功能的 记忆类
Foo foo = (Foo) moize(new FooImpl()) 这里 Foo是一个接口 它含有的方法是需要记忆的 FooImpl是Foo的一个实现 foo是Foo的一个引用 方法与FooImpl基本相同 区别在于Foo返回的值 会被缓存起来 单个记忆类的优点在于为任何类添加记忆功能是很简单的 定义一个包含需要记忆的方法的接口 然后调用memoize来实现一个实例
为了理解记忆类是怎么实现的 我们将分几步来解释 首先 我解释一下为何缓存能够在需要它的类中实现 然后 我测试一下如何为一个特定的类添加缓存包装器 最后 我解释一下如何才能使得一个缓存包装器能够通用于任意的类
为大计算量的程序添加缓存
作为一个大计算量程序的例子 我们考虑PiBinaryDigitsCalculator这个例子 计算二进制数据pi 仅有的public方法calculateBinaryDigit带有一个参数 整数n 代表需要精确到的位数 例如 将会返回小数点后的一百万位 通过byte值返回 每位为 或者
public class PiBinaryDigitsCalculator { /** * Returns the coefficient of ^n in the binary * expansion of pi * @param n the binary digit of pi to calculate * @throws ValidityCheckFailedException if the validity * check fails this means the implementation is buggy * or n is too large for sufficient precision to be * retained */ public byte calculateBinaryDigit(final int n) { return runBBPAlgorithm(n) }
private byte runBBPAlgorithm(final int n) { // Lengthy routine goes here ……
}
}
最简单直接的方法来缓存返回值可以通过修改这个类来实现 添加一个Map来保存之前计算得到的值 如下
import java util HashMap public class PiBinaryDigitsCalculator {
private HashMap cache = new HashMap()
public synchronized byte calculateBinaryDigit(final int n) {
final Integer N = new Integer(n) Byte B = (Byte) cache get(N) if (B == null) { byte b = runBBPAlgorithm(n) cache put(N new Byte(b)) return b } else { return B bytevalue() }
private byte runBBPAlgorithm(final int n) { // Lengthy routine goes here ……
}
calculateBinaryDigit方法首先会检查HashMap里面是否缓存了这个关键字 参数n 如果找到了 就直接返回这个值 否则 就会进行这个冗长的计算 并将结果保存到缓存里面 在添加进HashMap的时候 在原始类型和对象之间还要进行小小的转换
尽管这个方法是可行的 但是 有几个缺点 首先 进行缓存的代码和正常的算法代码不是显著分开的 一个类 不仅负责进行计算 也要负责进行维护缓存数据 这样 要进行一些测试就会显得很困难 比如 不能写一个测试程序来测试这个算法持续地返回相同的值 因为 从第二次开始 结果都是直接从cache中获得了
其次 当缓存代码不再需要 移除它会变得困难 因为它和算法块的代码是紧密结合在一起的 所以 要想知道缓存是否带来了很高的效率提升也是很困难的 因为不能写一个测试程序是和缓存数据分开的 当你改进了你的算法 缓存有可能失效 但是这个时候你并不知道
第三 缓存代码不能被重用 尽管代码遵从了一个普通的模式 但是都是在一个类 PiBinaryDigitsCalculator里面
前面两个问题都可以通过构造一个缓存包装器来解决
缓存包装器
通过使用Decorator模式 要分开计算代码和缓存代码是很容易的 首先 定义一个接口 里面定义基本的方法
public interface BinaryDigitsCalculator { public byte calculateBinaryDigit(final int n) }然后定义两个实现 分别负责两个任务
public class PiBinaryDigitsCalculator implements BinaryDigitsCalculator {
public byte calculateBinaryDigit(final int n) { return runBBPAlgorithm(n) }
private byte runBBPAlgorithm(final int n) { // Lengthy routine goes here ……
}
import java util HashMap
public class CachingBinaryDigitsCalculator implements BinaryDigitsCalculator {
private BinaryDigitsCalculator binaryDigitsCalculator private HashMap cache = new HashMap()
public CachingBinaryDigitsCalculator(BinaryDigitsCalculator calculator) { this binaryDigitsCalculator = calculator }
public synchronized byte calculateBinaryDigit(int n) { final Integer N = new Integer(n) Byte B = (Byte) cache get(N) if (B == null) { byte b = binaryDigitsCalculator calculateBinaryDigit(n) cache put(N new Byte(b)) return b } else { return B bytevalue() }
这是很之前的实现PiBinaryDigitsCalculator的一种简单的refactored版本
lishixinzhi/Article/program/Java/hx/201311/25515
java记忆翻牌代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java翻牌游戏、java记忆翻牌代码的信息别忘了在本站进行查找喔。








