189 8069 5689

java手写数字识别代码 java手写数字识别代码有哪些

java窗体猜数字代码

import java.awt.*;

网站建设公司,为您提供网站建设,网站制作,网页设计及定制网站建设服务,专注于企业网站建设,高端网页制作,对成都塑料袋等多个行业拥有丰富的网站建设经验的网站建设公司。专业网站设计,网站优化推广哪家好,专业网站推广优化,H5建站,响应式网站。

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class caishuzi extends JFrame implements ActionListener

{

private static final long serialVersionUID = 1L;

private JPanel textpanel, buttonpanel, fopanel, downpanel;

private JButton btn;

private JTextField jtinput, jttishi;

private JLabel lbnum0, lbnum, lbtishi, lbinput;

private JTextArea tafo;

private Boolean start = false, jisuan = false, jinru = false;

MenuBar mb = new MenuBar();

Menu filemenu = new Menu("文件"), systemmenu = new Menu("系统"),

helpmenu = new Menu("帮助");

MenuItem newfile = new MenuItem("文件"), openfile = new MenuItem("打开"),

closefile = new MenuItem("关闭"), quit = new MenuItem("退出"),

inputsecret = new MenuItem("输入密码"), help = new MenuItem("帮助"),

inputcishu = new MenuItem("输入限制次数");

char[] c = new char[4];

String strsecret = "123";

int xianzhi=8;

caishuzi()

{

super("猜数字");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//this.setLocationRelativeTo(null);

// 设置菜单

filemenu.add(newfile);

filemenu.add(openfile);

filemenu.addSeparator();

filemenu.add(closefile);

newfile.setEnabled(false);

openfile.setEnabled(false);

closefile.setEnabled(false);

inputsecret.addActionListener(this);

inputcishu.addActionListener(this);

inputcishu.setEnabled(false);

systemmenu.add(inputsecret);

systemmenu.add(inputcishu);

systemmenu.add(quit);

quit.addActionListener(this);

help.addActionListener(this);

helpmenu.add(help);

mb.add(filemenu);

mb.add(systemmenu);

mb.add(helpmenu);

this.setMenuBar(mb);

Container c = this.getContentPane();

BorderLayout b = new BorderLayout();

b.setVgap(10);

c.setLayout(b);

settextpanel();

c.add(textpanel, BorderLayout.NORTH);

setbuttonpanel();

setfopanel();

setdownpanel();

c.add(downpanel, BorderLayout.CENTER);

this.setSize(680,350);

//this.setResizable(false);

//this.pack();

this.setVisible(true);

}

void settextpanel()

{

textpanel = new JPanel();

textpanel.setLayout(new FlowLayout());

lbinput = new JLabel("输入:");

jtinput = new JTextField("", 10);

jtinput.setEditable(false);

jttishi = new JTextField(30);

lbnum = new JLabel("8");

lbnum0 = new JLabel("剩余次数:");

lbtishi = new JLabel("提示:");

jttishi.setEditable(false);

textpanel.add(lbinput);

textpanel.add(jtinput);

textpanel.add(lbtishi);

textpanel.add(jttishi);

textpanel.add(lbnum0);

textpanel.add(lbnum);

}

void setbuttonpanel()

{

String[] str =

{ "7", "8", "9", "4", "5", "6", "1", "2", "3", "确定", "0", "退格" };

buttonpanel = new JPanel();

buttonpanel.setLayout(new GridLayout(4, 3, 4, 8));

for (int i = 0; i str.length; i++)

{

btn = new JButton(str[i]);

btn.addActionListener(this);

buttonpanel.add(btn);

}

}

void setfopanel()

{

JButton btnright = new JButton("正确答案");

JPanel p = new JPanel();

btnright.addActionListener(this);

fopanel = new JPanel();

fopanel.setLayout(new BorderLayout());

btn = new JButton("开始");

btn.addActionListener(this);

tafo = new JTextArea(15,12);

p.setLayout(new GridLayout(1, 2));

p.add(btn);

p.add(btnright);

fopanel.add(p, BorderLayout.NORTH);

tafo.setEditable(false);

fopanel.add(tafo, BorderLayout.CENTER);

}

void setdownpanel()

{

downpanel = new JPanel();

downpanel.setLayout(new GridLayout(1, 2, 30, 0));

downpanel.add(buttonpanel);

downpanel.add(fopanel);

}

public static void main(String[] args)

{

new caishuzi();

}

public void actionPerformed(ActionEvent e)

{

String s = e.getActionCommand();

if (s.equals("退出"))

{

int i = JOptionPane.showConfirmDialog(this, "确认退出?", "",

JOptionPane.YES_NO_OPTION);

//System.out.println(i);

if (i == 0)

{

System.exit(0);

}

}

if (s.equals("帮助"))

{

JOptionPane.showMessageDialog(this,

"猜数字游戏,可以输入限制次数,密码为123,\n在菜单:系统——输入密码 中输入!");

}

if (s.equals("输入密码"))

{

while (true)

{

String inputValue = "";

inputValue = JOptionPane.showInputDialog(this, "输入密码");

// System.out.println(inputValue);

if (inputValue == null)

return;

if (inputValue.equals(strsecret))

{

jinru = true;

jttishi.setText("密码正确,点击开始");

inputsecret.setEnabled(false);

break;

} else

{

int i = JOptionPane.showConfirmDialog(this, "密码错误,重新输入?",

"", JOptionPane.YES_NO_OPTION);

if (i == 1)

break;

}

}

}

if (!jinru)

{

jttishi.setText("请输入密码:");

return;

}

if (s.equals("开始") || s.equals("重玩"))

{

StringBuffer sb = new StringBuffer("0123456789");

for (int i = 0; i c.length; i++)

{

int n = (int) (Math.random() * sb.length());

c[i] = sb.charAt(n);

sb.deleteCharAt(n);

}

btn.setText("重玩");

start = true;

lbnum.setText(String.valueOf(xianzhi));

jisuan = false;

//System.out.println(c);

jttishi.setText("请输入四不重复的数:");

jtinput.setText("");

tafo.setText("");

}

if (s.compareTo("0") = 0 s.compareTo("9") = 0)

{

jisuan = false;

if (!start)

jttishi.setText("点击开始!");

else

{

if (jtinput.getText().length() 4)

{

if (jtinput.getText().indexOf(s) 0)

{

jtinput.setText(jtinput.getText() + s);

if (jtinput.getText().length() 4)

jttishi.setText("还差"

+ (4 - jtinput.getText().length()) + "个数");

else

{

jttishi.setText("输入正确,点击确定");

jisuan = true;

}

} else

jttishi.setText("不能输入重复的数");

} else

{

jttishi.setText("不能超过四个数,点击确定");

jisuan = true;

}

}

}

if (s.equals("退格") start)

{

if (jtinput.getText().length() 0)

{

jtinput.setText(jtinput.getText().substring(0,

jtinput.getText().length() - 1));

jttishi.setText("还差" + (4 - jtinput.getText().length()) + "个数");

}

jisuan = false;

}

if (s.equals("确定"))

{

if (!start)

{

jttishi.setText("还没开始呢!");

return;

}

if (!jisuan)

{

jttishi.setText("输入不正确!");

return;

}

int numa = 0, numb = 0;

char[] c1 = jtinput.getText().toCharArray();

// System.out.println(c1);

for (int i = 0; i c1.length; i++)

{

for (int j = 0; j c.length; j++)

{

if (c1[i] == c[j] i != j)

{

numb++;

}

if (c1[i] == c[j] i == j)

{

numa++;

}

}

}

tafo.append(jtinput.getText() + "\t" + numa + "A" + numb + "B"

+ "\n");

jtinput.setText("");

jisuan = false;

if (numa == 4)

{

jttishi.setText("答对了,再来一局吧!");

start = false;

btn.setText("开始");

tafo.append("恭喜,答对了! ");

} else

{

lbnum.setText(String

.valueOf(Integer.valueOf(lbnum.getText()) - 1));

if (Integer.valueOf(lbnum.getText()) == 0)

{

String str = "";

jttishi.setText("机会没有了!很遗憾");

for (int i = 0; i c.length; i++)

str += c[i];

tafo.append("超过限制次数,失败!" + "正确答案是:" + str);

start = false;

btn.setText("开始");

} else

jttishi.setText("输入四个数:");

}

}

if (s.equals("正确答案"))

{

if (!start)

{

jttishi.setText("还没开始呢!");

return;

}

String str = "";

for (int i = 0; i c.length; i++)

str += c[i];

tafo.append("正确答案是:" + str);

jttishi.setText("重新开始吧!");

start = false;

jisuan = false;

btn.setText("开始");

}

if (!start)

{

inputcishu.setEnabled(true);

} else

inputcishu.setEnabled(false);

if (s.equals("输入限制次数"))

{

while (true)

{

try

{

String str = JOptionPane.showInputDialog(this,

"输入小于15的整数数字:");

if (str == null)

return;

if (Integer.valueOf(str) = 15 Integer.valueOf(str) 0)

{

xianzhi=Integer.valueOf(str);

lbnum.setText(String.valueOf(xianzhi));

break;

} else

{

int i = JOptionPane.showConfirmDialog(this, "输入错误,重试?",

"", JOptionPane.YES_NO_OPTION);

if (i == 1)

break;

}

} catch (Exception ex)

{

int i = JOptionPane.showConfirmDialog(this, "输入错误,重试?", "",

JOptionPane.YES_NO_OPTION);

if (i == 1)

break;

}

}

}

}

}

我做课程设计的最初代码 参考下吧 运行时先看帮助~

初学java,用以下代码(str.matches("\\d")) 来判断输入内容是否是数字

str.matches("\\d+")

\\d就是一位数字的意思,用一个+加号就行了,是一个以上的意思

求高手写个java代码!!!

代码如下:

import java.util.Arrays;

class Circle {

private int radius;

public Circle(int radius) {

this.radius = radius;

}

public int getRadius() {

return radius;

}

public void setRadius(int radius) {

this.radius = radius;

}

@Override

public String toString() {

return "Circle [radius=" + radius + "]";

}

}

public class App {

public static void main(String[] args) throws CloneNotSupportedException {

// 创建一个包含5个元素的数组

Circle[] circles = { new Circle(2), new Circle(10), new Circle(8), new Circle(4), new Circle(12) }; 

System.out.println(Arrays.toString(circles));

// 排序

Arrays.sort(circles, (x, y) - Integer.compare(x.getRadius(), y.getRadius()));

System.out.println(Arrays.toString(circles));

// 查找半径为 9 的圆

int index = Arrays.binarySearch(circles, 9, (x, y) - ((Circle)x).getRadius() - (int)y);

System.out.println(index =0 ? circles[index] : "没有找到半径为 9 的圆。");

// 查找半径为 10 的圆

index = Arrays.binarySearch(circles, 10, (x, y) - ((Circle)x).getRadius() - (int)y);

System.out.println(index =0 ? circles[index] : "没有找到半径为 10 的圆。");

// 拷贝数组

Circle[] circles2 = Arrays.copyOf(circles, circles.length);

System.out.println(Arrays.toString(circles2));

}

}

java导入mnist后怎样识别一个数字

其实就是python怎么读取binnary file

mnist的结构如下,选取train-images

TRAINING SET IMAGE FILE (train-images-idx3-ubyte):

[offset] [type] [value] [description]

0000 32 bit integer 0x00000803(2051) magic number

0004 32 bit integer 60000 number of images

0008 32 bit integer 28 number of rows

0012 32 bit integer 28 number of columns

0016 unsigned byte ?? pixel

0017 unsigned byte ?? pixel

........

xxxx unsigned byte ?? pixel

也就是之前我们要读取4个 32 bit integer

试过很多方法,觉得最方便的,至少对我来说还是使用

struct.unpack_from()

filename = 'train-images.idx3-ubyte'

binfile = open(filename , 'rb')

buf = binfile.read()

先使用二进制方式把文件都读进来

index = 0

magic, numImages , numRows , numColumns = struct.unpack_from('IIII' , buf , index)

index += struct.calcsize('IIII')

然后使用struc.unpack_from

'IIII'是说使用大端法读取4个unsinged int32

然后读取一个图片测试是否读取成功

im = struct.unpack_from('784B' ,buf, index)

index += struct.calcsize('784B')

im = np.array(im)

im = im.reshape(28,28)

fig = plt.figure()

plotwindow = fig.add_subplot(111)

plt.imshow(im , cmap='gray')

plt.show()

'784B'的意思就是用大端法读取784个unsigned byte

完整代码如下

import numpy as np

import struct

import matplotlib.pyplot as plt

filename = 'train-images.idx3-ubyte'

binfile = open(filename , 'rb')

buf = binfile.read()

index = 0

magic, numImages , numRows , numColumns = struct.unpack_from('IIII' , buf , index)

index += struct.calcsize('IIII')

im = struct.unpack_from('784B' ,buf, index)

index += struct.calcsize('784B')

im = np.array(im)

im = im.reshape(28,28)

fig = plt.figure()

plotwindow = fig.add_subplot(111)

plt.imshow(im , cmap='gray')

plt.show()

只是为了测试是否成功所以只读了一张图片


文章题目:java手写数字识别代码 java手写数字识别代码有哪些
转载源于:http://jkwzsj.com/article/dodgjgs.html

其他资讯