//都是從新手過來的,以下代碼供參考
創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:做網(wǎng)站、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的光澤網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
//1.
public?class?BankAccount?{
private?static?String?acctnum;
private?static?double?money;
private?static?void?showAcct()?{
System.out.println("賬號為:?"?+?acctnum);
}
private?static?void?showMoney()?{
System.out.println("余額為:?"?+?money);
}
public?BankAccount(String?acc,?double?m)?{
this.acctnum?=?acc;
this.money?=?m;
}
public?static?void?main(String[]?args)?{
BankAccount?ba?=?new?BankAccount("626600018888",?5000.00);
ba.showAcct();
ba.showMoney();
}
}
//2.
public?class?Triangle?{
private?static?float?a;
private?static?float?b;
private?static?float?c;
public?Triangle(float?a,?float?b,?float?c)?{
this.a?=?a;
this.b?=?b;
this.c?=?c;
}
public?static?boolean?judgeTriangle(float?a,?float?b,?float?c)?{
if?((a??Math.abs(b?-?c)??a??b?+?c)
?(b??Math.abs(a?-?c)??b??a?+?c)
?(c??Math.abs(a?-?b)??c??a?+?b))
return?true;
else
return?false;
}
public?float?getCircumference()?{
return?this.a?+?this.b?+?this.c;
}
}
//3.
public?class?TestTriangle?{
public?static?void?main(String[]?args)?{
Triangle?t?=?new?Triangle(5.3f,7.8f,9.3f);
if(t.judgeTriangle(5.3f,7.8f,9.3f)){
System.out.print("能夠成三角形,周長為:?");
System.out.printf("%9.2f",t.getCircumference());}
else
System.out.println("不能構(gòu)成三角形");
}
}
每天有時間的話 , 會回答一兩個圖形界面的問題, 但是分?jǐn)?shù)最好還是高點才有興趣.
具體代碼和詳細(xì)的注釋如下
員工類
public?class?Emp?{
private?int?num;//工號
private?String?name;//姓名
private?double?basicPay;//基本工資
private?double?meritPay;//績效工資
public?Emp(){//無參數(shù)構(gòu)造器
}
public?Emp(int?num,?String?name,?double?basicPay,?double?meritPay)?{//有參數(shù)構(gòu)造器
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{
//獲取數(shù)據(jù)
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){
//當(dāng)輸入不符合規(guī)范時?,??提示錯誤
JOptionPane.showMessageDialog(this,?"請輸入正確的數(shù)據(jù):\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();
}
}
測試效果
【ClientSocketDemo.java 客戶端Java源代碼】
import java.net.*;
import java.io.*;
public class ClientSocketDemo
{
//聲明客戶端Socket對象socket
Socket socket = null;
//聲明客戶器端數(shù)據(jù)輸入輸出流
DataInputStream in;
DataOutputStream out;
//聲明字符串?dāng)?shù)組對象response,用于存儲從服務(wù)器接收到的信息
String response[];
//執(zhí)行過程中,沒有參數(shù)時的構(gòu)造方法,本地服務(wù)器在本地,取默認(rèn)端口10745
public ClientSocketDemo()
{
try
{
//創(chuàng)建客戶端socket,服務(wù)器地址取本地,端口號為10745
socket = new Socket("localhost",10745);
//創(chuàng)建客戶端數(shù)據(jù)輸入輸出流,用于對服務(wù)器端發(fā)送或接收數(shù)據(jù)
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
//獲取客戶端地址及端口號
String ip = String.valueOf(socket.getLocalAddress());
String port = String.valueOf(socket.getLocalPort());
//向服務(wù)器發(fā)送數(shù)據(jù)
out.writeUTF("Hello Server.This connection is from client.");
out.writeUTF(ip);
out.writeUTF(port);
//從服務(wù)器接收數(shù)據(jù)
response = new String[3];
for (int i = 0; i response.length; i++)
{
response[i] = in.readUTF();
System.out.println(response[i]);
}
}
catch(UnknownHostException e){e.printStackTrace();}
catch(IOException e){e.printStackTrace();}
}
//執(zhí)行過程中,有一個參數(shù)時的構(gòu)造方法,參數(shù)指定服務(wù)器地址,取默認(rèn)端口10745
public ClientSocketDemo(String hostname)
{
try
{
//創(chuàng)建客戶端socket,hostname參數(shù)指定服務(wù)器地址,端口號為10745
socket = new Socket(hostname,10745);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
String ip = String.valueOf(socket.getLocalAddress());
String port = String.valueOf(socket.getLocalPort());
out.writeUTF("Hello Server.This connection is from client.");
out.writeUTF(ip);
out.writeUTF(port);
response = new String[3];
for (int i = 0; i response.length; i++)
{
response[i] = in.readUTF();
System.out.println(response[i]);
}
}
catch(UnknownHostException e){e.printStackTrace();}
catch(IOException e){e.printStackTrace();}
}
//執(zhí)行過程中,有兩個個參數(shù)時的構(gòu)造方法,第一個參數(shù)hostname指定服務(wù)器地址
//第一個參數(shù)serverPort指定服務(wù)器端口號
public ClientSocketDemo(String hostname,String serverPort)
{
try
{
socket = new Socket(hostname,Integer.parseInt(serverPort));
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
String ip = String.valueOf(socket.getLocalAddress());
String port = String.valueOf(socket.getLocalPort());
out.writeUTF("Hello Server.This connection is from client.");
out.writeUTF(ip);
out.writeUTF(port);
response = new String[3];
for (int i = 0; i response.length; i++)
{
response[i] = in.readUTF();
System.out.println(response[i]);
}
}
catch(UnknownHostException e){e.printStackTrace();}
catch(IOException e){e.printStackTrace();}
}
public static void main(String[] args)
{
String comd[] = args;
if(comd.length == 0)
{
System.out.println("Use localhost(127.0.0.1) and default port");
ClientSocketDemo demo = new ClientSocketDemo();
}
else if(comd.length == 1)
{
System.out.println("Use default port");
ClientSocketDemo demo = new ClientSocketDemo(args[0]);
}
else if(comd.length == 2)
{
System.out.println("Hostname and port are named by user");
ClientSocketDemo demo = new ClientSocketDemo(args[0],args[1]);
}
else System.out.println("ERROR");
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
【ServerSocketDemo.java 服務(wù)器端Java源代碼】
import java.net.*;
import java.io.*;
public class ServerSocketDemo
{
//聲明ServerSocket類對象
ServerSocket serverSocket;
//聲明并初始化服務(wù)器端監(jiān)聽端口號常量
public static final int PORT = 10745;
//聲明服務(wù)器端數(shù)據(jù)輸入輸出流
DataInputStream in;
DataOutputStream out;
//聲明InetAddress類對象ip,用于獲取服務(wù)器地址及端口號等信息
InetAddress ip = null;
//聲明字符串?dāng)?shù)組對象request,用于存儲從客戶端發(fā)送來的信息
String request[];
public ServerSocketDemo()
{
request = new String[3]; //初始化字符串?dāng)?shù)組
try
{
//獲取本地服務(wù)器地址信息
ip = InetAddress.getLocalHost();
//以PORT為服務(wù)端口號,創(chuàng)建serverSocket對象以監(jiān)聽該端口上的連接
serverSocket = new ServerSocket(PORT);
//創(chuàng)建Socket類的對象socket,用于保存連接到服務(wù)器的客戶端socket對象
Socket socket = serverSocket.accept();
System.out.println("This is server:"+String.valueOf(ip)+PORT);
//創(chuàng)建服務(wù)器端數(shù)據(jù)輸入輸出流,用于對客戶端接收或發(fā)送數(shù)據(jù)
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
//接收客戶端發(fā)送來的數(shù)據(jù)信息,并顯示
request[0] = in.readUTF();
request[1] = in.readUTF();
request[2] = in.readUTF();
System.out.println("Received messages form client is:");
System.out.println(request[0]);
System.out.println(request[1]);
System.out.println(request[2]);
//向客戶端發(fā)送數(shù)據(jù)
out.writeUTF("Hello client!");
out.writeUTF("Your ip is:"+request[1]);
out.writeUTF("Your port is:"+request[2]);
}
catch(IOException e){e.printStackTrace();}
}
public static void main(String[] args)
{
ServerSocketDemo demo = new ServerSocketDemo();
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class GameTest extends JFrame implements ActionListener{
/*
* 新建一個主面板(這個類可能是自定義的,本程序和API中沒有)。
*/
MainPanel j=new MainPanel();
JButton jPreview;
JLabel label;
Container container;
JPanel panel;
/**
* 主函數(shù)
* @param args
*/
public static void main(String[] args) {
//運行程序
new GameTest();
}
/**
* 構(gòu)造函數(shù)。
*
*/
public GameTest()
{
//新建一個標(biāo)題為“拼圖”的窗口
JFrame fr =new JFrame("拼圖");
//獲取窗口容器。
container=fr.getContentPane();
//創(chuàng)建菜單條
JMenuBar jMenuBar=new JMenuBar();
//以下初始化菜單,并且設(shè)置快捷鍵和添加監(jiān)聽器。
JMenu jMenuGame=new JMenu("游戲(G)");
jMenuGame.setMnemonic('g');
JMenuItem jMenuItemStart = new JMenuItem("開始(S)");
jMenuItemStart.setMnemonic('s');
jMenuItemStart.addActionListener(this);
JMenuItem jMenuItemExit=new JMenuItem("退出(E)");
jMenuItemExit.setMnemonic('e');
jMenuItemExit.addActionListener(this);
jMenuGame.add(jMenuItemStart);
jMenuGame.add(jMenuItemExit);
//初始化按鈕并設(shè)置快捷鍵和添加監(jiān)聽器
JButton jChoice=new JButton("選圖(X)");
jChoice.setMnemonic('x');
jChoice.addActionListener(this);
jPreview=new JButton("預(yù)覽(P)");
jPreview.setMnemonic('p');
jPreview.addActionListener(this);
//將菜單和按鈕添加到菜單條中
jMenuBar.add(jMenuGame);
jMenuBar.add(jChoice);
jMenuBar.add(jPreview);
//將菜單條設(shè)為該窗口的主菜單
fr.setJMenuBar(jMenuBar);
//將主面板添加到該窗口的容器中。
container.add(j);
//設(shè)置大小
fr.setSize(315,360 );
fr.setVisible(true);
//設(shè)置默認(rèn)關(guān)閉方式。
fr.setDefaultCloseOperation(3);
}
/**
* 事件處理函數(shù)。
*/
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="開始(S)")
{
j.Start();
}
if(e.getActionCommand()=="預(yù)覽(P)")
{
j.setVisible(false);
panel=new JPanel();
Icon icon=new ImageIcon("pictrue/pic"+"_"+MainPanel.pictureID+".jpg");
label=new JLabel(icon);
label.setBounds(300, 300, 0, 0);
panel.add(label);
panel.setSize(300, 300);
panel.setVisible(true);
this.container.add(panel);
jPreview.setText("返回(P)");
}
if(e.getActionCommand()=="返回(P)")
{
panel.setVisible(false);
j.setVisible(true);
j.repaint();
jPreview.setText("預(yù)覽(P)");
}
if(e.getActionCommand()=="退出(E)")
{
System.exit(0);
}
if(e.getActionCommand()=="選圖(X)")
{
//初始化選擇框,并提供選擇。
Choice pic = new Choice();
pic.add("七里香");
pic.add("依然范特西");
pic.add("八度空間");
pic.add("十一月的肖邦");
pic.add("魔杰座");
pic.add("葉惠美");
pic.add("我很忙");
int i=JOptionPane.showConfirmDialog(this, pic, "選擇圖片", JOptionPane.OK_CANCEL_OPTION);
if(i==JOptionPane.YES_OPTION)
{
//選擇圖片
MainPanel.pictureID=pic.getSelectedIndex()+1;
j.removeAll();
j.reLoadPicture();
j.repaint();
}
}
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class GradeStatistic {
public static void main(String[] args) {
GradeStatistic gs = new GradeStatistic();
ListMark list = new ArrayListMark();
float sum = 0;
while(true){
Scanner sc = new Scanner(System.in);
System.out.print("Please input student name: ");
String name = sc.nextLine();
if(name.equals("end")){
break;
}
System.out.print("Please input student score: ");
float score = sc.nextFloat();
sum += score;
list.add(gs.new Mark(name, score));
}
float max = list.get(0).getScore();
float min = list.get(0).getScore();
for(Mark mark: list){
if(max mark.getScore()){
max = mark.getScore();
}
if(min mark.getScore()){
min = mark.getScore();
}
}
float average = sum / list.size();
System.out.println("Average is: " + average);
System.out.println("Max is: " + max);
System.out.println("Min is: " + min);
}
private class Mark{
private String name;
private float score;
public Mark(String name, float score){
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public float getScore() {
return score;
}
}
}
----------------------
Please input student name: Zhang san
Please input student score: 100
Please input student name: Li Si
Please input student score: 91
Please input student name: Ec
Please input student score: 35
Please input student name: ma qi
Please input student score: 67
Please input student name: end
Average is: 73.25
Max is: 100.0
Min is: 35.0