java之day4

Posted 哈喽杏红

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

DAY4 Python数据类型之列表

DAY4 Python数据类型之列表

NSATP-A学习笔记之Day4-5实战2

Day4-Java 基础语法

day4-软件目录开发规范

day4之函数