新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.util.Arrays;
成都创新互联公司专注骨干网络服务器租用10余年,服务更有保障!服务器租用,绵阳服务器托管 成都服务器租用,成都服务器托管,骨干网络带宽,享受低延迟,高速访问。灵活、实现低成本的共享或公网数据中心高速带宽的专属高性能服务器。
class YG implements ComparableYG{//员工类,实现比较接口
String name;//姓名
int gz;//工资
public YG(String name, int gz) {
this.name = name;
this.gz = gz;
}
public String toString() {
return "姓名:"+name+"\t工资:"+gz;
}
public int compareTo(YG o) {//通过这个方法, 实现按工资排序
return o.gz-this.gz;//从大到小
}
}
public class MyTest {
public static void main(String[] args) {
YG[] ary = new YG[5];
ary[0] = new YG("张三", 5260);
ary[1] = new YG("赵四", 6000);
ary[2] = new YG("王五", 5500);
ary[3] = new YG("唐六", 5800);
ary[4] = new YG("杨七", 5200);
Arrays.sort(ary);//排序
double sum = 0;
for (YG yg : ary) {
sum+=yg.gz;
}
System.out.println("最高工资:"+ary[0]+"\n平均工资:"+sum/ary.length);
}
}
输出
最高工资:姓名:赵四 工资:6000
平均工资:5552.0
对数据库进行增删改查?
以下是 sql server 2013+java.实现的是对MSC对象的增删改查.
需要下载连接驱动程序:com.microsoft.sqlserver.jdbc.SQLServerDriver
网上搜一下就行
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
class MSC
{
public String MscID;
public String MscName;
public String MscCompany;
public float MscLongitude;
public float MscLatitude;
public float MscAltitude;
public MSC(String MscID, String MscName, String MscCompany,
float MscLongitude, float MscLatitude,float MscAltitude){
this.MscID = MscID;
this.MscName = MscName;
this.MscCompany = MscCompany;
this.MscLongitude =MscLongitude;
this.MscLatitude = MscLatitude;
this.MscAltitude = MscAltitude;
}
}
public class sqlserverjdbc {
public Connection getConnection(){
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=gsm"; //连接服务器和数据库sample
String userName = "sa"; //默认用户名
String userPwd = "123"; //密码
Connection dbConn = null;
try {
Class.forName(driverName);
dbConn =DriverManager.getConnection(dbURL, userName, userPwd);
} catch (Exception e) {
e.printStackTrace();
}
return dbConn;
}
public void printUserInfo(){
Connection con = getConnection();
Statement sta = null;
ResultSet rs = null;
System.out.println("打印表格MSC信息");
try {
sta = con.createStatement();
rs = sta.executeQuery("select * from MSC信息");
System.out.println("MscID\tMscName\tMscCompany\tMscLongitude\tMscLatitude\tMscAltitude");
while(rs.next()){
System.out.println(rs.getString("MscID")+"\t"+
rs.getString("MscName")+"\t"+
rs.getString("MscCompany")+"\t"+
rs.getFloat("MscLongitude")+"\t"+
rs.getFloat("MscLatitude")+"\t"+
rs.getFloat("MscAltitude"));
}
con.close();
sta.close();
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("打印完成\n");
}
public void delete(String MscID){
Connection con = getConnection();
String sql = "delete from MSC信息 where MscID = " + MscID;
PreparedStatement pst;
System.out.println("删除表格MSC信息中 ID = "+MscID+"的记录");
try {
pst = con.prepareStatement(sql);
pst.execute();
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("记录删除失败!!!");
}
System.out.println("记录删除成功!!!\n");
}
public void insert(MSC msc){
Connection con = getConnection();
String sql = "insert into MSC信息 values(?,?,?,?,?,?)";
PreparedStatement pst;
System.out.println("插入一条记录");
try {
pst = con.prepareStatement(sql);
pst.setString(1, msc.MscID);
pst.setString(2, msc.MscName);
pst.setString(3, msc.MscCompany);
pst.setFloat(4, msc.MscLongitude);
pst.setFloat(5, msc.MscLatitude);
pst.setFloat(6, msc.MscAltitude);
pst.execute();
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("插入失败!!!");
}
System.out.println("插入成功!!!\n");
}
//更新MscID的MscName
public void updateMscName(String MscID, String MscName){
Connection con = getConnection();
String sql = "update MSC信息 set MscName = ? where MscID = ?";
PreparedStatement pst;
System.out.println("修改一条记录");
try {
pst = con.prepareStatement(sql);
pst.setString(1, MscName);
pst.setString(2, MscID);
pst.execute();
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("修改失败!!!");
}
System.out.println("修改成功!!!\n");
}
public static void main(String args[]){
sqlserverjdbc sql = new sqlserverjdbc();
sql.printUserInfo();
sql.delete("1111");
sql.printUserInfo();
sql.updateMscName("5215", "联想");
sql.printUserInfo();
sql.insert(new MSC("1111", "中兴" ," 中兴", (float)12.2, (float)3.4,(float)45.5));
sql.printUserInfo();
}
}
person类:
public abstract class Person {
public double pay; // 总工资
public int hour; // 课时
public double countPay(int hour) {
return pay;
}
}
助教类:
public class Assistant extends Person {
public final double BASE_PAY = 800; // 基本工资
public final double HOUR_PAY = 25; // 每课时的费用
public double countPay(int hour) {
pay = BASE_PAY + hour * HOUR_PAY;
return pay;
}
}
讲师类:
public class Instructor extends Person {
public final double BASE_PAY = 1000; // 基本工资
public final double HOUR_PAY = 35; // 每课时的费用
public double countPay(int hour) {
pay = BASE_PAY + hour * HOUR_PAY;
return pay;
}
}
副教授类:
public class AssistantProfesson extends Person {
public final double BASE_PAY = 1200; // 基本工资
public final double HOUR_PAY = 40; // 每课时的费用
public double countPay(int hour) {
pay = BASE_PAY + hour * HOUR_PAY;
return pay;
}
}
教授类:
public class Professor extends Person {
public final double BASE_PAY = 1400; // 基本工资
public final double HOUR_PAY = 50; // 每课时的费用
public double countPay(int hour) {
pay = BASE_PAY + hour * HOUR_PAY;
return pay;
}
}
测试类:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Test {
public static void main(String[] args) {
System.out.println("人员类型如下:");
System.out.println("1 = 助教\r\n2 = 讲师\r\n3 = 副教授\r\n4 = 教授");
System.out.print("请选择:");
BufferedReader personType = new BufferedReader(new InputStreamReader(
System.in));
String type = null;
int hour = 0;
try {
type = personType.readLine();
} catch (Exception e) {
e.printStackTrace();
}
if (type.matches("[1-4]{1}")) {
switch (Integer.valueOf(type)) {
case 1:
hour = getHour();
if(hour == 0){return;}
Person p1 = new Assistant();
double pay1 = p1.countPay(hour);
System.out.println("助教工作" + hour + "课时的工资为:" + pay1);
break;
case 2:
hour = getHour();
if(hour == 0){return;}
Person p2 = new Instructor();
double pay2 = p2.countPay(hour);
System.out.println("讲师工作" + hour + "课时的工资为:" + pay2);
break;
case 3:
hour = getHour();
if(hour == 0){return;}
Person p3 = new AssistantProfesson();
double pay3 = p3.countPay(hour);
System.out.println("副教授工作" + hour + "课时的工资为:" + pay3);
break;
case 4:
hour = getHour();
if(hour == 0){return;}
Person p4 = new Professor();
double pay4 = p4.countPay(hour);
System.out.println("教授工作" + hour + "课时的工资为:" + pay4);
break;
}
} else {
System.out.println("输入数据错误!程序提前推出!");
return;
}
}
public static int getHour() {
System.out.print("请输入工作时间:");
BufferedReader hours = new BufferedReader(new InputStreamReader(
System.in));
String strHour = null;
int hour = 0;
try {
strHour = hours.readLine();
} catch (Exception e) {
e.printStackTrace();
}
if (strHour.matches("^[0-9]+?")) {
hour = Integer.parseInt(strHour);
} else {
System.out.println("输入参数不正确!程序提前推出!");
}
return hour;
}
}
代码一
public double earnings()
{double a=50000.456;
return a;
}
代码二
public double earnings()
{double b=2000;
return 12*b;}
代码三
public double earnings()
{double c=500;
return 52*c;}
代码四
我也正在思考中