快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

java300行代码实现的简单介绍

求java代码300行左右,要有注释,能运行,基础点的越基础的越好,300行以上就好,不要太长的

300行能干啥啊,你还不如自己做做练习题。

创新互联是一家网站制作、成都做网站,提供网页设计,网站设计,网站制作,建网站,按需制作,网站开发公司,2013年至今是互联行业建设者,服务者。以提升客户品牌价值为核心业务,全程参与项目的网站策划设计制作,前端开发,后台程序制作以及后期项目运营并提出专业建议和思路。

package suoha;

import java.util.ArrayList;import java.util.Random;

public class MainFunction {

private ArrayListMyCard cards = new ArrayListMyCard();

private int[] cardNumArr = new int[52];

private int[] mycards = new int[5];

private int[] p1cards = new int[5];

public static void main(String[] args) {

MainFunction mainfunc = new MainFunction();

mainfunc.makeCards();

mainfunc.cardNumArr = mainfunc.shuffleCards(mainfunc.cardNumArr);

mainfunc.catchCards();

mainfunc.showAllCardsOfHand();

//mainfunc.printNums();

}

/**

* 洗牌

* @param cardNums

* @return

*/

private int[] shuffleCards(int[] cardNums){

int size = cardNums.length;

int[] returnNums = new int[size];

//System.out.println(cardNums[0]+"=========================");

Random rand = new Random();

for(int i=size-1;i0;i--){

int nextInt = rand.nextInt(i);

returnNums[size-1-i] = cardNums[nextInt];

cardNums[nextInt] = cardNums[i];

cardNums[i] = returnNums[size-1-i];

//System.out.println("suijishu:"+nextInt+"||zhi:"+returnNums[size-1-i]);

}

returnNums[size-1] = cardNums[0];

return returnNums;

}

/**

* 抓牌

*/

private void catchCards(){

for(int i=0;i5;i++){

mycards[i] = cardNumArr[2*i];

p1cards[i] = cardNumArr[2*i+1];

}

}

private void showAllCardsOfHand(){

System.out.print("你手中的牌是:||");

for(int i=0;i5;i++){

MyCard myCard = cards.get(mycards[i]-1);

System.out.print(myCard.toString()+"||");

}

System.out.println();

System.out.print("对家手中的牌是:||");

for(int i=0;i5;i++){

MyCard myCard = cards.get(p1cards[i]-1);

System.out.print(myCard.toString()+"||");

}

}

/**

* 显示牌的顺序

*/

private void printNums(){

for(int i=0;icardNumArr.length;i++){

System.out.print(cardNumArr[i]+",");

}

}

/**

* 制作卡片

*/

private void makeCards(){

for(int i=1;i=13;i++){

for(int j=1;j=4;j++){

//System.out.println((i-1)*4+j);

MyCard card = new MyCard((i-1)*4+j, j, i);

cardNumArr[(i-1)*4+j-1] = (i-1)*4+j;

cards.add(card);

}

// MyCard card1 = new MyCard((i-1)*4+1, 1, i);

// MyCard card2 = new MyCard((i-1)*4+2, 2, i);

// MyCard card3 = new MyCard((i-1)*4+3, 3, i);

// MyCard card4 = new MyCard((i-1)*4+4, 4, i);

// cards.add(card1);

// cards.add(card2);

// cards.add(card3);

// cards.add(card4);

}

}

}

利用java编写代码实现如下功能,需要全部代码

很简单的应用,为了节省字数,代码注释我就不加了

首先是显示层,LoinWindow:

import java.awt.FlowLayout;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.FocusEvent;

import java.awt.event.FocusListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.border.EmptyBorder;

public class LoinWindow extends JFrame implements ActionListener, FocusListener {

private JPanel mainPanel, namePanel, btnPanel;

private JTextField tfName, tfPsd;

private JButton btnLogin, btnCancel;

private static final int WIDTH = 300;

private static final int HEIGHT = 200;

private LoginService service = new LoginService();

public LoinWindow() {

super("登录窗体");

}

public void launch() {

setSize(WIDTH, HEIGHT);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

GridLayout mainLayout = new GridLayout(2, 1);

mainLayout.setVgap(10);

mainPanel = new JPanel(mainLayout);

GridBagLayout nameLayout = new GridBagLayout();

namePanel = new JPanel(nameLayout);

namePanel.setBorder(new EmptyBorder(10, 10, 10, 10));

JLabel nameLabel = new JLabel("姓名:");

tfName = new JTextField();

JLabel psdLabel = new JLabel("密码:");

tfPsd = new JTextField();

JLabel blank = new JLabel(" ");

namePanel.add(nameLabel);

namePanel.add(tfName);

namePanel.add(blank);

namePanel.add(psdLabel);

namePanel.add(tfPsd);

GridBagConstraints s = new GridBagConstraints();

s.fill = GridBagConstraints.BOTH;

s.gridwidth = 1;

s.weightx = 0;

s.weighty = 0;

nameLayout.setConstraints(nameLabel, s);

s.gridwidth = 0;

s.weightx = 1;

s.weighty = 0;

nameLayout.setConstraints(tfName, s);

s.gridwidth = 0;

s.weightx = 4;

s.weighty = 0;

nameLayout.setConstraints(blank, s);

s.gridwidth = 1;

s.weightx = 0;

s.weighty = 0;

nameLayout.setConstraints(psdLabel, s);

s.gridwidth = 3;

s.weightx = 1;

s.weighty = 0;

nameLayout.setConstraints(tfPsd, s);

FlowLayout btnLayout = new FlowLayout();

btnLayout.setAlignment(FlowLayout.CENTER);

btnPanel = new JPanel(btnLayout);

btnLogin = new JButton("确定");

btnCancel = new JButton("取消");

btnPanel.add(btnLogin);

btnPanel.add(btnCancel);

btnCancel.addActionListener(this);

btnLogin.addActionListener(this);

mainPanel.add(namePanel);

mainPanel.add(btnPanel);

setContentPane(mainPanel);

tfName.addFocusListener(this);

tfPsd.addFocusListener(this);

pack();

setSize(WIDTH, HEIGHT);

setLocationRelativeTo(null);

}

@Override

public void actionPerformed(ActionEvent e) {

Object source = e.getSource();

if(source == btnCancel) {

System.exit(0);

} else if(source == btnLogin) {

String username = tfName.getText();

String password = tfPsd.getText();

boolean success = service.login(username, password);

if(success) {

warn("成功", "登录成功!");

} else {

warn("失败", "您输入的用户名或密码错误 !");

}

}

}

@Override

public void focusGained(FocusEvent arg0) {

}

@Override

public void focusLost(FocusEvent e) {

Object source = e.getSource();

if(source == tfName) {

String username = tfName.getText();

try {

service.matchUsername(username);

} catch (LoginException e1) {

warn("验证错误", e1.getMessage());

}

} else if(source == tfPsd) {

String password = tfPsd.getText();

try {

service.matchPassword(password);

} catch (LoginException e1) {

warn("验证错误", e1.getMessage());

}

}

}

private void warn(String title, String msg) {

JOptionPane.showMessageDialog(null, msg, title, JOptionPane.INFORMATION_MESSAGE);

}

public static void main(String[] args) {

new LoinWindow().launch();

}

}

然后是模型层:LoginDao

public class LoginDao {

public boolean login(String username, String password) {

if(username.equals("admin")  password.equals("12345")) {

return true;

}

return false;

}

}

LoginService

import java.util.regex.Pattern;

public class LoginService {

private static final Pattern LOGIN_PATTERN = Pattern.compile("[a-zA-Z]+");

private static final Pattern PASSWORD_PATTERN = Pattern.compile("[1-9]+");

private LoginDao dao = new LoginDao();

public boolean matchUsername(String username) throws LoginException {

if(null == username || username.isEmpty()) {

return false;

}

if(!LOGIN_PATTERN.matcher(username).matches()) {

throw new LoginException("您输入的用户名不合法,请输入英文!");

}

return true;

}

public boolean matchPassword(String password) throws LoginException {

if(null == password || password.isEmpty()) {

return false;

}

if(!PASSWORD_PATTERN.matcher(password).matches()) {

throw new LoginException("您输入的密码不合法,请输入数字!");

}

return true;

}

public boolean login(String username, String password) {

if(null == username || username.isEmpty()) {

return false;

}

if(null == password || password.isEmpty()) {

return false;

}

if(!dao.login(username, password)) {

return false;

}

return true;

}

}

LoginException

public class LoginException extends Exception {

public LoginException(String arg0) {

super(arg0);

}

}

不知道分层设计思想是不是我想的这样

求基础级java代码,150-200行,自己写的

我有计算器程序

import javax.swing.*;

import javax.swing.border.Border;

import java.awt.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.math.BigDecimal;

import java.math.RoundingMode;

import java.util.HashMap;

/**

* 我的计算器。MyCalculator 继承于 JFrame,是计算器的界面

*/

public class Calculator extends JFrame {

/**

*

*/

private static final long serialVersionUID = 1L;

private Border border = BorderFactory.createEmptyBorder(5, 5, 5, 5);

private JTextField textbox = new JTextField("0");

private CalculatorCore core = new CalculatorCore();

private ActionListener listener = new ActionListener() {

public void actionPerformed(ActionEvent e) {

JButton b = (JButton) e.getSource();

String label = b.getText();

String result = core.process(label);

textbox.setText(result);

}

};

public Calculator(String title) throws HeadlessException {

super(title); // 调用父类构造方法

setupFrame(); // 调整窗体属性

setupControls(); // 创建控件

}

private void setupControls() {

setupDisplayPanel(); // 创建文本面板

setupButtonsPanel(); // 创建按钮面板

}

// 创建按钮面板并添加按钮

private void setupButtonsPanel() {

JPanel panel = new JPanel();

panel.setBorder(border);

panel.setLayout(new GridLayout(4, 5, 3, 3));

createButtons(panel, new String[]{

"7", "8", "9", "+", "C",

"4", "5", "6", "-", "CE",

"1", "2", "3", "*", "", // 空字符串表示这个位置没有按钮

"0", ".", "=", "/", ""

});

this.add(panel, BorderLayout.CENTER);

}

/**

* 在指定的面板上创建按钮

*

* @param panel 要创建按钮的面板

* @param labels 按钮文字

*/

private void createButtons(JPanel panel, String[] labels) {

for (String label : labels) {

// 如果 label 为空,则表示创建一个空面板。否则创建一个按钮。

if (label.equals("")) {

panel.add(new JPanel());

} else {

JButton b = new JButton(label);

b.addActionListener(listener); // 为按钮添加侦听器

panel.add(b);

}

}

}

// 设置显示面板,用一个文本框来作为计算器的显示部分。

private void setupDisplayPanel() {

JPanel panel = new JPanel();

panel.setLayout(new BorderLayout());

panel.setBorder(border);

setupTextbox();

panel.add(textbox, BorderLayout.CENTER);

this.add(panel, BorderLayout.NORTH);

}

// 调整文本框

private void setupTextbox() {

textbox.setHorizontalAlignment(JTextField.RIGHT); // 文本右对齐

textbox.setEditable(false); // 文本框只读

textbox.setBackground(Color.white); // 文本框背景色为白色

}

// 调整窗体

private void setupFrame() {

this.setDefaultCloseOperation(EXIT_ON_CLOSE); // 当窗体关闭时程序结束

this.setLocation(100, 50); // 设置窗体显示在桌面上的位置

this.setSize(300, 200); // 设置窗体大小

this.setResizable(false); // 窗体大小固定

}

// 程序入口

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

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

Calculator frame = new Calculator("我的计算器");

frame.setVisible(true); // 在桌面上显示窗体

}

}

/**

* 计算器核心逻辑。这个逻辑只能处理 1~2 个数的运算。

*/

class CalculatorCore {

private String displayText = "0"; // 要显示的文本

boolean reset = true;

int MaxLen = 30;

private BigDecimal number1, number2;

private String operator;

private HashMapString, Operator operators = new HashMapString, Operator();

private HashMapString, Processor processors = new HashMapString, Processor();

CalculatorCore() {

setupOperators();

setupProcessors();

}

// 为每种命令添加处理方式

private void setupProcessors() {

processors.put("[0-9]", new Processor() {

public void calculate(String command) {

numberClicked(command);

}

});

processors.put("\\.", new Processor() {

public void calculate(String command) {

dotClicked();

}

});

processors.put("=", new Processor() {

public void calculate(String command) {

equalsClicked();

}

});

processors.put("[+\\-*/]", new Processor() {

public void calculate(String command) {

operatorClicked(command);

}

});

processors.put("C", new Processor() {

public void calculate(String command) {

clearClicked();

}

});

processors.put("CE", new Processor() {

public void calculate(String command) {

clearErrorClicked();

}

});

}

// 为每种 operator 添加处理方式

private void setupOperators() {

operators.put("+", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.add(number2);

}

});

operators.put("-", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.subtract(number2);

}

});

operators.put("*", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.multiply(number2);

}

});

operators.put("/", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.divide(number2, 30, RoundingMode.HALF_UP);

}

});

}

// 根据命令处理。这里的命令实际上就是按钮文本。

public String process(String command) {

for (String pattern : processors.keySet()) {

if (command.matches(pattern)) {

processors.get(pattern).calculate(command);

break;

}

}

return displayText;

}

// 当按下 CE 时

private void clearErrorClicked() {

if (operator == null) {

number1 = null;

} else {

number2 = null;

}

displayText = "0";

reset = true;

}

// 当按下 C 时,将计算器置为初始状态。

private void clearClicked() {

number1 = null;

number2 = null;

operator = null;

displayText = "0";

reset = true;

}

// 当按下 = 时

private void equalsClicked() {

calculateResult();

number1 = null;

number2 = null;

operator = null;

reset = true;

}

// 计算结果

/**

*

*/

private void calculateResult() {

number2 = new BigDecimal(displayText);

Operator oper = operators.get(operator);

if (oper != null) {

try {

BigDecimal result = oper.process(number1, number2);

displayText = result.toString();

} catch (RuntimeException e) {

clearClicked();//将计算器置为初始状态

JOptionPane.showMessageDialog(null,"不能用零作除数","出错了",JOptionPane.OK_OPTION);

//e.printStackTrace();

}

}

}

// 当按下 +-*/ 时(这里也可以扩展成其他中间操作符)

private void operatorClicked(String command) {

if (operator != null) {

calculateResult();

}

number1 = new BigDecimal(displayText);

operator = command;

reset = true;

}

// 当按下 . 时

private void dotClicked() {

if (displayText.indexOf(".") == -1) {

displayText += ".";

} else if (reset) {

displayText = "0.";

}

reset = false;

}

// 当按下 0-9 时

private void numberClicked(String command) {

if (reset) {

displayText = command;

} else {

if(displayText.length() MaxLen)

displayText += command;

else

JOptionPane.showMessageDialog(null,"输入的数字太长了","出错了",JOptionPane.OK_OPTION);

}

reset = false;

}

// 运算符处理接口

interface Operator {

BigDecimal process(BigDecimal number1, BigDecimal number2);

}

// 按钮处理接口

interface Processor {

void calculate(String command);

}

}

谁帮我写一段300行左右的代码?

这个计算器可是我自己写了好几天才写好的耶,你能在网上找到?

计算器:

// Calculator.java

public class Calculator {

private static final char NO_OP = '\0';

private static final char PLUS = '+';

private static final char SUBTRACT = '-';

private static final char MULTIPLY = '*';

private static final char DIVIDE = '/';

private float number1 = 0.0F;

//private float number2 = Float.NaN;

private char operator = NO_OP;

//等于运算

public String opEquals(String number) {

float result;

if ( operator == NO_OP ) {

result = parseNumber(number);

} else {

result = performOperation(parseNumber(number));

}

operator = NO_OP;

number1 = result;

return Float.toString(result);

}

//加法运算

public String opAdd(String number) {

float result;

if ( operator == NO_OP ) {

result = parseNumber(number);

} else {

result = performOperation(parseNumber(number));

}

operator = PLUS;

number1 = result;

return Float.toString(result);

}

//减法运算

public String opSubtract(String number) {

float result;

if ( operator == NO_OP ) {

result = parseNumber(number);

} else {

result = performOperation(parseNumber(number));

}

operator = SUBTRACT;

number1 = result;

return Float.toString(result);

}

//乘法运算

public String opMultiply(String number) {

float result;

if ( operator == NO_OP ) {

result = parseNumber(number);

} else {

result = performOperation(parseNumber(number));

}

operator = MULTIPLY;

number1 = result;

return Float.toString(result);

}

//除法运算

public String opDivide(String number) {

float result;

if ( operator == NO_OP ) {

result = parseNumber(number);

} else {

result = performOperation(parseNumber(number));

}

operator = DIVIDE;

number1 = result;

return Float.toString(result);

}

//处理运算符

private float performOperation(float number2) {

float result = 0.0F;

switch ( operator ) {

case PLUS:

result = number1 + number2;

break;

case SUBTRACT:

result = number1 - number2;

break;

case MULTIPLY:

result = number1 * number2;

break;

case DIVIDE:

result = number1 / number2;

break;

}

return result;

}

//将字符串类型转换为浮点型

private static float parseNumber(String number) {

float real_number;

try {

real_number = Float.parseFloat(number);

} catch (NumberFormatException e) {

real_number = Float.NaN;

}

return real_number;

}

}

GUI程序:

// CalculatorGUI.java

import java.awt.*;

import java.awt.event.*;

public class CalculatorGUI {

// 定义按钮

private Button key0, key1, key2, key3, key4;

private Button key5, key6, key7, key8, key9;

private Button keyequal, keyplus, keyminus;

private Button keyperiod, keymult, keydiv;

// 定义存放按钮的区域

private Panel buttonArea;

// 定义存放运算结果的区域

private Label answer;

// 用来实现运算功能的类的对象实例化

private Calculator calculator;

private boolean readyForNextNumber;

public CalculatorGUI() {

calculator = new Calculator();

readyForNextNumber = true;

answer = new Label("0.0",Label.RIGHT);

key0 = new Button("0");

key1 = new Button("1");

key2 = new Button("2");

key3 = new Button("3");

key4 = new Button("4");

key5 = new Button("5");

key6 = new Button("6");

key7 = new Button("7");

key8 = new Button("8");

key9 = new Button("9");

keyequal = new Button("=");

keyplus = new Button("+");

keyminus = new Button("-");

keymult = new Button("*");

keydiv = new Button("/");

keyperiod = new Button(".");

buttonArea = new Panel();

}

public void launchFrame() {

buttonArea.setLayout(new GridLayout(4,4));

buttonArea.add(key7);

buttonArea.add(key8);

buttonArea.add(key9);

buttonArea.add(keyplus);

buttonArea.add(key4);

buttonArea.add(key5);

buttonArea.add(key6);

buttonArea.add(keyminus);

buttonArea.add(key1);

buttonArea.add(key2);

buttonArea.add(key3);

buttonArea.add(keymult);

buttonArea.add(key0);

buttonArea.add(keyperiod);

buttonArea.add(keyequal);

buttonArea.add(keydiv);

// 设置事件接收句柄

OpButtonHanlder op_handler = new OpButtonHanlder();

NumberButtonHanlder number_handler = new NumberButtonHanlder();

key0.addActionListener(number_handler);

key1.addActionListener(number_handler);

key2.addActionListener(number_handler);

key3.addActionListener(number_handler);

key4.addActionListener(number_handler);

key5.addActionListener(number_handler);

key6.addActionListener(number_handler);

key7.addActionListener(number_handler);

key8.addActionListener(number_handler);

key9.addActionListener(number_handler);

keyperiod.addActionListener(number_handler);

keyplus.addActionListener(op_handler);

keyminus.addActionListener(op_handler);

keymult.addActionListener(op_handler);

keydiv.addActionListener(op_handler);

keyequal.addActionListener(op_handler);

// 新建一个帧,并且加上消息监听

Frame f = new Frame("计算器");

f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

f.setSize(200, 200);

f.add(answer, BorderLayout.NORTH);

f.add(buttonArea, BorderLayout.CENTER);

f.setVisible (true);

}

//响应按钮事件,并根据不同的按钮事件进行不同的运算

private class OpButtonHanlder implements ActionListener {

public void actionPerformed(ActionEvent event) {

char operator = event.getActionCommand().charAt(0);

String result = "";

switch ( operator ) {

case '+':

result = calculator.opAdd(answer.getText());

break;

case '-':

result = calculator.opSubtract(answer.getText());

break;

case '*':

result = calculator.opMultiply(answer.getText());

break;

case '/':

result = calculator.opDivide(answer.getText());

break;

case '=':

result = calculator.opEquals(answer.getText());

break;

}

answer.setText(result);

readyForNextNumber = true;

}

}

//处理连续运算时主界面的显示

private class NumberButtonHanlder implements ActionListener {

public void actionPerformed(ActionEvent event) {

if ( readyForNextNumber ) {

answer.setText(event.getActionCommand());

readyForNextNumber = false;

} else {

answer.setText(answer.getText() + event.getActionCommand().charAt(0));

}

}

}

//主函数,也是该应用程序的执行入口处

public static void main(String[] args) {

CalculatorGUI calcGUI = new CalculatorGUI();

calcGUI.launchFrame();

}

}

注意:运行的是GUI程序


分享题目:java300行代码实现的简单介绍
分享路径:http://6mz.cn/article/hgchji.html

其他资讯