測試代碼 :

創(chuàng)新互聯(lián)主營若羌網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都APP應(yīng)用開發(fā),若羌h5微信小程序開發(fā)搭建,若羌網(wǎng)站營銷推廣歡迎若羌等地區(qū)企業(yè)咨詢
import java.util.regex.*;
class Test {
public static void main(String args[]) {
Pattern p;
Matcher m;
String patternString = "\\dA\\d"; // 給出一個正則表達(dá)式
String matchedString = "0A1A2A3A4A5A6A7A8A9"; // 給出待匹配的字符序列
p = Pattern.compile(patternString); // 用模式patternString初試化模式對象p
m = p.matcher(matchedString); // 用matchedString初始化匹配對象m
while (m.find()) {
String str = m.group();
System.out.print("從" + m.start() + "到" + m.end() + "匹配模式子序列:");
System.out.println(str); // 查找matchedString中和patternString匹配的全部子字符串,并輸出
// 這些子字符串,以及它們在matchedString中的起止位置
}
String temp = m.replaceAll("####");
System.out.println(temp);
System.out.println(matchedString);
matchedString = ("9A00A3"); // 重新給出待匹配的字符序列
m = p.matcher("9A00A3"); // 重新初始化匹配對象m
if (m.matches()) { // 判斷matchedString是否匹配patternString的條件表達(dá)式
String str2 = m.group();
System.out.println(str2);
} else
System.out.println("不完全匹配");
if (m.lookingAt()) // 判斷從matchedString的開始位置是否有和patternString匹配的子序列
{
String str3 = m.group();
System.out.println(str3);
}
}
}
運行結(jié)果:
從0到3匹配模式子序列:0A1
從4到7匹配模式子序列:2A3
從8到11匹配模式子序列:4A5
從12到15匹配模式子序列:6A7
從16到19匹配模式子序列:8A9
####A####A####A####A####
0A1A2A3A4A5A6A7A8A9
不完全匹配
9A0
java課程設(shè)計題目及代碼分別是:
1、題目:計算器。設(shè)計內(nèi)容是設(shè)計一個圖形界面(GUI)的計算器應(yīng)用程序,完成簡單的算術(shù)運算。
設(shè)計要求是設(shè)計的計算器應(yīng)用程序可以完成家法、減法、乘法、除法和取余運算。且有小數(shù)點、正負(fù)號、求倒數(shù)、退格和清零功能。
2、代碼:
數(shù)字按鈕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課程設(shè)計題目及代碼是:
題目:華容道。編寫一個按鈕的子類,使用該子類創(chuàng)建的對象代表華容道中的人物。通過焦點事件控制人物顏色,當(dāng)人物獲得焦點時顏色為藍(lán)色,當(dāng)失去焦點時顏色為灰色。
通過鍵盤事件和鼠標(biāo)事件來實現(xiàn)曹操、關(guān)羽等人物的移動。當(dāng)人物上發(fā)生鼠標(biāo)事件或鍵盤事件時,如果鼠標(biāo)指針的位置是在人物的下方(也就是組件的下半部分)或按下鍵盤的“↓“鍵,該人物向下移動。向左、向右和向上的移動原理類似。
代碼是:
String name[]={"曹操","關(guān)羽","張","劉","馬","許","兵","兵","兵","兵"}.
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);
public?class?Circle?{
double?x,y,r;
//無參構(gòu)造方法
public?Circle()?{}
//三個參數(shù)的構(gòu)造方法
public?Circle(double?x,?double?y,?double?r)?{
this.x?=?x;
this.y?=?y;
this.r?=?r;
}
//求面積的方法,返回面積值
public?double?getArea()?{
//求圓的面積
double?s?=?3.14*r*r;
return?s;
}
//求周長的方法
public?double?getPerimeter()?{
double?l?=?3.14*r*2;
return?l;
}
//判斷是否相交
public?String?getStatus(Circle?c)?{
//判斷
//通過兩圓的圓心距當(dāng)圓心距小于兩圓半徑之差時?兩圓內(nèi)含
//當(dāng)圓心距等于兩圓半徑之差時?兩圓內(nèi)切
//當(dāng)圓心距小于兩圓半徑之和?大于半徑之差時?兩圓相交
//當(dāng)圓心距等于兩圓半徑之和時?兩圓外切
//當(dāng)圓心距大于兩圓半徑之和時?兩圓外離
String?result?=?"";
double?l?=?Math.sqrt((x-c.x)*(x-c.x)+(y-c.y)*(y-c.y));
if(l??r+c.r)?{
result?=?"相離";
}else?if(l?==?r+c.r)?{
result?=?"外切";
}else?if(l??r+c.r??l??r+c.r)?{
result?=?"相交";
}else?if(l?==?Math.abs(r-c.r))?{
result?=?"內(nèi)切";
}else?{
result?=?"內(nèi)含";
}
return?result;
}
}
public?class?TestCircle?{
public?static?void?main(String[]?args)?{
Circle?c1?=?new?Circle(5,?5,?5);?
Circle?c2?=?new?Circle(1,?2,?3);?
//面積?
System.out.println(c1.getArea());?
//周長?
System.out.println(c2.getPerimeter());?
//狀態(tài)?
System.out.println(c1.getStatus(c2));
}
}
第一題,x和n從命令行作為參數(shù)輸入:
public class Test1{
public static void main(String[] args){
int argLen = args.length;
//判斷是否至少傳入了兩個參數(shù)
if (argLen 2){
System.out.println("請輸入兩個整型參數(shù)");
return;
}
int x = 0;
int n = 0;
//轉(zhuǎn)換傳遞進來的參數(shù),如果輸入的參數(shù)不合法,不能轉(zhuǎn)換為int型,則Integer.parseInt方法會拋出NumberFormatException異常
try{
x = Integer.parseInt(args[0]);
n = Integer.parseInt(args[1]);
}
catch(NumberFormatException e)
{
System.out.println("輸入的參數(shù)不是整數(shù)");
System.exit(1);
}
//判斷x和n的值是否是正數(shù)
if (x=0 || n=0)
{
System.out.println("不能輸入負(fù)值或0,請輸入兩個正整數(shù)");
System.exit(1);
}
//打印轉(zhuǎn)換后的x和n
System.out.println("你輸入的x和n分別為: " + x + ", " + n);
/*
y=1+x/1+x*x*x/3+......+x^n/n
根據(jù)公式計算結(jié)果。由于公式中y增長的很快,所以我們定義一個double型的變量存儲結(jié)果的值。但仍然很有可能溢出。必要的話可以使用math包中的類來進行任意長度和精度的處理,但這里就不麻煩了。
*/
double y = 1.0;
for (int i=1; i=n; i+=2)
{
y += Math.pow(x, i)/(double)i;
}
//打印結(jié)果
System.out.println("根據(jù)公式y(tǒng)=1+x/1+x*x*x/3+......+x^n/n所計算出的結(jié)果為: " + y);
} // main()
} /* Test1 */
第二題,需要的test11.html文件內(nèi)容如下:
html
head
titleTest11 demo/title
/head
body
applet width="300" height="400" code="Test11.class"/applet
/body
/html
然后使用appletviewer test11.html瀏覽小應(yīng)用程序(在瀏覽器中可能不能正常運行)。
java代碼如下:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Label;
public class Test11 extends Applet{
//定義文字所在位置與頂部的距離
private int posY = 200;
private Label textsLabel = new Label("我猜你將看到這句話一直在滾動");
public void init()
{
textsLabel.setBounds(50, 200, 200, 30);
this.add(textsLabel);
//啟動新線程
SecThread st = new SecThread();
st.start();
} // init()
public void paint(Graphics g){
super.paint(g);
} //paint()
//定義一個內(nèi)部類,以啟動一個新的線程
private class SecThread extends Thread{
public void run()
{
while(true){
//讓當(dāng)前線程休眠50毫秒,注意sleep方法會拋出InterruptedException異常
try{
Thread.sleep(50);
}
catch(InterruptedException e){
System.out.println("執(zhí)行過程中出錯");
System.exit(1);
}
//設(shè)置文字的新位置
posY -= 5;
//判斷是否小于0(即已經(jīng)到達(dá)頂部),如果小于0則重置為400
posY = (posY=0?400:posY);
textsLabel.setBounds(50, posY, 200, 30);
Test11.this.repaint();
}
}
}
} /* Test2 */
3, 4兩題實在很簡單,略過了。
找到你的帖子了!
將3,和4也寫一下:
3.運行方法看2:
import java.applet.Applet;
import java.awt.Graphics;
public class Test111 extends Applet
{
public void paint(Graphics g)
{
for (int i=1; i=10; i++) //畫橫線
{
g.drawLine(20, i*20, 200, i*20);
}
for (int j=1; j=10; j++) //畫豎線
{
g.drawLine(j*20, 20, j*20, 200);
}
}
}
4. 代碼如下:(你說已經(jīng)寫好的程序怎么改成applet。記住一點,applet在運行時自動調(diào)用init、start和paint方法,而通常的應(yīng)用程序調(diào)用main方法。只要將main方法中的內(nèi)容妥善地移到這三個方法中就可以了。但修改的時候要注意,不要引入錯誤。)
//任意輸入三個數(shù),可以有小數(shù),然后比較它們的大小
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
public class Test1111 extends Applet
{
public void paint(Graphics g)
{
this.setLayout(null);
Button btn = new Button("開始輸入");
btn.setBounds(100, 130, 100, 30);
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
sort();
}
});
this.add(btn);
}
private void sort()
{
//3個元素的字符串?dāng)?shù)組,存放輸入的數(shù)
String[] numberStrs = new String[3];
for (int i=0; inumberStrs.length; i++)
{
//如果輸入時按了取消按鈕,則繼續(xù)提示輸入
while(numberStrs[i] == null)
{
numberStrs[i] = JOptionPane.showInputDialog("請輸入第 " + (i+1) + " 個數(shù)");
}
}
//定義3個元素的double型數(shù)組,存放轉(zhuǎn)換后的值
double[] numbers = new double[3];
try
{
for (int j=0; jnumbers.length; j++)
{
numbers[j] = Double.parseDouble(numberStrs[j]);
}
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "輸入的不是數(shù)字!"
, "ERROR", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
String result = "";
result += "你輸入的數(shù)字為: ";
for (int k=0; knumbers.length-1; k++)
{
result += numbers[k] + ", ";
}
result += numbers[numbers.length-1] + "\n";
//簡單點,使用冒泡排序
for (int i=1; inumbers.length; i++)
{
for (int j=0; jnumbers.length-1; j++)
{
if (numbers[j] numbers[j+1])
{
double temp = numbers[j];
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}
result += "排序后的數(shù)字為: ";
for (int k=0; knumbers.length-1; k++)
{
result += numbers[k] + ", ";
}
result += numbers[numbers.length-1];
//輸出結(jié)果
JOptionPane.showMessageDialog(null, result, "Result", JOptionPane.PLAIN_MESSAGE);
}
}
java.util.Date[]dates=newjava.util.Date[10];你只是new了一個Date類型的數(shù)組對象,但數(shù)組對象中并沒有對象,會報空指針的.for(inti=0;i
Java程序:
import?java.io.IOException;
import?java.util.ArrayList;
import?java.util.List;
import?java.util.Scanner;
/**
*?汽車類
*/
class?Car?{
/**
?*?汽車編號
?*/
protected?int?id?=?0;
/**
?*?汽車款式
?*/
protected?String?type?=?null;
/**
?*?構(gòu)造汽車對象
?*/
public?Car()?{
}
/**
?*?構(gòu)造汽車對象
?*?@param?id?汽車編號
?*?@param?type?汽車款式
?*/
public?Car(int?id,?String?type)?{
this.id?=?id;
this.type?=?type;
}
/**
?*?獲得汽車編號
?*?@return?汽車編號
?*/
public?int?getId()?{
return?this.id;
}
/**
?*?獲得汽車款式
?*?@return?汽車款式
?*/
public?String?getType()?{
return?this.type;
}
}
/**
*?汽車銷售人員類
*/
class?Saler?{
/**
?*?姓名
?*/
protected?String?name?=?null;
public?ListCar?cars?=?new?ArrayListCar();
/**
?*?構(gòu)造銷售汽車人員對象
?*/
public?Saler()?{
}
/**
?*?構(gòu)造汽車銷售人員對象
?*?@param?name?姓名
?*/
public?Saler(String?name)?{
this.name?=?name;
}
/**
?*?獲得姓名
?*?@return?姓名
?*/
public?String?getName()?{
return?this.name;
}
}
public?class?Main?{
public?static?void?main(String[]?args)?{
Scanner?scan?=?new?Scanner(System.in);
ListCar?allCar?=?new?ArrayListCar(); //待售汽車對象的集合
allCar.add(new?Car(1001,?"凱越"));
allCar.add(new?Car(1002,?"凱越"));
allCar.add(new?Car(1003,?"凱越"));
allCar.add(new?Car(1004,?"凱越"));
allCar.add(new?Car(2001,?"君威"));
allCar.add(new?Car(2002,?"君威"));
allCar.add(new?Car(2003,?"君威"));
allCar.add(new?Car(2004,?"君威"));
allCar.add(new?Car(2005,?"君威"));
Saler?saler?=?new?Saler("張三其");
int?choice?=?0;
int?type; //銷售車型
int?num; //銷售數(shù)量
while(true)?{
System.out.println("請選擇銷售方式");
System.out.println("按車輛銷售:\t1");
System.out.println("按車型銷售:\t2");
System.out.println("查看銷售情況:\t3");
System.out.println("退出:\t\t0");
System.out.print("您的選擇:");
choice?=?scan.nextInt();
switch(choice)?{
case?0: //退出系統(tǒng)
System.out.println("退出系統(tǒng)");
System.exit(0);
break;
case?1: //按車輛銷售
for(Car?car?:?allCar)?{
if(!?exists(saler.cars,?car))?{
saler.cars.add(car);
System.out.printf("\t售出?%s?1?輛\n",?car.getType());
break;
}
}
break;
case?2: //按車型銷售
System.out.print("車型(凱越??0/君威??1):");
type?=?scan.nextInt();
System.out.print("銷售數(shù)量:");
num?=?scan.nextInt();
int?c?=?0; //實際銷售數(shù)量
for(Car?car?:?allCar)?{
if(c?=?num)?{
break;
}
if(car.getType().equals(type?==?0???"凱越"?:?"君威")??!?exists(saler.cars,?car))?{
saler.cars.add(car);
c++;
}
}
if(c??num)?{
System.out.printf("\t庫存不足,實際售出?%s?%d?輛\n",?type?==?0???"凱越"?:?"君威",?c);
}
else?{
System.out.printf("\t售出?%s?%d?輛\n",?type?==?0???"凱越"?:?"君威",?num);
}
break;
case?3: //查看銷售情況
System.out.println("\t當(dāng)前銷售情況一覽");
System.out.printf("\t%10s%10s\n",?"汽車款式",?"汽車編號");
for(Car?car?:?saler.cars)?{
System.out.printf("\t%10s%10d\n",?car.getType(),?car.getId());
}
System.out.println("---------------------------");
System.out.printf("\t小計:\t%d?輛\n",?saler.cars.size());
break;
default:
break;
}
try?{
System.in.read();
}?catch?(IOException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}
}
}
//判斷car在cars中是否存在
public?static?boolean?exists(ListCar?cars,?Car?car)?{
for(Car?c?:?cars)?{
if(c.getId()?==?car.getId())?{
return?true;
}
}
return?false;
}
}
運行測試:
請選擇銷售方式
按車輛銷售: 1
按車型銷售: 2
查看銷售情況: 3
退出: 0
您的選擇:1
售出?凱越?1?輛
請選擇銷售方式
按車輛銷售: 1
按車型銷售: 2
查看銷售情況: 3
退出: 0
您的選擇:2
車型(凱越??0/君威??1):0
銷售數(shù)量:3
售出?凱越?3?輛
請選擇銷售方式
按車輛銷售: 1
按車型銷售: 2
查看銷售情況: 3
退出: 0
您的選擇:3
當(dāng)前銷售情況一覽
??汽車款式??????汽車編號
凱越??????1001
凱越??????1002
凱越??????1003
凱越??????1004
---------------------------
小計: 4?輛
請選擇銷售方式
按車輛銷售: 1
按車型銷售: 2
查看銷售情況: 3
退出: 0
您的選擇:0
退出系統(tǒng)
網(wǎng)站題目:java代碼檢視題目,java代碼檢查工具
標(biāo)題路徑:http://chinadenli.net/article3/dsedios.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、網(wǎng)站設(shè)計、網(wǎng)站內(nèi)鏈、定制網(wǎng)站、企業(yè)建站、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)