第一個:
創(chuàng)新互聯(lián)建站主要從事成都做網(wǎng)站、網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)密山,10多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792
import?java.util.Scanner;
/**
*?@author?zhangdong
*?@date?2018/4/24??16:14
*/
public?class?HomeWork1?{
public?static?void?main(String[]?args)?{
System.out.println("請輸入一個數(shù)字(0-3)");
Scanner?scanner?=?new?Scanner(System.in);
int?num?=?scanner.nextInt();
switch?(num)?{
case?0:
System.out.println("0-?明天去打球");
break;
case?1:
System.out.println("1-?明天去跑步");
break;
case?2:
System.out.println("2-?明天去放鴨");
break;
case?3:
System.out.println("3-?明天去爬山");
break;
default:
System.out.println("其他-明天呆在家里");
break;
}
}
}
第二個:
import?java.util.Scanner;
/**
*?@author?zhangdong
*?@date?2018/4/24??16:18
*/
public?class?HomeWork2?{
public?static?void?main(String[]?args)?{
System.out.println("請輸入一個數(shù)字(1-7)");
Scanner?scanner?=?new?Scanner(System.in);
int?num?=?scanner.nextInt();
switch?(num)?{
case?1:
System.out.println("星期一");
break;
case?2:
System.out.println("星期二");
break;
case?3:
System.out.println("星期三");
break;
case?4:
System.out.println("星期四");
break;
case?5:
System.out.println("星期五");
break;
case?6:
System.out.println("星期六");
break;
case?7:
System.out.println("星期日");
break;
default:
System.out.println("輸入錯誤!");
break;
}
}
}
第三個:
/**
*?@author?zhangdong
*?@date?2018/4/24??16:20
*/
public?class?Car?{
private?String?name;
private?String?color;
private?String?placeOrigin;
public?Car(String?name,?String?color,?String?placeOrigin)?{
this.name?=?name;
this.color?=?color;
this.placeOrigin?=?placeOrigin;
}
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?String?getColor()?{
return?color;
}
public?void?setColor(String?color)?{
this.color?=?color;
}
public?String?getPlaceOrigin()?{
return?placeOrigin;
}
public?void?setPlaceOrigin(String?placeOrigin)?{
this.placeOrigin?=?placeOrigin;
}
public?void?run()?{
System.out.println("汽車正在以50km/s的速度行駛中");
}
public?void?run(int?speed)?{
System.out.println("汽車正在以"?+?speed?+?"km/s的速度行駛中");
}
@Override
public?String?toString()?{
return?"汽車基本信息:"?+
"???車名:'"?+?name?+?'\''?+
"???顏色:'"?+?color?+?'\''?+
"???產(chǎn)地:'"?+?placeOrigin?+?'\''?+
'}';
}
public?static?void?main(String[]?args)?{
Car?car?=?new?Car("法拉利",?"黑色",?"德國");
System.out.println(car.toString());
car.run();
car.run(80);
}
}
/**
需求:獎客富翁游戲。
菜單為:
1.注冊用戶信息。填寫用戶名,密碼,電腦隨機(jī)輸出一個4位
數(shù)字作為會員卡號。提示用戶名,密碼,及會員卡號??蛇x擇第二步了
2.登陸。校驗(yàn)是否已注冊(是否存在會員卡號)。如果為空,
提示用戶不存在。
如果用戶名通過,密碼錯誤,提示用戶名和密碼不匹配。如果
驗(yàn)證成功結(jié)束。如果輸入的用戶名密碼均匹配,給出登陸成功提示。
可選擇第三步了。
3.先判斷用戶是否已登陸(是否存在會員卡)
詢問是否進(jìn)行抽獎,選Y,電腦隨機(jī)輸出5個4位
數(shù)字。如果其中有一個數(shù)字與用戶卡號相同,則為中獎,獎勵
人民幣為卡號數(shù)字的金額。否則,謝謝參與。程序退出。
*/
import?java.util.Scanner;
import?java.util.Random;
public?class?RichPerson?{
public?static?void?main(String[]?args)?{
//?輸出菜單。
for?(int?a?=?0;?a??6;?a++)?{
System.out.print("*");
}
System.out.print("歡迎進(jìn)入獎客富翁系統(tǒng)");
for?(int?b?=?0;?b??6;?b++)?{
System.out.print("*");
}
System.out.print("\n\t\t1.注冊\n"?+?"\t\t2.登錄\n"?+?"\t\t3.抽獎\n");
for?(int?c?=?0;?c??31;?c++)?{
System.out.print("*");
}
//?輸出菜單結(jié)束。
int?caidan?=?0;//?操作菜單
String?name?=?null;//?會員注冊名字
String?myname?=?null;//?會員登錄名字
String?password?=?null;//?會員注冊密碼
String?mypassword?=?null;//?會員登錄密碼
int?number?=?0;//?會員卡號
//?int?luckynumber=0;//幸運(yùn)號碼
String?str?=?null;//?詢問是否重試
//?輸入數(shù)字選擇菜單。
Scanner?input?=?new?Scanner(System.in);
Random?output?=?new?Random();
do?{
System.out.println("\n請選擇菜單:");
caidan?=?input.nextInt();
switch?(caidan)?{
case?1:
System.out.println("歡迎注冊富翁,請依次輸入用戶名和密碼");
System.out.println("用戶名:");
name?=?input.next();
System.out.println("密碼:");
password?=?input.next();
//?隨機(jī)給出4位數(shù)的會員卡號
number?=?output.nextInt(8999)?+?1000;
System.out.println("注冊成功!您的用戶名:"?+?name?+?",密碼:"?+?password
+?",會員卡號:"?+?number?+?"。請牢記!");
break;
case?2:
//?先判斷用戶是否已注冊。
if?(name?==?null)?{
System.out.println("您好,請先注冊!");
break;
}?else?{
System.out.println("歡迎登錄富翁,請依次輸入用戶名和密碼!");
//?用戶名和密碼匹配校驗(yàn)次數(shù)設(shè)置為3次。
for?(int?x?=?0;?x??3;?x++)?{
System.out.println("用戶名:");
myname?=?input.next();
if?(!myname.equals(name))?{
System.out.println("用戶"?+?myname?+?"不存在!請重試,剩余次數(shù)"
+?(2?-?x)?+?"次!");
}?else?{
System.out.println("密碼:");
}
mypassword?=?input.next();
if?(!mypassword.equals(password))?{
System.out.println(myname?+?"密碼錯誤!"?+?"請重試,剩余次數(shù)"
+?(2?-?x)?+?"次!");
}?else?{
System.out.println("登陸成功!");
break;
}
}
}
break;
case?3:
//?判斷用戶是否已注冊,是否已登錄。
if?(mypassword?==?null)?{
System.out.println("您好,請先登錄!");
break;
}?else?if?(!mypassword.equals(password))?{
System.out.println("您好,請先重試登錄!");
break;
}?else?if?(mypassword.equals(password))?{
System.out.println("歡迎進(jìn)入富翁,請問是否進(jìn)行幸運(yùn)抽獎(y/n)?");
String?choujiang?=?input.next();
if?(choujiang.equals("y"))?{
int[]?luckynumber?=?new?int[5];
for?(int?m?=?0;?m??5;?m++)?{
luckynumber[m]?=?output.nextInt(8999)?+?1000;
}
for?(int?m?=?0;?m??5;?m++)?{
System.out.print(luckynumber[m]?+?"\t");
}
System.out.println("\n"?+?number);
for?(int?m?=?0;?m??5;?m++)?{
if?(number?==?luckynumber[m])?{
System.out.println("\n中獎!金額為:"?+?luckynumber);
break;
}?else?{
System.out.println("\n木有中獎!");
break;
}
}
這個計(jì)算器可是我自己寫了好幾天才寫好的耶,你能在網(wǎng)上找到?
計(jì)算器:
// Calculator.java
public class Calculator {
private static final char NO_OP = '\0';
private static final char PLUS = '+';
private static final char SUBTRACT = '-';
private static final char MULTIPLY = '*';
private static final char DIVIDE = '/';
private float number1 = 0.0F;
//private float number2 = Float.NaN;
private char operator = NO_OP;
//等于運(yùn)算
public String opEquals(String number) {
float result;
if ( operator == NO_OP ) {
result = parseNumber(number);
} else {
result = performOperation(parseNumber(number));
}
operator = NO_OP;
number1 = result;
return Float.toString(result);
}
//加法運(yùn)算
public String opAdd(String number) {
float result;
if ( operator == NO_OP ) {
result = parseNumber(number);
} else {
result = performOperation(parseNumber(number));
}
operator = PLUS;
number1 = result;
return Float.toString(result);
}
//減法運(yùn)算
public String opSubtract(String number) {
float result;
if ( operator == NO_OP ) {
result = parseNumber(number);
} else {
result = performOperation(parseNumber(number));
}
operator = SUBTRACT;
number1 = result;
return Float.toString(result);
}
//乘法運(yùn)算
public String opMultiply(String number) {
float result;
if ( operator == NO_OP ) {
result = parseNumber(number);
} else {
result = performOperation(parseNumber(number));
}
operator = MULTIPLY;
number1 = result;
return Float.toString(result);
}
//除法運(yùn)算
public String opDivide(String number) {
float result;
if ( operator == NO_OP ) {
result = parseNumber(number);
} else {
result = performOperation(parseNumber(number));
}
operator = DIVIDE;
number1 = result;
return Float.toString(result);
}
//處理運(yùn)算符
private float performOperation(float number2) {
float result = 0.0F;
switch ( operator ) {
case PLUS:
result = number1 + number2;
break;
case SUBTRACT:
result = number1 - number2;
break;
case MULTIPLY:
result = number1 * number2;
break;
case DIVIDE:
result = number1 / number2;
break;
}
return result;
}
//將字符串類型轉(zhuǎn)換為浮點(diǎn)型
private static float parseNumber(String number) {
float real_number;
try {
real_number = Float.parseFloat(number);
} catch (NumberFormatException e) {
real_number = Float.NaN;
}
return real_number;
}
}
GUI程序:
// CalculatorGUI.java
import java.awt.*;
import java.awt.event.*;
public class CalculatorGUI {
// 定義按鈕
private Button key0, key1, key2, key3, key4;
private Button key5, key6, key7, key8, key9;
private Button keyequal, keyplus, keyminus;
private Button keyperiod, keymult, keydiv;
// 定義存放按鈕的區(qū)域
private Panel buttonArea;
// 定義存放運(yùn)算結(jié)果的區(qū)域
private Label answer;
// 用來實(shí)現(xiàn)運(yùn)算功能的類的對象實(shí)例化
private Calculator calculator;
private boolean readyForNextNumber;
public CalculatorGUI() {
calculator = new Calculator();
readyForNextNumber = true;
answer = new Label("0.0",Label.RIGHT);
key0 = new Button("0");
key1 = new Button("1");
key2 = new Button("2");
key3 = new Button("3");
key4 = new Button("4");
key5 = new Button("5");
key6 = new Button("6");
key7 = new Button("7");
key8 = new Button("8");
key9 = new Button("9");
keyequal = new Button("=");
keyplus = new Button("+");
keyminus = new Button("-");
keymult = new Button("*");
keydiv = new Button("/");
keyperiod = new Button(".");
buttonArea = new Panel();
}
public void launchFrame() {
buttonArea.setLayout(new GridLayout(4,4));
buttonArea.add(key7);
buttonArea.add(key8);
buttonArea.add(key9);
buttonArea.add(keyplus);
buttonArea.add(key4);
buttonArea.add(key5);
buttonArea.add(key6);
buttonArea.add(keyminus);
buttonArea.add(key1);
buttonArea.add(key2);
buttonArea.add(key3);
buttonArea.add(keymult);
buttonArea.add(key0);
buttonArea.add(keyperiod);
buttonArea.add(keyequal);
buttonArea.add(keydiv);
// 設(shè)置事件接收句柄
OpButtonHanlder op_handler = new OpButtonHanlder();
NumberButtonHanlder number_handler = new NumberButtonHanlder();
key0.addActionListener(number_handler);
key1.addActionListener(number_handler);
key2.addActionListener(number_handler);
key3.addActionListener(number_handler);
key4.addActionListener(number_handler);
key5.addActionListener(number_handler);
key6.addActionListener(number_handler);
key7.addActionListener(number_handler);
key8.addActionListener(number_handler);
key9.addActionListener(number_handler);
keyperiod.addActionListener(number_handler);
keyplus.addActionListener(op_handler);
keyminus.addActionListener(op_handler);
keymult.addActionListener(op_handler);
keydiv.addActionListener(op_handler);
keyequal.addActionListener(op_handler);
// 新建一個幀,并且加上消息監(jiān)聽
Frame f = new Frame("計(jì)算器");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setSize(200, 200);
f.add(answer, BorderLayout.NORTH);
f.add(buttonArea, BorderLayout.CENTER);
f.setVisible (true);
}
//響應(yīng)按鈕事件,并根據(jù)不同的按鈕事件進(jìn)行不同的運(yùn)算
private class OpButtonHanlder implements ActionListener {
public void actionPerformed(ActionEvent event) {
char operator = event.getActionCommand().charAt(0);
String result = "";
switch ( operator ) {
case '+':
result = calculator.opAdd(answer.getText());
break;
case '-':
result = calculator.opSubtract(answer.getText());
break;
case '*':
result = calculator.opMultiply(answer.getText());
break;
case '/':
result = calculator.opDivide(answer.getText());
break;
case '=':
result = calculator.opEquals(answer.getText());
break;
}
answer.setText(result);
readyForNextNumber = true;
}
}
//處理連續(xù)運(yùn)算時(shí)主界面的顯示
private class NumberButtonHanlder implements ActionListener {
public void actionPerformed(ActionEvent event) {
if ( readyForNextNumber ) {
answer.setText(event.getActionCommand());
readyForNextNumber = false;
} else {
answer.setText(answer.getText() + event.getActionCommand().charAt(0));
}
}
}
//主函數(shù),也是該應(yīng)用程序的執(zhí)行入口處
public static void main(String[] args) {
CalculatorGUI calcGUI = new CalculatorGUI();
calcGUI.launchFrame();
}
}
注意:運(yùn)行的是GUI程序
300行能干啥啊,你還不如自己做做練習(xí)題。
package suoha;
import java.util.ArrayList;import java.util.Random;
public class MainFunction {
private ArrayListMyCard cards = new ArrayListMyCard();
private int[] cardNumArr = new int[52];
private int[] mycards = new int[5];
private int[] p1cards = new int[5];
public static void main(String[] args) {
MainFunction mainfunc = new MainFunction();
mainfunc.makeCards();
mainfunc.cardNumArr = mainfunc.shuffleCards(mainfunc.cardNumArr);
mainfunc.catchCards();
mainfunc.showAllCardsOfHand();
//mainfunc.printNums();
}
/**
* 洗牌
* @param cardNums
* @return
*/
private int[] shuffleCards(int[] cardNums){
int size = cardNums.length;
int[] returnNums = new int[size];
//System.out.println(cardNums[0]+"=========================");
Random rand = new Random();
for(int i=size-1;i0;i--){
int nextInt = rand.nextInt(i);
returnNums[size-1-i] = cardNums[nextInt];
cardNums[nextInt] = cardNums[i];
cardNums[i] = returnNums[size-1-i];
//System.out.println("suijishu:"+nextInt+"||zhi:"+returnNums[size-1-i]);
}
returnNums[size-1] = cardNums[0];
return returnNums;
}
/**
* 抓牌
*/
private void catchCards(){
for(int i=0;i5;i++){
mycards[i] = cardNumArr[2*i];
p1cards[i] = cardNumArr[2*i+1];
}
}
private void showAllCardsOfHand(){
System.out.print("你手中的牌是:||");
for(int i=0;i5;i++){
MyCard myCard = cards.get(mycards[i]-1);
System.out.print(myCard.toString()+"||");
}
System.out.println();
System.out.print("對家手中的牌是:||");
for(int i=0;i5;i++){
MyCard myCard = cards.get(p1cards[i]-1);
System.out.print(myCard.toString()+"||");
}
}
/**
* 顯示牌的順序
*/
private void printNums(){
for(int i=0;icardNumArr.length;i++){
System.out.print(cardNumArr[i]+",");
}
}
/**
* 制作卡片
*/
private void makeCards(){
for(int i=1;i=13;i++){
for(int j=1;j=4;j++){
//System.out.println((i-1)*4+j);
MyCard card = new MyCard((i-1)*4+j, j, i);
cardNumArr[(i-1)*4+j-1] = (i-1)*4+j;
cards.add(card);
}
// MyCard card1 = new MyCard((i-1)*4+1, 1, i);
// MyCard card2 = new MyCard((i-1)*4+2, 2, i);
// MyCard card3 = new MyCard((i-1)*4+3, 3, i);
// MyCard card4 = new MyCard((i-1)*4+4, 4, i);
// cards.add(card1);
// cards.add(card2);
// cards.add(card3);
// cards.add(card4);
}
}
}
java項(xiàng)目的大小衡量標(biāo)準(zhǔn):
微型:只是一個人,甚至是半日工作在幾天內(nèi)完成的軟件; ?
小型:一個人半年內(nèi)完成的?2000?行以內(nèi)的程序;
中型:?5?個人在?1?年多的時(shí)間內(nèi)完成的?5000-50000?行的程序;
大型:?5-10?人在兩年內(nèi)完成的?50000-100000?行的程序;
甚大型:?100-1000?人參加用?4-5?年完成的具有?100?,?0000?行的軟件項(xiàng)目;
極大型:?2000-5000?人參加,?10?年內(nèi)完成的?1000?萬行以內(nèi)的程序;
以上摘自:《軟件工程概論》???鄭人杰、殷人民編
這樣的觀點(diǎn)是以代碼行作為計(jì)量標(biāo)準(zhǔn)的,認(rèn)為代碼行多的自然項(xiàng)目也就大了。
以1000行為準(zhǔn),超過千行就要考慮類拆分了。
對于經(jīng)常使用的java類,代碼行數(shù)應(yīng)該盡可能的少,這樣能減少java類的加載時(shí)間,減少內(nèi)存頻繁占用和回收。如果類過大,java類加載會耗時(shí)并且占用內(nèi)存大。容易引起內(nèi)存回收。
如果前期準(zhǔn)備得當(dāng),設(shè)計(jì),建模合理。一般來說,每個類的代碼就不會很多,幾百行。很早之前看過有前輩說,300行之內(nèi)。