「java大作业代码」javaweb大作业带源码

博主:adminadmin 2022-11-24 17:47:08 47

今天给各位分享java大作业代码的知识,其中也会对javaweb大作业带源码进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

求java小程序代码,500行左右。。大作业用。追加50

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class mypanel extends Panel implements MouseListener

{

int chess[][] = new int[11][11];

boolean Is_Black_True;

mypanel()

{

Is_Black_True = true;

for(int i = 0;i 11;i++)

{

for(int j = 0;j 11;j++)

{

chess[i][j] = 0;

}

}

addMouseListener(this);

setBackground(Color.BLUE);

setBounds(0, 0, 360, 360);

setVisible(true);

}

public void mousePressed(MouseEvent e)

{

int x = e.getX();

int y = e.getY();

if(x 25 || x 330 + 25 ||y 25 || y 330+25)

{

return;

}

if(chess[x/30-1][y/30-1] != 0)

{

return;

}

if(Is_Black_True == true)

{

chess[x/30-1][y/30-1] = 1;

Is_Black_True = false;

repaint();

Justisewiner();

return;

}

if(Is_Black_True == false)

{

chess[x/30-1][y/30-1] = 2;

Is_Black_True = true;

repaint();

Justisewiner();

return;

}

}

void Drawline(Graphics g)

{

for(int i = 30;i = 330;i += 30)

{

for(int j = 30;j = 330; j+= 30)

{

g.setColor(Color.WHITE);

g.drawLine(i, j, i, 330);

}

}

for(int j = 30;j = 330;j += 30)

{

g.setColor(Color.WHITE);

g.drawLine(30, j, 330, j);

}

}

void Drawchess(Graphics g)

{

for(int i = 0;i 11;i++)

{

for(int j = 0;j 11;j++)

{

if(chess[i][j] == 1)

{

g.setColor(Color.BLACK);

g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16);

}

if(chess[i][j] == 2)

{

g.setColor(Color.WHITE);

g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16);

}

}

}

}

void Justisewiner()

{

int black_count = 0;

int white_count = 0;

int i = 0;

for(i = 0;i 11;i++)//横向判断

{

for(int j = 0;j 11;j++)

{

if(chess[i][j] == 1)

{

black_count++;

if(black_count == 5)

{

JOptionPane.showMessageDialog(this, "黑棋胜利");

Clear_Chess();

return;

}

}

else

{

black_count = 0;

}

if(chess[i][j] == 2)

{

white_count++;

if(white_count == 5)

{

JOptionPane.showMessageDialog(this, "白棋胜利");

Clear_Chess();

return;

}

}

else

{

white_count = 0;

}

}

}

for(i = 0;i 11;i++)//竖向判断

{

for(int j = 0;j 11;j++)

{

if(chess[j][i] == 1)

{

black_count++;

if(black_count == 5)

{

JOptionPane.showMessageDialog(this, "黑棋胜利");

Clear_Chess();

return;

}

}

else

{

black_count = 0;

}

if(chess[j][i] == 2)

{

white_count++;

if(white_count == 5)

{

JOptionPane.showMessageDialog(this, "白棋胜利");

Clear_Chess();

return;

}

}

else

{

white_count = 0;

}

}

}

for(i = 0;i 7;i++)//左向右斜判断

{

for(int j = 0;j 7;j++)

{

for(int k = 0;k 5;k++)

{

if(chess[i + k][j + k] == 1)

{

black_count++;

if(black_count == 5)

{

JOptionPane.showMessageDialog(this, "黑棋胜利");

Clear_Chess();

return;

}

}

else

{

black_count = 0;

}

if(chess[i + k][j + k] == 2)

{

white_count++;

if(white_count == 5)

{

JOptionPane.showMessageDialog(this, "白棋胜利");

Clear_Chess();

return;

}

}

else

{

white_count = 0;

}

}

}

}

for(i = 4;i 11;i++)//右向左斜判断

{

for(int j = 6;j = 0;j--)

{

for(int k = 0;k 5;k++)

{

if(chess[i - k][j + k] == 1)

{

black_count++;

if(black_count == 5)

{

JOptionPane.showMessageDialog(this, "黑棋胜利");

Clear_Chess();

return;

}

}

else

{

black_count = 0;

}

if(chess[i - k][j + k] == 2)

{

white_count++;

if(white_count == 5)

{

JOptionPane.showMessageDialog(this, "白棋胜利");

Clear_Chess();

return;

}

}

else

{

white_count = 0;

}

}

}

}

}

void Clear_Chess()

{

for(int i=0;i11;i++)

{

for(int j=0;j11;j++)

{

chess[i][j]=0;

}

}

repaint();

}

public void paint(Graphics g)

{

Drawline(g);

Drawchess(g);

}

public void mouseExited(MouseEvent e){}

public void mouseEntered(MouseEvent e){}

public void mouseReleased(MouseEvent e){}

public void mouseClicked(MouseEvent e){}

}

class myframe extends Frame implements WindowListener

{

mypanel panel;

myframe()

{

setLayout(null);

panel = new mypanel();

add(panel);

panel.setBounds(0,23, 360, 360);

setTitle("单人版五子棋");

setBounds(200, 200, 360, 383);

setVisible(true);

addWindowListener(this);

}

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

public void windowDeactivated(WindowEvent e){}

public void windowActivated(WindowEvent e){}

public void windowOpened(WindowEvent e){}

public void windowClosed(WindowEvent e){}

public void windowIconified(WindowEvent e){}

public void windowDeiconified(WindowEvent e){}

}

public class mywindow

{

public static void main(String argc [])

{

myframe f = new myframe();

}

}

java课程设计题目及代码是什么?

java课程设计题目及代码分别是:

1、题目:计算器。设计内容是设计一个图形界面(GUI)的计算器应用程序,完成简单的算术运算。

设计要求是设计的计算器应用程序可以完成家法、减法、乘法、除法和取余运算。且有小数点、正负号、求倒数、退格和清零功能。

2、代码:

数字按钮NumberButton类如下:

import java.awt.

import java.awt.event.

import javax.swing.

public class NumberButton extends Button.

{

int number.

public NumberButton(int number).

 {

super(""+number).

this.number=number.

setForeground(Color.blue).

}

public int getNumber().

{

return number;

}

}

其它java课程设计题目及代码是:

题目:华容道。编写一个按钮的子类,使用该子类创建的对象代表华容道中的人物。通过焦点事件控制人物颜色,当人物获得焦点时颜色为蓝色,当失去焦点时颜色为灰色。

通过键盘事件和鼠标事件来实现曹操、关羽等人物的移动。当人物上发生鼠标事件或键盘事件时,如果鼠标指针的位置是在人物的下方(也就是组件的下半部分)或按下键盘的“↓“键,该人物向下移动。向左、向右和向上的移动原理类似。

代码是:

String name[]={"曹操","关羽","张","刘","马","许","兵","兵","兵","兵"}.

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

{

person[i]=new Person(i,name[i]).

person[i].addKeyListener(this).

person[i].addMouseListener(this).

//     person[i].addFocusListener(new Person).

add(person[i]).

}

person[0].setBounds(104,54,100,100).

person[1].setBounds(104,154,100,50).

person[2].setBounds(54,154,50,100).

person[3].setBounds(204,154,50,100).

person[4].setBounds(54,54,50,100).

person[5].setBounds(204,54,50,100);

person[6].setBounds(54,254,50,50);

person[7].setBounds(204,254,50,50);

person[8].setBounds(104,204,50,50);

person[9].setBounds(154,204,50,50);

使用java编写代码如下要求

NewPhone类

package com.baidu.question;

public class NewPhone extends Phone {

    private boolean mute = true;

    @Override

    public void call() {

        if(mute){

            super.call();

        }else{

            System.out.println("语音已关闭");

        }

    }

    //这里是直接设置

    public void setMute(boolean mute){

        this.mute=mute;

    }

    //担心你的题目是要求两种方法,写的第二种,下面两个方法负责开关

    public void openMute(){

        this.mute=true;

        /*

        * 也可以这样写

        * setMute(true);

        * 下边的方法一样

        * */

    }

    public void closeMute(){

        this.mute = false;

    }

}

Phone类

package com.baidu.question;

public class Phone {

    public void call(){

        System.out.println("打电话");

    }

}

测试类

package com.baidu.question;

public class PhoneTest {

    public static void main(String[] args) {

        Phone phone = new Phone();

        phone.call();

        NewPhone newPhone = new NewPhone();

        newPhone.call();

        newPhone.setMute(false);

        newPhone.call();

        newPhone.openMute();

        newPhone.call();

        newPhone.closeMute();

        newPhone.call();

    }

}

测试结果

打电话

打电话

语音已关闭

打电话

语音已关闭

需要一份500行的java程序,期末大作业,最好带详细注释。

Java生成CSV文件简单操作实例

CSV是逗号分隔文件(Comma Separated Values)的首字母英文缩写,是一种用来存储数据的纯文本格式,通常用于电子表格或数据库软件。在 CSV文件中,数据“栏”以逗号分隔,可允许程序通过读取文件为数据重新创建正确的栏结构,并在每次遇到逗号时开始新的一栏。如:

123   1,张三,男2,李四,男3,小红,女   

Java生成CSV文件(创建与导出封装类)

package com.yph.omp.common.util;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.LinkedHashMap;

import java.util.List;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import org.junit.Test;

/**

* Java生成CSV文件

*/

public class CSVUtil {

/**

* 生成为CVS文件

*

* @param exportData

*            源数据List

* @param map

*            csv文件的列表头map

* @param outPutPath

*            文件路径

* @param fileName

*            文件名称

* @return

*/

@SuppressWarnings("rawtypes")

public static File createCSVFile(List exportData, LinkedHashMap map,

String outPutPath, String fileName) {

File csvFile = null;

BufferedWriter csvFileOutputStream = null;

try {

File file = new File(outPutPath);

if (!file.exists()) {

file.mkdir();

}

// 定义文件名格式并创建

csvFile = File.createTempFile(fileName, ".csv",

new File(outPutPath));

// UTF-8使正确读取分隔符","

csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(

new FileOutputStream(csvFile), "GBK"), 1024);

// 写入文件头部

for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator

.hasNext();) {

java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator

.next();

csvFileOutputStream

.write("\"" + (String) propertyEntry.getValue() != null ? (String) propertyEntry

.getValue() : "" + "\"");

if (propertyIterator.hasNext()) {

csvFileOutputStream.write(",");

}

}

csvFileOutputStream.newLine();

// 写入文件内容

for (Iterator iterator = exportData.iterator(); iterator.hasNext();) {

Object row = (Object) iterator.next();

for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator

.hasNext();) {

java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator

.next();

/*-------------------------------*/ 

//以下部分根据不同业务做出相应的更改

StringBuilder sbContext = new StringBuilder("");

if (null != BeanUtils.getProperty(row,(String) propertyEntry.getKey())) {

if("证件号码".equals(propertyEntry.getValue())){

//避免:身份证号码 ,读取时变换为科学记数 - 解决办法:加 \t(用Excel打开时,证件号码超过15位后会自动默认科学记数)

sbContext.append(BeanUtils.getProperty(row,(String) propertyEntry.getKey()) + "\t");

}else{

sbContext.append(BeanUtils.getProperty(row,(String) propertyEntry.getKey()));                         

}

}

csvFileOutputStream.write(sbContext.toString());

/*-------------------------------*/                 

if (propertyIterator.hasNext()) {

csvFileOutputStream.write(",");

}

}

if (iterator.hasNext()) {

csvFileOutputStream.newLine();

}

}

csvFileOutputStream.flush();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

csvFileOutputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return csvFile;

}

/**

* 下载文件

*

* @param response

* @param csvFilePath

*            文件路径

* @param fileName

*            文件名称

* @throws IOException

*/

public static void exportFile(HttpServletRequest request,

HttpServletResponse response, String csvFilePath, String fileName)

throws IOException {

response.setCharacterEncoding("UTF-8");

response.setContentType("application/csv;charset=GBK");

response.setHeader("Content-Disposition", "attachment; filename="

+ new String(fileName.getBytes("GB2312"), "ISO8859-1"));

InputStream in = null;

try {

in = new FileInputStream(csvFilePath);

int len = 0;

byte[] buffer = new byte[1024];

OutputStream out = response.getOutputStream();

while ((len = in.read(buffer)) 0) {

out.write(buffer, 0, len);

}

} catch (FileNotFoundException e1) {

System.out.println(e1);

} finally {

if (in != null) {

try {

in.close();

} catch (Exception e1) {

throw new RuntimeException(e1);

}

}

}

}

/**

* 删除该目录filePath下的所有文件

*

* @param filePath

*            文件目录路径

*/

public static void deleteFiles(String filePath) {

File file = new File(filePath);

if (file.exists()) {

File[] files = file.listFiles();

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

if (files[i].isFile()) {

files[i].delete();

}

}

}

}

/**

* 删除单个文件

*

* @param filePath

*            文件目录路径

* @param fileName

*            文件名称

*/

public static void deleteFile(String filePath, String fileName) {

File file = new File(filePath);

if (file.exists()) {

File[] files = file.listFiles();

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

if (files[i].isFile()) {

if (files[i].getName().equals(fileName)) {

files[i].delete();

return;

}

}

}

}

}

@SuppressWarnings({ "unchecked", "rawtypes" })

@Test

public void createFileTest() {

List exportData = new ArrayListMap();

Map row1 = new LinkedHashMapString, String();

row1.put("1", "11");

row1.put("2", "12");

row1.put("3", "13");

row1.put("4", "14");

exportData.add(row1);

row1 = new LinkedHashMapString, String();

row1.put("1", "21");

row1.put("2", "22");

row1.put("3", "23");

row1.put("4", "24");

exportData.add(row1);

LinkedHashMap map = new LinkedHashMap();

map.put("1", "第一列");

map.put("2", "第二列");

map.put("3", "第三列");

map.put("4", "第四列");

String path = "d:/export";

String fileName = "文件导出";

File file = CSVUtil.createCSVFile(exportData, map, path, fileName);

String fileNameNew = file.getName();

String pathNew = file.getPath();

System.out.println("文件名称:" + fileNameNew );

System.out.println("文件路径:" + pathNew );

}

}

//注:BeanUtils.getProperty(row,(String) propertyEntry.getKey()) + "\t" ,只为解决数字格式超过15位后,在Excel中打开展示科学记数问题。

Java面向对象大作业,要求过去的不优雅的正确的代码一份和修改后的优雅的正确代码

import java.lang.*;

class TestThread

{

public static void main(String args[])

{

try{

printHello ph = new PrintHello();

Thread Tph = new Thread(ph);

for(;;){

System.out.println("I'm main");

Thread.sleep(100);

}

}

catch(Exception e){

e.printStackTrace();

}

}

}

class PrintHello implements Runnable

{

public void run(){

try{

for(;;){

System.out.println("I'm hello");

}

}

catch(Exception e){

e.printStackTrace();

}

}

}

JAVA大作业

一句一句看,其实不难的

import java.util.Random;

import java.util.Scanner;

public class Test {

public static Random r = new Random();

public static void printMenu() {    //自定义的方法

System.out.println("(1) play another round");

System.out.println("(2) exit the game");

System.out.print("Selection:");

}

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

{

String content = null;

Scanner scan = new Scanner(System.in);  //

while (true)                            //true在这里使得程序一直循环

{

printMenu();                        //调用方法,打印出三句话 ...

content = scan.nextLine();          //读取输入

System.out.println();

// 输入内容是2,则退出程序

if (content.equals("2"))

break;                      //break 跳出当前的循环,也就是跳出while循环,程序往下执行,若没有可执行则结束程序

if (content.equals("1")) {

System.out.print("Text to Match: ");

int count = r.nextInt(50) + 1;   //得到1~50随机数,即[1,50]

char[] letters = new char[count]; //长度为count的char数组

for (int i = 0; i count; i++)   //i count等价i letters.length

{

letters[i] = (char) (r.nextInt(26) + 97);  //给letters[i]赋值,字母a~z

System.out.print(letters[i]);

}

System.out.print("\n               ");

long startTime = System.currentTimeMillis();   //得到开始时间

int correctCount = 0;

content = scan.nextLine();                     //得到输入的字符串

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

if (content.charAt(i) == letters[i])       //输入的字符串的每个字母和letters中的比较

correctCount++;

}

long endTime = System.currentTimeMillis();     //得到结束时间

int time = (int) ((endTime - startTime) / 1000);  //计算所花的时间

if (correctCount == 0)

System.out

.println("INCORRECTLY TYPED, YOU GET A SPEED OF 0!");

else {

System.out.println("共花了" + time + "秒,正确率为:"

+ ((int) (((double) correctCount / count)) * 100)

+ "%");

}

}

System.out.println();

}

}

}

java大作业代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于javaweb大作业带源码、java大作业代码的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-24,除非注明,否则均为首码项目网原创文章,转载请注明出处。