扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
第一个:
创新互联建站主要从事成都做网站、网站制作、成都外贸网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务密山,10多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务: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("请输入一个数字(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("请输入一个数字(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 + '\'' +
" 产地:'" + placeOrigin + '\'' +
'}';
}
public static void main(String[] args) {
Car car = new Car("法拉利", "黑色", "德国");
System.out.println(car.toString());
car.run();
car.run(80);
}
}
/**
需求:奖客富翁游戏。
菜单为:
1.注册用户信息。填写用户名,密码,电脑随机输出一个4位
数字作为会员卡号。提示用户名,密码,及会员卡号。可选择第二步了
2.登陆。校验是否已注册(是否存在会员卡号)。如果为空,
提示用户不存在。
如果用户名通过,密码错误,提示用户名和密码不匹配。如果
验证成功结束。如果输入的用户名密码均匹配,给出登陆成功提示。
可选择第三步了。
3.先判断用户是否已登陆(是否存在会员卡)
询问是否进行抽奖,选Y,电脑随机输出5个4位
数字。如果其中有一个数字与用户卡号相同,则为中奖,奖励
人民币为卡号数字的金额。否则,谢谢参与。程序退出。
*/
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("欢迎进入奖客富翁系统");
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("*");
}
// 输出菜单结束。
int caidan = 0;// 操作菜单
String name = null;// 会员注册名字
String myname = null;// 会员登录名字
String password = null;// 会员注册密码
String mypassword = null;// 会员登录密码
int number = 0;// 会员卡号
// int luckynumber=0;//幸运号码
String str = null;// 询问是否重试
// 输入数字选择菜单。
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();
// 随机给出4位数的会员卡号
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("欢迎登录富翁,请依次输入用户名和密码!");
// 用户名和密码匹配校验次数设置为3次。
for (int x = 0; x 3; x++) {
System.out.println("用户名:");
myname = input.next();
if (!myname.equals(name)) {
System.out.println("用户" + myname + "不存在!请重试,剩余次数"
+ (2 - x) + "次!");
} else {
System.out.println("密码:");
}
mypassword = input.next();
if (!mypassword.equals(password)) {
System.out.println(myname + "密码错误!" + "请重试,剩余次数"
+ (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("欢迎进入富翁,请问是否进行幸运抽奖(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;
}
}
这个计算器可是我自己写了好几天才写好的耶,你能在网上找到?
计算器:
// 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;
//等于运算
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);
}
//加法运算
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);
}
//减法运算
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);
}
//乘法运算
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);
}
//除法运算
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);
}
//处理运算符
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;
}
//将字符串类型转换为浮点型
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;
// 定义存放按钮的区域
private Panel buttonArea;
// 定义存放运算结果的区域
private Label answer;
// 用来实现运算功能的类的对象实例化
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);
// 设置事件接收句柄
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);
// 新建一个帧,并且加上消息监听
Frame f = new Frame("计算器");
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);
}
//响应按钮事件,并根据不同的按钮事件进行不同的运算
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;
}
}
//处理连续运算时主界面的显示
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));
}
}
}
//主函数,也是该应用程序的执行入口处
public static void main(String[] args) {
CalculatorGUI calcGUI = new CalculatorGUI();
calcGUI.launchFrame();
}
}
注意:运行的是GUI程序
300行能干啥啊,你还不如自己做做练习题。
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项目的大小衡量标准:
微型:只是一个人,甚至是半日工作在几天内完成的软件;
小型:一个人半年内完成的 2000 行以内的程序;
中型: 5 个人在 1 年多的时间内完成的 5000-50000 行的程序;
大型: 5-10 人在两年内完成的 50000-100000 行的程序;
甚大型: 100-1000 人参加用 4-5 年完成的具有 100 , 0000 行的软件项目;
极大型: 2000-5000 人参加, 10 年内完成的 1000 万行以内的程序;
以上摘自:《软件工程概论》 郑人杰、殷人民编
这样的观点是以代码行作为计量标准的,认为代码行多的自然项目也就大了。
以1000行为准,超过千行就要考虑类拆分了。
对于经常使用的java类,代码行数应该尽可能的少,这样能减少java类的加载时间,减少内存频繁占用和回收。如果类过大,java类加载会耗时并且占用内存大。容易引起内存回收。
如果前期准备得当,设计,建模合理。一般来说,每个类的代码就不会很多,几百行。很早之前看过有前辈说,300行之内。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流