每天有时间的话 , 会回答一两个图形界面的问题, 但是分数最好还是高点才有兴趣.
创新互联-专业网站定制、快速模板网站建设、高性价比武宁网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式武宁网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖武宁地区。费用合理售后完善,十载实体公司更值得信赖。
具体代码和详细的注释如下
员工类
public class Emp {
private int num;//工号
private String name;//姓名
private double basicPay;//基本工资
private double meritPay;//绩效工资
public Emp(){//无参数构造器
}
public Emp(int num, String name, double basicPay, double meritPay) {//有参数构造器
super();
this.num = num;
this.name = name;
this.basicPay = basicPay;
this.meritPay = meritPay;
}
//重写Object的toString 方法
public String toString() {
return "工号:"+num+"\t姓名:"+name+"\t基本工资:"+basicPay+"\t绩效工资"+meritPay;
}
//下面是属性的set和get
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBasicPay() {
return basicPay;
}
public void setBasicPay(double basicPay) {
this.basicPay = basicPay;
}
public double getMeritPay() {
return meritPay;
}
public void setMeritPay(double meritPay) {
this.meritPay = meritPay;
}
}
输入界面类
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class EmpFrome extends JFrame implements ActionListener {
JTextField jtfnum, jtfname, jtfbp, jtfmp;
JButton jbwtf;
public EmpFrome() {
JLabel jl1 = new JLabel("工号");
jtfnum = new JTextField(8);
add(jl1);
add(jtfnum);
JLabel jl2 = new JLabel("姓名");
jtfname = new JTextField(8);
add(jl2);
add(jtfname);
JLabel jl3 = new JLabel("基本工资");
jtfbp = new JTextField(8);
add(jl3);
add(jtfbp);
JLabel jl4 = new JLabel("绩效工资");
jtfmp = new JTextField(8);
add(jl4);
add(jtfmp);
JLabel jl5 = new JLabel();
jbwtf = new JButton("写入文件");
jbwtf.addActionListener(this);
add(jl5);
add(jbwtf);
setLayout(new GridLayout(5, 2));
setTitle("员工信息录入");
setSize(290, 230);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("写入文件")) {
try{
//获取数据
int num = Integer.parseInt(jtfnum.getText().trim());
String name = jtfname.getText().trim();
double bp = Double.parseDouble(jtfbp.getText().trim());
double mp = Double.parseDouble(jtfmp.getText().trim());
Emp emp = new Emp(num, name, bp, mp);
writeToFile(emp);
JOptionPane.showMessageDialog(this, "录入成功");//提示成功
//清空文本框
jtfnum.setText("");
jtfname.setText("");
jtfbp.setText("");
jtfmp.setText("");
}catch(Exception ex){
//当输入不符合规范时 , 提示错误
JOptionPane.showMessageDialog(this, "请输入正确的数据:\n工号整型,工资浮点型","录入错误",JOptionPane.ERROR_MESSAGE);
}
}
}
//定义的文件路径
final static String FILE_PATH = "employee.dat";
public void writeToFile(Emp emp) {//IO操作,追加写入
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(new File(FILE_PATH), true));//为true表示追加
bw.write(emp.toString());//写入员工信息
bw.newLine();//换行
} catch (IOException e) {
e.printStackTrace();
}finally{
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
测试类
public class EmpTest {
public static void main(String[] args) {
new EmpFrome();
}
}
测试效果
兄弟可以联系 hi留言也行
我这里保留了,从学习java第一天起到现在 学习练习代码,
基础部分注释很详细。
//每一位相加求和
public class Demo
{
public static void main(String[] args)
{
int i = 45678;
int sum = 0;
int c = 0;
for(int j = 4; j = 0; j--)
{
int b = (int) Math.pow(10, j);
c = i / b;
System.out.println(c);
sum += c;
System.out.println("sum=" + sum);
i %= b;
}
System.out.println(sum);
}
}
public class ZY1030
{
public static void main(String[] asgs)
{
zy1();
zy2();
zy3();
zy4();
zy5();
zy6();
zy7();
yanghuisanjiao();
}
public static void yanghuisanjiao()
{
int[][] pas = new int[6][];
for(int i = 0; i pas.length; i++)
{
pas[i] = new int[i + 1];
pas[i][0] = 1;
pas[i][i] = 1;
for(int j = 0; j pas[i].length - 1; j++)
{
if(j = 1 i 1)
pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j];
}
}
for(int i = 0; i pas.length; i++)
{
for(int j = 0; j pas[i].length; j++)
{
System.out.print(pas[i][j]);
System.out.println();
}
}
System.out.println();
}
private static void zy1()
{
System.out.println("��ҵ1");
/*
*
**
***
****
*****
******
*******
*/
for(int r = 0; r 7; r++)
{
for(int j = 0; j r + 1; j++)
{
System.out.print("*");
}
System.out.println();
}
}
private static void zy2()
{
System.out.println("\n��ҵ2");
/*
*
***
*****
*******
*********
*/
for(int r = 0; r = 5; r++)
{
for(int j = 0; j 11 - r; j++)
{
System.out.print(" ");
}
for(int j = 0; j r * 2 + 1; j++)
{
System.out.print("*");
}
System.out.println();
}
}
private static void zy3()
{
System.out.println("\n��ҵ3");
/*
*
* *
* * *
* * * *
* * * * *
*/
for(int r = 0; r = 5; r++)
{
for(int j = 0; j 10 - r; j++)
{
System.out.print(" ");
}
for(int j = 0; j r; j++)
{
System.out.print("* ");
}
System.out.println();
}
}
private static void zy4()
{
System.out.println("\n��ҵ4");
/*
***********
***********
***********
***********
***********
*/
for(int r = 0; r = 5; r++)
{
for(int j = 0; j 7 - r; j++)
{
System.out.print(" ");
}
for(int j = 0; j 10; j++)
{
System.out.print("*");
}
System.out.println();
}
}
private static void zy5()
{
System.out.println("\n��ҵ5");
/*
1X1=1
1X2=2 2X2=4
1X3=3 2X3=6 3X3=9
....
*/
for(int r = 1; r = 9; r++)
{
for(int j = 1; j = r; j++)
{
int iValue = j * r; //����ֵ
String str = j + "X" + r + "=" + iValue + " ";
System.out.print(str);
//System.out.print(" J:" + j);
}
System.out.println();
//System.out.println("R:" + r);
}
}
private static void zy6()
{
System.out.println("\n��ҵ6");
/*
������(����) ֻ�ܱ�1�ͱ����������������
3 5 7 11 13 17 19 23 ....
int n = 4;
boolean t = true; // assume is SU_SHU
for(int i=2; in; i++)
{
if(n%i==0)
{
t = false;
break;
}
}
System.out.println(t);
*/
for(int i = 2; i = 100; i++)
{
// if(i % 2 ==0 || i % 3 == 0 )
// {
// continue;
// }
// System.out.print(i + " ");
boolean bTrue = true;
for(int j = 2; j i; j++)
{
if(i % j == 0)
{
bTrue = false;
break;
}
}
if(bTrue)
{
System.out.println(i);
}
}
System.out.println();
}
private static void zy7()
{
System.out.println("\n��ҵ7");
/*
int n = 23623;
int x = n % 100000 / 10000;
System.out.println(x);
x = n % 10000 / 1000;
System.out.println(x);
x = n % 1000 / 100;
System.out.println(x);
*/
}
}
我用了半个小时 帮你写了一个简单的验证用户名和密码登陆问题 别辜负我的好意 下面是代码!(建好包和类 代码粘过去就能用)
实体类 包entity
-------------------------------------------------------------
package entity;
/**
* 用户实体类
* @author new
*
*/
public class Users {
private String name;//用户名
private String pass;//用户密码
/**
* 空的构造函数 用户实力化 此类对象
*/
public Users(){
}
/**
* 构造函数 接收用户名和密码
* @param name
* @param pass
*/
public Users(String name, String pass) {
this.name = name;
this.pass = pass;
}
/**
* 下面set和get方法就不用解释了吧
* @return
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}
数据库类 包dao(我是模拟一下数据库 没有用到数据库)
--------------------------------------------------------------
package dao;
import java.util.*;
import entity.Users;//导入实体类
/**
* 模拟数据库 用户DAO
* @author new
*
*/
public class UsersDAO {
private static Users users=new Users();
static
{
users.setName("tom");
users.setPass("jerry");
}
/**
* 根据姓名查找这个用户 (模拟一下数据库)
* @param name
* @return
*/
public Users findUserByName(String name)
{
if(name.equals(this.users.getName()))
{
return this.users;
}
return null;
}
}
业务类 包service (验证用户名和密码)
------------------------------------------------------------
package service;
import dao.UsersDAO;
import entity.Users;
/**
* 验证密码 业务类
* @author new
*
*/
public class validatePass {
//实力化DAO对象
private UsersDAO us=new UsersDAO();
/**
* 验证输入的密码是否正确
* @param name
* @param pass
* @return
*/
public Users validate(String name,String pass)
{
Users user=null;
user=us.findUserByName(name);
//如果不为空 说明查到了
if(user!=null)
{
//用查询出来对象的密码和传过来的密码比较
if(user.getPass().equals(pass))
{
return user;
}
}
return null;
}
}
最后是测试test类 包test
----------------------------------------------------------
package test;
import entity.Users;
import service.validatePass;
/**
* 测试类
* @author new
*
*/
public class test {
/**
* main方法 用于测试
* @param args
*/
public static void main(String[] args)
{
//实例化业务类对象
validatePass v=new validatePass();
//用户名和密码
String name="tom";
String pass="jerry";
//开始验证
Users user=v.validate(name, pass);
if(user==null)
{
System.out.println("你输入的用户名或密码错误!");
}else
{
System.out.println("你已经通过验证,成功登陆!");
}
}
}
package com.io2.homework;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/*压缩文件夹*/
public class MyMultipleFileZip
{
private String currentZipFilePath = "F:/MyZip.zip";
private String sourceFilePath;
private ZipOutputStream zos;
private FileInputStream fis;
public MyMultipleFileZip(String sourceFilePath)
{
try
{
this.sourceFilePath = sourceFilePath;
zos = new ZipOutputStream(new FileOutputStream(currentZipFilePath));
//设定文件压缩级别
zos.setLevel(9);
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
// 在当前条目中写入具体内容
public void writeToEntryZip(String filePath)
{
try
{
fis = new FileInputStream(filePath);
} catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
byte[] buff = new byte[1024];
int len = 0;
try
{
while ((len = fis.read(buff)) != -1)
{
zos.write(buff, 0, len);
}
} catch (IOException e)
{
e.printStackTrace();
}finally
{
if (fis != null)
try
{
fis.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
// 添加文件条目
public void addFileEntryZip(String fileName)
{
try
{
zos.putNextEntry(new ZipEntry(fileName));
} catch (IOException e)
{
e.printStackTrace();
}
}
public void addDirectoryEntryZip(String directoryName)
{
try
{
zos.putNextEntry(new ZipEntry(directoryName + "/"));
} catch (IOException e)
{
e.printStackTrace();
}
}
// 遍历文件夹
public void listMyDirectory(String filePath)
{
File f = new File(filePath);
File[] files = f.listFiles();
if(files!=null)
{
for (File currentFile : files)
{
// 设置条目名称(此步骤非常关键)
String entryName= currentFile.getAbsolutePath().split(":")[1].substring(1);
// 获取文件物理路径
String absolutePath = currentFile.getAbsolutePath();
if (currentFile.isDirectory())
{
addDirectoryEntryZip(entryName);
//进行递归调用
listMyDirectory(absolutePath);
}
else
{
addFileEntryZip(entryName);
writeToEntryZip(absolutePath);
}
}
}
}
// 主要流程
public void mainWorkFlow()
{
listMyDirectory(this.sourceFilePath);
if(zos!=null)
try
{
zos.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new MyMultipleFileZip("F:/fountainDirectory").mainWorkFlow();
}
}
第一题
import java.util.Random;
import java.util.Scanner;
public class Guess{
public static void main(String[] args) {
int rightNum = new Random().nextInt(100) + 1;
Scanner scanner = new Scanner(System.in);
int input = 0;
do{
System.out.print("清猜数字(1-100)!");
input = scanner.nextInt();
if(input rightNum){
System.out.println("猜大了!");
}
else if(input rightNum){
System.out.println("猜小了!");
}
}while(input != rightNum);
System.out.println("猜对了" + rightNum);
}
}
第二题
import java.util.* ;
public class A{
public static void main(String args[]){
int i,j,k,temp;
int a[][]=new int[2][3];
a[0][0]=(int)(100*Math.random());
a[0][1]=(int)(100*Math.random());
a[0][2]=(int)(100*Math.random());
a[1][0]=(int)(100*Math.random());
a[1][1]=(int)(100*Math.random());
a[1][2]=(int)(100*Math.random());
for(j=0;j3;j++)
System.out.println("a[0]["+j+"]="+a[0][j]);
System.out.println(" ");
for(j=0;j3;j++)
System.out.println("a[1]["+j+"]="+a[1][j]);
System.out.println(" ");
for(i=0;i2;i++){
for(j=0;j2;j++){
for(k=j;k2;k++){
if(a[i][j]a[i][k+1]){
temp=a[i][j];
a[i][j]=a[i][k+1];
a[i][k+1]=temp;
}
}
}
}
System.out.println("第一行按从小到大排列:");
for(j=0;j3;j++){
System.out.println("a[0]["+j+"]="+a[0][j]);
}
System.out.println("第二行按从小到大排列:");
for(j=0;j3;j++)
System.out.println("a[1]["+j+"]=" +a[1][j]);
}
}
春春??还不快采纳嘛