重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
Java如何實現驗證碼驗證功能呢?日常生活中,驗證碼隨處可見,他可以在一定程度上保護賬號安全,那么他是怎么實現的呢?
創新互聯堅持“要么做到,要么別承諾”的工作理念,服務領域包括:網站制作、成都做網站、企業官網、英文網站、手機端網站、網站推廣等服務,滿足客戶于互聯網時代的昆明網站設計、移動媒體設計的需求,幫助企業找到有效的互聯網解決方案。努力成為您成熟可靠的網絡建設合作伙伴!
Java實現驗證碼驗證功能其實非常簡單:用到了一個Graphics類在畫板上繪制字母,隨機選取一定數量的字母隨機生成,然后在畫板上隨機生成幾條干擾線。
首先,寫一個驗證碼生成幫助類,用來繪制隨機字母:
import?java.awt.Color;
import?java.awt.Font;
import?java.awt.Graphics;
import?java.awt.image.BufferedImage;
import?java.io.IOException;
import?java.io.OutputStream;
import?java.util.Random;
import?javax.imageio.ImageIO;
public?final?class?GraphicHelper?{
/**
*?以字符串形式返回生成的驗證碼,同時輸出一個圖片
*
*?@param?width
*????????????圖片的寬度
*?@param?height
*????????????圖片的高度
*?@param?imgType
*????????????圖片的類型
*?@param?output
*????????????圖片的輸出流(圖片將輸出到這個流中)
*?@return?返回所生成的驗證碼(字符串)
*/
public?static?String?create(final?int?width,?final?int?height,?final?String?imgType,?OutputStream?output)?{
StringBuffer?sb?=?new?StringBuffer();
Random?random?=?new?Random();
BufferedImage?image?=?new?BufferedImage(width,?height,?BufferedImage.TYPE_INT_RGB);
Graphics?graphic?=?image.getGraphics();
graphic.setColor(Color.getColor("F8F8F8"));
graphic.fillRect(0,?0,?width,?height);
Color[]?colors?=?new?Color[]?{?Color.BLUE,?Color.GRAY,?Color.GREEN,?Color.RED,?Color.BLACK,?Color.ORANGE,
Color.CYAN?};
//?在?"畫板"上生成干擾線條?(?50?是線條個數)
for?(int?i?=?0;?i??50;?i++)?{
graphic.setColor(colors[random.nextInt(colors.length)]);
final?int?x?=?random.nextInt(width);
final?int?y?=?random.nextInt(height);
final?int?w?=?random.nextInt(20);
final?int?h?=?random.nextInt(20);
final?int?signA?=?random.nextBoolean()???1?:?-1;
final?int?signB?=?random.nextBoolean()???1?:?-1;
graphic.drawLine(x,?y,?x?+?w?*?signA,?y?+?h?*?signB);
}
//?在?"畫板"上繪制字母
graphic.setFont(new?Font("Comic?Sans?MS",?Font.BOLD,?30));
for?(int?i?=?0;?i??6;?i++)?{
final?int?temp?=?random.nextInt(26)?+?97;
String?s?=?String.valueOf((char)?temp);
sb.append(s);
graphic.setColor(colors[random.nextInt(colors.length)]);
graphic.drawString(s,?i?*?(width?/?6),?height?-?(height?/?3));
}
graphic.dispose();
try?{
ImageIO.write(image,?imgType,?output);
}?catch?(IOException?e)?{
e.printStackTrace();
}
return?sb.toString();
}
}?
接著,創建一個servlet,用來固定圖片大小,以及處理驗證碼的使用場景,以及捕獲頁面生成的驗證碼(捕獲到的二維碼與用戶輸入的驗證碼一致才能通過)。
import?java.io.OutputStream;
import?javax.servlet.ServletException;
import?javax.servlet.annotation.WebServlet;
import?javax.servlet.http.HttpServlet;
import?javax.servlet.http.HttpServletRequest;
import?javax.servlet.http.HttpServletResponse;
import?javax.servlet.http.HttpSession;
@WebServlet(urlPatterns?=?"/verify/regist.do"?)
public?class?VerifyCodeServlet?extends?HttpServlet?{
private?static?final?long?serialVersionUID?=?3398560501558431737L;
@Override
protected?void?service(HttpServletRequest?request,?HttpServletResponse?response)
throws?ServletException,?IOException?{
//?獲得?當前請求?對應的?會話對象
HttpSession?session?=?request.getSession();
//?從請求中獲得?URI?(?統一資源標識符?)
String?uri?=?request.getRequestURI();
System.out.println("hello?:?"?+?uri);
final?int?width?=?180;?//?圖片寬度
final?int?height?=?40;?//?圖片高度
final?String?imgType?=?"jpeg";?//?指定圖片格式?(不是指MIME類型)
final?OutputStream?output?=?response.getOutputStream();?//?獲得可以向客戶端返回圖片的輸出流
//?(字節流)
//?創建驗證碼圖片并返回圖片上的字符串
String?code?=?GraphicHelper.create(width,?height,?imgType,?output);
System.out.println("驗證碼內容:?"?+?code);
//?建立?uri?和?相應的?驗證碼?的關聯?(?存儲到當前會話對象的屬性中?)
session.setAttribute(uri,?code);
System.out.println(session.getAttribute(uri));
}
}?
接著寫一個HTML注冊頁面用來檢驗一下:
html
head
meta?charset="UTF-8"
title注冊/title
link?rel="stylesheet"?href="styles/general.css"
link?rel="stylesheet"?href="styles/cell.css"
link?rel="stylesheet"?href="styles/form.css"
script?type="text/javascript"?src="js/ref.js"/script
style?type="text/css"?
.logo-container?{
margin-top:?50px?;
}
.logo-container?img?{
width:?100px?;
}
.message-container?{
height:?80px?;
}
.link-container?{
height:?40px?;
line-height:?40px?;
}
.link-container?a?{
text-decoration:?none?;
}
/style
/head
body
div?class="container?form-container"
form?action="/wendao/regist.do"?method="post"
div?class="form"?!--?注冊表單開始?--
div?class="form-row"
span?class="cell-1"
i?class="fa?fa-user"/i
/span
span?class="cell-11"?style="text-align:?left;"
input?type="text"?name="username"?placeholder="請輸入用戶名"
/span
/div
div?class="form-row"
span?class="cell-1"
i?class="fa?fa-key"/i
/span
span?class="cell-11"?style="text-align:?left;"
input?type="password"?name="password"?placeholder="請輸入密碼"
/span
/div
div?class="form-row"
span?class="cell-1"
i?class="fa?fa-keyboard-o"/i
/span
span?class="cell-11"?style="text-align:?left;"
input?type="password"?name="confirm"?placeholder="請確認密碼"
/span
/div
div?class="form-row"
span?class="cell-7"
input?type="text"?name="verifyCode"?placeholder="請輸入驗證碼"
/span
span?class="cell-5"?style="text-align:?center;"
img?src="/demo/verify/regist.do"?onclick="myRefersh(this)"
/span
/div
div?class="form-row"?style="border:?none;"
span?class="cell-6"?style="text-align:?left"
input?type="reset"?value="重置"
/span
span?class="cell-6"??style="text-align:right;"
input?type="submit"?value="注冊"
/span
/div
/div?!--?注冊表單結束?--
/form
/div
/body
/html
效果如下圖:
在控制臺接收到的圖片中驗證碼的變化如下:
當點擊刷新頁面的時候,驗證碼也會隨著變化,但我們看不清驗證碼時,只要點擊驗證碼就會刷新,這樣局部的刷新可以用JavaScript來實現。
在img
src="/demo/verify/regist.do"中,添加一個問號和一串后綴數字,當刷新時讓后綴數字不斷改變,那么形成的驗證碼也會不斷變化,我們可以采用的一種辦法是后綴數字用date代替,date獲取本機時間,時間是隨時變的,這樣就保證了刷新驗證碼可以隨時變化。
代碼如下:
function?myRefersh(?e?)?{
const?source?=?e.src?;?//?獲得原來的?src?中的內容
//console.log(?"source?:?"?+?source??)?;
var?index?=?source.indexOf(?"?"?)?;??//?從?source?中尋找???第一次出現的位置?(如果不存在則返回?-1?)
//console.log(?"index?:?"?+?index??)?;
if(?index??-1?)?{?//?如果找到了????就進入內部
var?s?=?source.substring(?0?,?index?)?;?//?從?source?中截取?index?之前的內容?(?index?以及?index?之后的內容都被舍棄?)
//console.log(?"s?:?"?+?s??)?;
var?date?=?new?Date();?//?創建一個?Date?對象的?一個?實例
var?time?=?date.getTime()?;?//?從?新創建的?Date?對象的實例中獲得該時間對應毫秒值
e.src?=?s?+?"?time="?+?time?;?//?將?加了?尾巴?的?地址?重新放入到?src?上
//console.log(?e.src?)?;
}?else?{
var?date?=?new?Date();
e.src?=?source?+?"?time="?+?date.getTime();
}
}
如回答不詳細可追問
如何用70行Java代碼實現神經網絡算法
import java.util.Random;
public class BpDeep{
public double[][] layer;//神經網絡各層節點
public double[][] layerErr;//神經網絡各節點誤差
public double[][][] layer_weight;//各層節點權重
public double[][][] layer_weight_delta;//各層節點權重動量
public double mobp;//動量系數
public double rate;//學習系數
public BpDeep(int[] layernum, double rate, double mobp){
this.mobp = mobp;
this.rate = rate;
layer = new double[layernum.length][];
layerErr = new double[layernum.length][];
layer_weight = new double[layernum.length][][];
layer_weight_delta = new double[layernum.length][][];
Random random = new Random();
for(int l=0;llayernum.length;l++){
layer[l]=new double[layernum[l]];
layerErr[l]=new double[layernum[l]];
if(l+1layernum.length){
layer_weight[l]=new double[layernum[l]+1][layernum[l+1]];
layer_weight_delta[l]=new double[layernum[l]+1][layernum[l+1]];
for(int j=0;jlayernum[l]+1;j++)
for(int i=0;ilayernum[l+1];i++)
layer_weight[l][j][i]=random.nextDouble();//隨機初始化權重
}
}
}
//逐層向前計算輸出
public double[] computeOut(double[] in){
for(int l=1;llayer.length;l++){
for(int j=0;jlayer[l].length;j++){
double z=layer_weight[l-1][layer[l-1].length][j];
for(int i=0;ilayer[l-1].length;i++){
layer[l-1][i]=l==1?in[i]:layer[l-1][i];
z+=layer_weight[l-1][i][j]*layer[l-1][i];
}
layer[l][j]=1/(1+Math.exp(-z));
}
}
return layer[layer.length-1];
}
//逐層反向計算誤差并修改權重
public void updateWeight(double[] tar){
int l=layer.length-1;
for(int j=0;jlayerErr[l].length;j++)
layerErr[l][j]=layer[l][j]*(1-layer[l][j])*(tar[j]-layer[l][j]);
while(l--0){
for(int j=0;jlayerErr[l].length;j++){
double z = 0.0;
for(int i=0;ilayerErr[l+1].length;i++){
z=z+l0?layerErr[l+1][i]*layer_weight[l][j][i]:0;
layer_weight_delta[l][j][i]= mobp*layer_weight_delta[l][j][i]+rate*layerErr[l+1][i]*layer[l][j];//隱含層動量調整
layer_weight[l][j][i]+=layer_weight_delta[l][j][i];//隱含層權重調整
if(j==layerErr[l].length-1){
layer_weight_delta[l][j+1][i]= mobp*layer_weight_delta[l][j+1][i]+rate*layerErr[l+1][i];//截距動量調整
layer_weight[l][j+1][i]+=layer_weight_delta[l][j+1][i];//截距權重調整
}
}
layerErr[l][j]=z*layer[l][j]*(1-layer[l][j]);//記錄誤差
}
}
}
public void train(double[] in, double[] tar){
double[] out = computeOut(in);
updateWeight(tar);
}
}
1. 選擇一個算法(提供選擇見下),利用各種方法(圖形、動畫等)演示算法的演示過程。
2. 可以進行手動演示,也可以自動步進式演示。
3. 允許用戶設置算法的各個輸入參數,以及自動步進式演示中的時間間隔。
4. 不同的算法輸入要求見下。
界面要求:
1. 盡量使用圖形界面實現,要符合日常軟件使用規范來設計菜單和界面。
2. 如果無法實現圖形界面,則在命令行方式下也需要提供菜單,方便用戶操作。
其他要求:
1. 標識符命名遵循Windows命名規范。
2. 能夠注意各種異常處理,注重提高程序運行效率。
提交內容:
1. 全部源代碼。
2. 軟件設計和使用說明書(UML類圖;實現的功能、主要技術;使用幫助文檔)
參考算法:
1. 最小生成樹算法:Prim算法、Kruskal算法。允許以下方式輸入一個圖形:繪制圖形、輸入鄰接矩陣、輸入邊及其關聯的頂點。要求在圖形方式下進行演示算法執行步驟。
2. 單源最短路算法:Dijkstra算法。允許以下方式輸入一個圖形:繪制圖形、輸入鄰接矩陣、輸入邊及其關聯的頂點。要求在圖形方式下進行演示算法執行步驟。
3. 最優編碼算法:Huffman編碼算法。允許用戶輸入一段英文文字,或者打開一個txt文檔(英文內容),據此文檔內容進行編碼。要求動態列出每個字符的出現概率統計結果以及對應編碼。
4. 其他可供演示的具有一定難度的算法,如關鍵路徑問題、有向圖的極大連通分支等。
沒java的 發段源代碼給你 有興趣自己慢慢理解
#include time.h
#include dos.h
#include math.h
#include conio.h
#include stdio.h
#include stdlib.h
#include time.h
#include graphics.h
#define ESC 0x1b
#define ENTER 0x0d
#define TRUE 1
#define FALSE 0
/*每隔TIME秒就變換一次優先級*/
#define TIME 5
/*數據結構*/
/****************************************************************/
enum _Status/*進程狀態枚舉*/
{
READY =0,/*就緒*/
RUN,/*執行中*/
SUSPEND,/*掛起*/
};
typedef enum _Status Status;
/****************************************************************/
struct _Pcb/*進程結構*/
{
int PID;/*進程ID,ID為負數的進程為系統后備隊列的作業*/
int Time;/*進程運行需要的時間*/
int Prior;/*進程的優先級,越大越優先*/
Status Sts;/*狀態*/
struct _Pcb *Next;/*指向本進程隊列中下個進程的PCB*/
};
typedef struct _Pcb PCB;
/****************************************************************/
struct _Batch/*多道處理中的道結構*/
{
PCB *pcb;/*該道當前正在處理的進程*/
struct _Batch *Next;/*下一道*/
};
typedef struct _Batch Batch;
/****************************************************************/
/*多道系統相關全局變量*/
PCB *ProcQueue = NULL;/*進程鏈表,按優先級從大到小排列*/
Batch *BatchQueue = NULL;/*系統多道鏈表*/
/****************************************************************/
/*動態優先權搶占式調度算法及相關函數聲明*/
/****************************************************************/
int InitBatchs(int n);/*初始化多道系統,n為道數*/
int InsertProc(int prior, int time);/*向進程鏈表中按優先級大小插入一個新進程*/
int InsertIDLE();/*向進程隊列中加入后備進程,當系統空閑時將被調入*/
int SortProcQueue();/*將進程鏈表按優先級從大到小排列*/
int AddBatch();/*增加系統道數*/
int DeleteBatch();/*減少系統道數*/
int UnSuspendProc(int id);/*解除ID為id的進程的掛起狀態*/
int UpdateBatchs();/*多道系統根據進程鏈表進行更新,并將執行完畢的進程刪除*/
int PassSeconds(int n);/*系統經過n秒后計算數據并進行優先級調度*/
/****************************************************************/
/*各函數的定義*/
/****************************************************************/
int InitBatchs(int n)
{
int i;
for (i=0; in; ++i)
{
AddBatch();
}
return (UpdateBatchs());
}
int InsertProc(int prior, int time)
{
static int sysid = 0;/*該系統已經加入過多少進程,此值將是新進程的ID*/
PCB *last,*now,*pcb;
pcb = (PCB*)malloc(sizeof(PCB));
if (pcb == NULL) return FALSE;
pcb-Prior = prior;
pcb-Time = time;
pcb-PID = (++sysid);
pcb-Sts = READY;
if (ProcQueue == NULL)/*如果進程隊列為空*/
{
ProcQueue = pcb;
pcb-Next = NULL;
return TRUE;
}
last = ProcQueue;
now = last-Next;
if (pcb-Prior last-Prior)/*pcb將排在隊頭*/
{
pcb-Next = ProcQueue;
ProcQueue = pcb;
return TRUE;
}
while ((now != NULL) (pcb-Prior now-Prior))/*尋找插入位置*/
{
last = now;
now = last-Next;
}
last-Next = pcb;
pcb-Next = now;
return TRUE;
}
int InsertIDLE()
{
PCB *now = ProcQueue;
PCB *idle = (PCB*)malloc(sizeof(PCB));
if (idle == NULL) return FALSE;
idle-PID = -1;
idle-Prior = -1;
idle-Sts = SUSPEND;
idle-Time = -1;
idle-Next = NULL;
if (ProcQueue == NULL)
{
ProcQueue = idle;
return TRUE;
}
while(now-Next != NULL)
{
now = now-Next;
}
now-Next = idle;
return TRUE;
}
int SortProcQueue()
{ /*冒泡排序*/
PCB *last, *now;
int b = FALSE;/*上次遍歷是否無交換產生*/
if (ProcQueue==NULL || ProcQueue-Next==NULL)/*如果鏈表中無進程或只有一個進程*/
return FALSE;
while (!b)
{
b = TRUE;
last=ProcQueue;
now=last-Next;
if (last-Prior now-Prior)
{
ProcQueue = now;
last-Next = now-Next;
now-Next = last;
b = FALSE;
last = ProcQueue;
now = last-Next;
}
while (now-Next!=NULL)
{
if ((now-Prior)(now-Next-Prior))
{
last-Next = now-Next;
now-Next = now-Next-Next;
last-Next-Next = now;
b = FALSE;
}
else
last = last-Next;
now = last-Next;
}
}
return TRUE;
}
int AddBatch()
{
Batch *bt = (Batch*)malloc(sizeof(Batch));
if (bt==NULL) return FALSE;
bt-Next = BatchQueue;
BatchQueue = bt;
bt-pcb = NULL;
return (InsertIDLE());
}
int DeleteBatch()
{
Batch *bt = BatchQueue;
PCB *last, *now;
if (BatchQueue==NULL || BatchQueue-Next==NULL)/*如果只剩最后一道則不刪除*/
return FALSE;
if (ProcQueue==NULL || ProcQueue-Next==NULL)/*如果只有最后一個后備空閑進程*/
return FALSE;/**/
last = ProcQueue;
now = last-Next;
while (now-Next != NULL)/*查找到最后一個進程,該進程必定是后備空閑進程*/
{
last = now;
now = last-Next;
}
if (now==NULL || now-PID=0)/*未查找到后備進程*/
return FALSE;/**/
free(now);
last-Next = NULL;
BatchQueue = BatchQueue-Next;
free(bt);
return TRUE;
}
int UnSuspendProc(int id)
{
PCB *now = ProcQueue;
if (ProcQueue==NULL) return FALSE;
while (now != NULL)
{
if (now-PID == id)
{
now-Sts = READY;
return TRUE;
}
}
return FALSE;
}
int UpdateBatchs()
{
Batch *bt = BatchQueue;
PCB *last = ProcQueue, *now;
while (bt != NULL)
{
bt-pcb = NULL;
bt = bt-Next;
}
if (ProcQueue == NULL) return TRUE;
while (last-Sts==RUN last-PID=0 last-Time=0)
{
ProcQueue = ProcQueue-Next;
free(last);
last = ProcQueue;
}
now = last-Next;
while (now != NULL)
{
if (now-Sts==RUN now-PID=0 now-Time=0)/*如果該進程是運行中的一般進程并已執行完畢*/
{
last-Next = now-Next;
free(now);
}
else
last = last-Next;
now = last-Next;
}
bt = BatchQueue;
now = ProcQueue;
while (bt != NULL now != NULL)
{
bt-pcb = now;
now-Sts = RUN;
bt = bt-Next;
now = now-Next;
}
while (now != NULL)
{
if (now-Sts == RUN)
{
now-Sts = SUSPEND;
}
now = now-Next;
}
return TRUE;
}
int PassSeconds(int n)
{
static int time = 0;
int i=0, ProcEnd = FALSE;
PCB *pcb = ProcQueue;
Batch *bt = BatchQueue;
if (bt == NULL) return FALSE;
time += n;
if (time=TIME)
{
i = time/TIME;/*經過多少時間段*/
time = time%TIME;
}
while (bt != NULL)/*更新進程運行時間*/
{
if (bt-pcb-PID=0)
{
bt-pcb-Time -= n;
if (bt-pcb-Time = 0)/*進程結束*/
{
ProcEnd = TRUE;
}
}
bt = bt-Next;
}
if (i 0)
{
while (pcb != NULL)/*更新進程優先權(動態優先權)*/
{
if (pcb-Sts == RUN pcb-PID=0)/*運行的進程優先權降低*/
{
pcb-Prior -= i;
if (pcb-Prior 0)
pcb-Prior = 0;
}
else if (pcb-Sts == SUSPEND pcb-PID=0)/*掛起的進程優先權升高*/
pcb-Prior += i;
pcb = pcb-Next;
}
}
if (i0)
SortProcQueue();/*如果優先級有變動則重新排序*/
if (ProcEnd || i0)
{
UpdateBatchs();/*更新多道進程*/
}
return TRUE;
}
/****************************************************************/
/*圖形界面相關函數*/
/****************************************************************/
/*表格的單位寬度和高度*/
#define WIDTH 64
#define HEIGHT 12
void *black=NULL;/*背景色方格,使用它擦出表格中的圖形*/
int InitGraph()/*初始化圖形界面*/
{
int GraphDriver; /* The Graphics device driver */
int GraphMode; /* The Graphics mode value */
int ErrorCode;
GraphDriver = DETECT; /* Request auto-detection */
initgraph( GraphDriver, GraphMode, "" );
ErrorCode = graphresult(); /* Read result of initialization*/
if( ErrorCode != grOk )
{ /* Error occured during init */
printf(" Graphics System Error: %s\n", grapherrormsg( ErrorCode ) );
getch();
return FALSE;
}
cleardevice();
black = (void*)malloc(imagesize(1,1,WIDTH-1,HEIGHT-1));
getimage(1,1,WIDTH-1,HEIGHT-1,black);
DrawFrame();
DrawData();
return TRUE;
}
int DrawFrame()/*畫邊框和表頭*/
{
settextjustify(CENTER_TEXT, CENTER_TEXT);
gprintf(320, HEIGHT/2-1, "Multi-Batch System Emulation");
settextjustify(LEFT_TEXT, TOP_TEXT);
moveto(0,HEIGHT);
lineto(0,479);
lineto(639,479);
lineto(639,HEIGHT);
lineto(0,HEIGHT);
line(WIDTH*4,HEIGHT,WIDTH*4,479);
line(WIDTH*7,HEIGHT,WIDTH*7,479);
line(0,HEIGHT*2,639,HEIGHT*2);
line(0,HEIGHT*3,639,HEIGHT*3);
gprintf(HEIGHT*0+2,HEIGHT*1+2,"System Batchs");/*系統多道列表頭*/
gprintf(HEIGHT*0+2,HEIGHT*2+2,"Batch");
gprintf(WIDTH*1+2,HEIGHT*2+2,"ProcID");
gprintf(WIDTH*2+2,HEIGHT*2+2,"Time");
gprintf(WIDTH*3+2,HEIGHT*2+2,"Prior");
gprintf(WIDTH*4+2,HEIGHT*1+2,"Suspended Processes");/*掛起隊列列表頭*/
gprintf(WIDTH*4+2,HEIGHT*2+2,"ProcID");
gprintf(WIDTH*5+2,HEIGHT*2+2,"Time");
gprintf(WIDTH*6+2,HEIGHT*2+2,"Prior");
gprintf(WIDTH*7+2,HEIGHT*1+2,"Ready Processes");/*就緒隊列列表頭*/
gprintf(WIDTH*7+2,HEIGHT*2+2,"ProcID");
gprintf(WIDTH*8+2,HEIGHT*2+2,"Time");
gprintf(WIDTH*9+2,HEIGHT*2+2,"Prior");
}
int DrawData()/*繪制系統數據*/
{
int numRun=0, numSus=0, numRed=0;/*運行掛起和就緒的進程各有多少*/
PCB* now = ProcQueue;
int x=0, y=0;
while (now != NULL)
{
switch(now-Sts)
{
case RUN:
x = WIDTH*1;
y = HEIGHT*(3+(numRun++));
break;
case SUSPEND:
x = WIDTH*4;
y = HEIGHT*(3+(numSus++));
break;
case READY:
x = WIDTH*7;
y = HEIGHT*(3+(numRed++));
break;
}
if (now-Sts==RUN)/*該進程為正在運行的進程*/
{
putimage(x-WIDTH+1,y+1,black,COPY_PUT);
gprintf(x-WIDTH+2,y+2,"%d",numRun);
}
if (now-PID=0)/*該進程不是后備進程*/
{
putimage(x+1,y+1,black,COPY_PUT);
gprintf(x+2,y+2,"%d",now-PID);
putimage(x+1+WIDTH,y+1,black,COPY_PUT);
gprintf(x+WIDTH+2,y+2,"%d",now-Time);
putimage(x+1+WIDTH*2,y+1,black,COPY_PUT);
gprintf(x+WIDTH*2+2,y+2,"%d",now-Prior);
}
else
{
putimage(x+1,y+1,black,COPY_PUT);
putimage(x+1+WIDTH,y+1,black,COPY_PUT);
putimage(x+1+WIDTH*2,y+1,black,COPY_PUT);
gprintf(x+2,y+2,"system idle process");
}
now = now-Next;
}
}
int DlgGetNum(char *buf,int l,int t,int r,int b,int gettime)
{
char ch;
int pos=0;
bar(l,t,r,b);
gprintf(l+10,t+5,"Add new Process");
if (gettime)
gprintf(l+10,t+20,"input the time:");
else
gprintf(l+10,t+20,"input the priority:");
while (1)
{
ch = getch();
if (ch == ENTER)/*如果輸入了回車鍵*/
{
if(pos!=0)/*如果位置不在第一位(回車鍵不準第一個輸入)*/
{
buf[pos]='\0';
break;
}
}
else if (ch='0' ch='9')
{
buf[pos++]=ch;
buf[pos]='\0';
}
else if (ch == ESC)
{
return FALSE;
}
else/*其他按鍵均按BackSpace處理*/
{
--pos;
buf[pos]='\0';
}
if (pos0)
{ pos=0; buf[pos]='\0';}
else if (pos4)/*最多輸入4位數*/
{ pos=4; buf[pos]='\0';}
bar(l,t+35,r,t+47);
gprintf(l+50,t+35,buf);
}
return TRUE;
}
int NewProcDlg()
{
int l=200,t=150,r=380,b=200,pos=0,prior=0,time=0;
char buf[5],ch;
PCB *pcb;
void* bg = (void*)malloc(imagesize(l,t,r,b));
getimage(l,t,r,b,bg);
setfillstyle(1,2);
flushall();
/*******輸入優先級**********/
if (!DlgGetNum(buf,l,t,r,b,FALSE))
goto exit;
prior = atoi(buf);
/*******輸入時間**********/
pos=0;
buf[pos]='\0';
if (!DlgGetNum(buf,l,t,r,b,TRUE))
goto exit;
time = atoi(buf);
InsertProc(prior, time);
exit:
putimage(l,t,bg,COPY_PUT);
free(bg);
return TRUE;
}
int gprintf( int xloc, int yloc, char *fmt, ... )/*圖形系統中的格式輸出*/
{
va_list argptr; /* Argument list pointer */
char str[140]; /* Buffer to build sting into */
int cnt; /* Result of SPRINTF for return */
va_start( argptr, format ); /* Initialize va_ functions */
cnt = vsprintf( str, fmt, argptr ); /* prints string to buffer */
outtextxy( xloc, yloc, str ); /* Send string in graphics mode */
va_end( argptr ); /* Close va_ functions */
return( cnt ); /* Return the conversion count */
}
/****************************************************************/
/*main函數*/
int main()
{
clock_t start=0, end=0;
char kb;
InitBatchs(3);/*初始化為3道系統*/
InitGraph();
while (1)
{
start = end = clock();
while (!kbhit())
{
start = clock();
if ((start-end)/18.2 = 1)/*時間過了一秒*/
{
start = end = clock();
PassSeconds(1);
cleardevice();
DrawFrame();
DrawData();
}
}
kb = getch();
switch (kb)
{
case ESC:
closegraph();
return 0;
case 'w':
AddBatch();
UpdateBatchs();
cleardevice();
DrawFrame();
DrawData();
break;
case 's':
DeleteBatch();
UpdateBatchs();
cleardevice();
DrawFrame();
DrawData();
break;
case 'i':
NewProcDlg();
UpdateBatchs();
DrawFrame();
DrawData();
break;
}
}
return 0;
}
創建一個名字為“ReportCard”的類,然后用下邊的內容全部替換掉,你會成為全班最亮的仔。
import java.util.HashMap;
/**
* 學生成績單
*/
public class ReportCard {
public static void main(String[] args) {
ReportCard reportCard = new ReportCard("張三", "070602213");
reportCard.set("語文", 80.0);
reportCard.set("數學", 59.5);
reportCard.set("英語", 66.0);
reportCard.set("java", 80, 99.0);
reportCard.set("數據庫", 80, 66.0);
reportCard.set("毛概", null);
System.out.println(reportCard.getStudentName() + "語文分數:" + reportCard.get("語文"));
System.out.println(reportCard.getStudentName() + "數學考核結果:" + (reportCard.isPassed("數學") ? "合格" : "不合格"));
System.out.println(reportCard.getStudentName() + "期末是否掛科:" + (reportCard.isAllPassed() ? "否" : "是"));
}
// 學生姓名
private String studentName;
// 學生學號
private String studentNumber;
// 成績單
private HashMapString, CourseResult cards = new HashMap();
public ReportCard() {
}
public ReportCard(String studentName, String studentNumber) {
this.studentName = studentName;
this.studentNumber = studentNumber;
}
public Double get(String courseName){
CourseResult courseResult = cards.get(courseName);
return courseResult == null ? Double.NaN : courseResult.getStudentScore();
}
public void set(String courseName, Double studentScore){
CourseResult courseResult = new CourseResult(courseName, studentScore);
cards.put(courseName, courseResult);
}
public void set(String courseName, double passMark, Double studentScore){
CourseResult courseResult = new CourseResult(courseName, passMark, studentScore);
cards.put(courseName, courseResult);
}
public boolean isPassed(String courseName){
return cards.get(courseName).isPassed();
}
public boolean isAllPassed(){
for(CourseResult cr : cards.values()){
if ( ! cr.isPassed()) {
return false;
}
}
return true;
}
public String getStudentName() {
return studentName;
}
public String getStudentNumber() {
return studentNumber;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}
/**
* 課程
*/
class Course{
// 課程名稱
protected String courseName;
// 及格分
protected double passMark = 60;
public Course(String courseName, Double passMark) {
this.courseName = courseName;
if ( passMark != null) {
this.passMark = passMark;
}
}
}
/**
* 課程成績
*/
class CourseResult extends Course{
// 學生成績
private Double studentScore;
public CourseResult(String courseName, Double studentScore) {
this(courseName, null, studentScore);
}
public CourseResult(String courseName, Double passMark, Double studentScore) {
super(courseName, passMark);
this.studentScore = studentScore == null ? Double.NaN : studentScore;
}
public boolean isPassed(){
return studentScore = passMark;
}
public String getCourseName() {
return courseName;
}
public double getPassMark() {
return passMark;
}
public Double getStudentScore() {
return studentScore;
}
}