java之day4
Calc.java
package day4; public class Calc { public int sum(int num1, int num2) { int sum = num1 + num2; return sum; } }
CalcTest.java
package day4; import java.util.Scanner; public class CalcTest { public static void main(String[] args) { Calc calc = new Calc(); Scanner scanner= new Scanner(System.in); boolean goOn = false; do { System.out.println("請輸入第一個數:"); int num1 = scanner.nextInt(); System.out.println("請輸入第二個數:"); int num2 = scanner.nextInt(); int result = calc.sum(num1, num2); System.out.println("計算結果為" + result); System.out.println("是否繼續(y/n)"); String s = scanner.next(); goOn = s.equals("y"); } while (goOn); System.out.println("pikapika"); } }
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CustomerBiz.java
package day4; public class CustomerBiz { String names[] = new String[30]; int i = 0; // 添加姓名 public void addName(String name) { names[i] = name; i++; } // 顯示所有姓名 public void showName() { for (int i = 0; names[i] != null; i++) { System.out.println(names[i]); } } // 查找姓名 public boolean findName(String name) { for (int i = 0; names[i] != null; i++) { if (names[i].equals(name)) { return true; } } return false; } // 區間查找姓名 public boolean findNameRange(String name, int start, int end) { for (int i = start - 1; i < end; i++) { if (names[i].equals(name)) { return true; } } return false; } }
CustomerBizTest
package day4; import java.util.Scanner; public class CustomerBizTest { public static void main(String[] args) { Scanner input = new Scanner(System.in); CustomerBiz biz = new CustomerBiz(); do { System.out.println("請輸入客戶姓名"); String name = input.next(); biz.addName(name); System.out.println("是否繼續(y/n)"); String s = input.next(); if (s.equals("n")) { break; } } while (true); biz.showName(); System.out.println("請輸入您要查找的姓名:"); String name = input.next(); if (biz.findName(name)) { System.out.println("找到了"); } else { System.out.println("沒找到"); } System.out.println("請輸入您要查找的姓名:"); System.out.println("請輸入您要查找的姓名的開始位置:"); System.out.println("請輸入您要查找的姓名的結束位置:"); String name2 = input.next(); int start = input.nextInt(); int end = input.nextInt(); if (biz.findNameRange(name2, start, end)) { System.out.println("找到了"); } else { System.out.println("沒找到"); } } }
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
First.java 本類方法互相調用
package day4; public class First { // 本類方法相互調用 public void method() { System.out.println("我是First類中方法1"); } public void method2() { // 說的就是下邊這句 method(); System.out.println("我是First類中方法2"); } public static void main(String[] args) { First first = new First(); first.method2(); } }
Second.java 其他類方法調用
package day4; public class Second { // 調用其它類的方法 public void method21() { System.out.println("我是Second類中的第一個方法"); } public void method22() { // 說的是下邊這句 First first = new First(); first.method(); method21(); System.out.println("我是Second類中的第二個方法"); } public static void main(String[] args) { Second second = new Second(); second.method22(); } }
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
管理員系統
Manager.java
package day4; public class Manager { String name; String pwd; public Manager(String name,String pwd){ this.name = name; this.pwd = pwd; } public void show(){ System.out.println("管理員信息:"); System.out.println("用戶名:"+name); System.out.println("密碼:"+pwd); } }
ManagerTest.java
package day4; import java.util.Scanner; public class ManagerTest { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("請輸入用戶名:"); String name = scanner.next(); System.out.println("密碼:"); String pwd = scanner.next(); if(name.equals("12345")&&pwd.equals("12345")){ Manager manager = new Manager(name,pwd); manager.show(); }else{ System.out.println("賬號密碼不正確"); } } }
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
總成績&平均成績
ScoreCalc
package day4; public class ScoreCalc { int score1; int score2; int score3; public int sum() { int sum = score1 + score2 + score3; return sum; } public double avg() { int sum = score1 + score2 + score3; return (double) sum / 3; } public ScoreCalc(int score1, int score2, int score3) { this.score1 = score1; this.score2 = score2; this.score3 = score3; } }
ScoreCalcTest
package day4; import java.util.Scanner; public class ScoreCalcTest { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("java成績"); int score1 = scanner.nextInt(); System.out.println("C成績"); int score2 = scanner.nextInt(); System.out.println("DB成績"); int score3 = scanner.nextInt(); ScoreCalc calc = new ScoreCalc(score1, score2, score3); System.out.println("總成績為" + calc.sum()); System.out.println("平均成績為" + calc.avg()); } }
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
管理系統
Menu.java
package day4; import java.util.Scanner; public class Menu { public void showMenu1() { System.out.println("歡迎來到我行我素購物管理系統"); System.out.println("\t1:登錄系統"); System.out.println("\t2:推出系統"); System.out.println("************************"); System.out.println("請選擇,輸入數字:"); Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); switch (num) { case 1: showMenu2(); break; case 2: System.out.println("退出系統成功!"); break; default: System.out.println("您輸入有誤!重新輸入"); showMenu1(); break; } } public void showMenu2() { System.out.println("\t歡迎來到我行我素購物管理系統"); System.out.println("************************"); System.out.println("\t1:客戶信息管理"); System.out.println("\t2:真情回饋"); System.out.println("************************"); System.out.println("請選擇,輸入數字(0:返回上級):"); Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); switch (num) { case 1: System.out.println("功能正在完善,敬請期待"); showMenu2(); break; case 2: showMenu3(); case 0: showMenu1(); break; default: System.out.println("您輸入數字有誤!"); showMenu2(); break; } } public void showMenu3() { System.out.println("我行我素購物管理系統--->真情回饋"); System.out.println("************************"); System.out.println("\t1:幸運大放送"); System.out.println("\t2:幸運抽獎"); System.out.println("請選擇,輸入數字:"); Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); } }
MenuTest
package day4; public class MenuTest { public static void main(String[] args) { Menu menu = new Menu(); menu.showMenu1(); } }
java之day4