java一些简单的项目

发布于 2024-03-21  296 次阅读


java一些简单的项目

妈的我写的都什么垃圾,(Φ皿Φ)

基础数组扩容,排序,操作

class zhanjie66// 扩容/减少/排序数组

{
    public static void main(String[] args) {
        Scanner myScanner = new Scanner(System.in);
        int shuz[] = { 4, -1, 9, 1000, 233333, 144, 252 };
        char ye = 0;
        System.out.println("当前数组为");
        for (int i = 0; i <= shuz.length - 1; ++i) {
            System.out.print(shuz[i] + " ");
        }
        System.out.println("\n1扩容2缩减3排序");
        ye = myScanner.next().charAt(0);
        if (ye == '1') {
            do {
                System.out.println("要添加多少个数");
                int c = myScanner.nextInt();
                int t = shuz.length - 1;
                int temp[] = new int[shuz.length + c];
                for (int i = 0; i < shuz.length; i++) {
                    temp[i] = shuz[i];
                }
                shuz = temp;
                System.out.println("目前数组扩为" + shuz.length);
                for (int i = 1; i <= c; i++) {
                    System.out.println("要添加的第" + i + "个数");
                    int f = myScanner.nextInt();
                    t += 1;
                    shuz[t] = f;
                }
                System.out.println("当前数组为");
                for (int i = 0; i <= shuz.length - 1; ++i) {
                    System.out.print(shuz[i] + " ");
                }
                System.out.println("\n是否继续扩y/n");
                ye = myScanner.next().charAt(0);
            } while ('y' == ye);
        } else if (ye == '2') {
            do {
                System.out.println("要缩减多少个数");
                int c = myScanner.nextInt();
                if (shuz.length - c <= 0) {
                    System.out.println("当前元素数不足以缩减" + c + "个");
                    break;
                }
                int temp[] = new int[shuz.length - c];
                for (int i = 0; i < shuz.length - c; i++) {
                    temp[i] = shuz[i];
                }
                shuz = temp;
                System.out.println("当前数组为");
                for (int i = 0; i <= shuz.length - 1; ++i) {
                    System.out.print(shuz[i] + " ");
                }
                System.out.println("\n是否继续缩y/n");
                ye = myScanner.next().charAt(0);
            } while (ye == 'y');
        } else if (ye == '3') {
            do {
                System.out.println("由小到大排序");
                int temp = 0;
                for (int i2 = 0; i2 <= shuz.length - 1; i2++) {
                    for (int i = 0; i <= shuz.length - 2; i++) {
                        if (shuz[i] <= shuz[i + 1]) {

                        } else if (shuz[i] > shuz[i + 1]) {
                            temp = shuz[i];
                            shuz[i] = shuz[i + 1];
                            shuz[i + 1] = temp;
                        }
                    }
                }
                System.out.println("当前数组为");
                for (int i = 0; i <= shuz.length - 1; ++i) {
                    System.out.print(shuz[i] + " ");
                }
                System.out.println("\n是否继续y/n");
                ye = myScanner.next().charAt(0);
            } while (ye == 'y');
        }
    }
}

房屋菜单(对象思想)

public class qidong {
    public static void main(String[] args) {
        Frame aaa=new Frame();
        aaa.jmain();
    }
}

class House {
    private String name;
    private int telephone;
    private String address;
    private double monthlyRent;
    private String status;

    public House() {

    }

    public House(String name, int telephone,
                 String address, double monthlyRent, String status) {
        this.name = name;
        this.telephone = telephone;
        this.address = address;
        this.monthlyRent = monthlyRent;
        this.status = status;
    }

    public String toString() {
        return "姓名:" + this.name + "\t电话:" + this.telephone + "\n地址:" + this.address
                + "\n月租:" + this.monthlyRent + "\n状态:" + this.status;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getTelephone() {
        return telephone;
    }

    public void setTelephone(int telephone) {
        this.telephone = telephone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public double getMonthlyRent() {
        return monthlyRent;
    }

    public void setMonthlyRent(double monthlyRent) {
        this.monthlyRent = monthlyRent;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

class Frame {
    Scanner myScanner = new Scanner(System.in);
    public int select;
    public boolean controls = true;
    private House[] housingList = new House[0];

    public void jmain() {
        do {
            this.menu();
            this.judgmentFrame();
        } while (controls);
        System.out.println("----------退出系统----------");
    }

    public void menu() {
        System.out.println("----------菜单----------");
        System.out.println("\t1.新增房源");
        System.out.println("\t2.查找房屋");
        System.out.println("\t3.删除房屋");
        System.out.println("\t4.修改房屋信息");
        System.out.println("\t5.房屋列表");
        System.out.println("\t6.退出");
        System.out.println("请输入:");
        select = myScanner.nextInt();
    }

    public void judgmentFrame() {
        switch (select) {
            case 1:
                newHousing();
                break;
            case 2:
                findHouse();
                break;
            case 3:
                deleteHouse();
                break;
            case 4:
                modifyInformation();
                break;
            case 5:
                houseList();
                break;
            case 6:
                controls = false;
                break;
            default:
                System.out.println("请输入1-6的值");
                break;
        }

    }

    public void newHousing() {
//扩大housingList数组
        House[] temp = new House[housingList.length + 1];
        for (int i = 0; i < housingList.length; i++) {
            temp[i] = housingList[i];
        }
        housingList = temp;
        House aaa = new House();
        housingList[housingList.length - 1] = aaa;
//新建一个House对象
        System.out.println("----------添加房屋----------");
        System.out.println("姓名:");
        housingList[housingList.length - 1].setName(myScanner.next());
        System.out.println("电话:");
        housingList[housingList.length - 1].setTelephone(myScanner.nextInt());
        System.out.println("地址:");
        housingList[housingList.length - 1].setAddress(myScanner.next());
        System.out.println("月租:");
        housingList[housingList.length - 1].setMonthlyRent(myScanner.nextDouble());
        System.out.println("状态:");
        housingList[housingList.length - 1].setStatus(myScanner.next());
        System.out.println("----------添加完成----------");
    }

    public void findHouse() {
        System.out.println("----------查找系统----------");
        System.out.println("\t1.查找名称");
        System.out.println("\t2.查找电话");
        System.out.println("\t3.查找地址");
        System.out.println("\t4.查找月租");
        System.out.println("请输入:");
        select = myScanner.nextInt();
        int c = -1;
        switch (select) {
            case 1:
                c=-1;
                System.out.println("输入名称:");
                String fineName=myScanner.next();
                for (int i = 0; i <housingList.length; i++) {
                    if(fineName.equals(housingList[i].getName())){
                        System.out.println("查找到为列表"+(i+1)+"个");
                        System.out.println(housingList[i]);
                        c++;
                    }
                }
                if(c==-1){
                    System.out.println("未找到");
                }
                break;
            case 2:
                c=-1;
                System.out.println("输入电话:");
                int fineTelephone=myScanner.nextInt();
                for (int i = 0; i <housingList.length; i++) {
                    if(fineTelephone==housingList[i].getTelephone()){
                        System.out.println("查找到为列表"+(i+1)+"个");
                        System.out.println(housingList[i]);
                        c++;
                    }
                }
                if(c==-1){
                    System.out.println("未找到");
                }
                break;
            case 3:
                c=-1;
                System.out.println("输入地址:");
                String fineAdd=myScanner.next();
                for (int i = 0; i <housingList.length; i++) {
                    if(fineAdd.equals(housingList[i].getAddress())){
                        System.out.println("查找到为列表"+(i+1)+"个");
                        System.out.println(housingList[i]);
                        c++;
                    }
                }
                if(c==-1){
                    System.out.println("未找到");
                }
                break;
            case 4:
                c=-1;
                System.out.println("输入月租:");
                double fineDDD=myScanner.nextDouble();
                for (int i = 0; i <housingList.length; i++) {
                    if(fineDDD==housingList[i].getMonthlyRent()){
                        System.out.println("查找到为列表"+(i+1)+"个");
                        System.out.println(housingList[i]);
                        c++;
                    }
                }
                if(c==-1){
                    System.out.println("未找到");
                }
                break;
            default:
                System.out.println("请输入正确值1-4");
                break;
        }
    }

    public void deleteHouse() {
        System.out.println("----------删除房屋----------");
        System.out.println("输入要删除的房屋编编号");
        int deleteId=myScanner.nextInt();
        if(deleteId>housingList.length){
            System.out.println("编号错误");
            return;
        }
        House[] temp2 = new House[housingList.length -1];
        int j=0;
        for (int i = 0; i < housingList.length; i++,j++) {
            if((i+1)==deleteId){
                j--;
            }else {
                temp2[j] = housingList[i];
            }
        }

        housingList = temp2;
        System.out.println("删除完成QwQ");
    }

    public void modifyInformation() {
        System.out.println("----------修改房间信息--------");
        System.out.println("输入要修改的房屋编号");
        int modifyId=myScanner.nextInt();
        if(modifyId>housingList.length){
            System.out.println("编号错误");
            return;
        }
        System.out.println("姓名:");
        housingList[modifyId-1].setName(myScanner.next());
        System.out.println("电话:");
        housingList[modifyId-1].setTelephone(myScanner.nextInt());
        System.out.println("地址:");
        housingList[modifyId-1].setAddress(myScanner.next());
        System.out.println("月租:");
        housingList[modifyId-1].setMonthlyRent(myScanner.nextDouble());
        System.out.println("状态:");
        housingList[modifyId-1].setStatus(myScanner.next());
        System.out.println("----------更改完成----------");
    }

    public void houseList() {
        System.out.println("----------列表----------");
        for (int i = 0; i < housingList.length; i++) {
            System.out.println("列表" + (i + 1) + "\n" + housingList[i]);
        }
    }
}

计算器(不过好绕)

public class wwww {
    public static void main(String[] args) {
        YNumber three = new Suc(new Suc(new Suc(new Zero())));

        YNumber five = new Suc(new Suc(new Suc(new Suc(new Suc(new Zero())))));

        FloatSolver solver = new FloatSolver();
        YNumber he=new AddOp(three,five);
        System.out.println(solver.solve(he));

    }

}

interface YNumber {
}
class FloatSolver{
    public float solve(YNumber N) {
        if (N instanceof AddOp c) {
            return solve2(c);
        } else if (N instanceof MultiOp c) {
            return solve4(c);
        } else if (N instanceof MinusOp c) {
            return solve3(c);
        } else if (N instanceof DivOp c) {
            return solve5(c);
        } else if (N instanceof LiteralNum c) {
            return solve1(c);
        }else  if(N instanceof Suc c){
            return solve6(c);
        }else if(N instanceof Zero c){
            return solve7(c);
        }
        throw new RuntimeException();
    }
    private float solve1(LiteralNum i){
        return (float)i.i;
    }
    private float solve2(AddOp i){
        return solve(i.i)+solve(i.n);
    }
    private float solve3(MinusOp i){
        return solve(i.i)-solve(i.n);
    }
    private float solve4(MultiOp i){
        return solve(i.i)*solve(i.n);
    }
    private float solve5(DivOp i){
        return solve(i.i)/solve(i.n);
    }
    private float solve6(Suc i){
        return solve(i.i)+1;
    }
    private float solve7(Zero i){
        return 0;
    }
}

class LiteralNum implements YNumber {
    public LiteralNum(int i) {
        this.i = i;
    }
    public final int i;
}
class Zero implements YNumber{
}
class Suc implements YNumber{
    public final YNumber i;
    public Suc(YNumber i){
        this.i=i;
    }
}
class AddOp implements YNumber {
    public AddOp(YNumber i, YNumber n) {
        this.i = i;
        this.n = n;
    }
    public final YNumber i;
    public final YNumber n;
}
class MinusOp implements YNumber {
    public final YNumber i;
    public final YNumber n;
    public MinusOp(YNumber i, YNumber n) {
        this.i = i;
        this.n = n;
    }
}
class MultiOp implements YNumber {
    public final YNumber i;
    public final YNumber n;
    public MultiOp(YNumber i, YNumber n) {
        this.i = i;
        this.n = n;
    }
}
class DivOp implements YNumber {
    public final YNumber i;
    public final YNumber n;
    public DivOp(YNumber i, YNumber n) {
        this.i = i;
        this.n = n;
    }
}

零钱通

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Qishi {
    public static void main(String[] args) {
        Gong aaa=new Gong();
        aaa.menu();
    }
}

class Gong {
    Scanner sc = new Scanner(System.in);
    boolean loop = true;
    private static double balance = 0;
    private double money = 0;
    private String why;
    private String[] linqian = new String[0];
    Date times = new Date();
    SimpleDateFormat timeGes = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    String details = "----------零钱明细----------";

    public void menu() {
        do {
            System.out.println("----------菜单----------");
            System.out.println("1.零钱明细");
            System.out.println("2.收益入账");
            System.out.println("3.消费");
            System.out.println("4.退出");
            System.out.println("请选择(1-4):");
            int key = sc.nextInt();
            switch (key) {
                case 1:
                    minxi();
                    break;
                case 2:
                    shouru();
                    break;
                case 3:
                    xiaof();
                    break;
                case 4:
                    System.out.println("退出");
                    loop = false;
                    break;
                default:
                    System.out.println("不正确");
            }
        } while (loop);
        System.out.println("----------运行结束----------");
    }

    public void minxi() {
        System.out.println(details);
        for (int i = 0; i < linqian.length; i++) {
            System.out.println(linqian[i]);
        }
    }

    public void shouru() {
        System.out.println("收益入账:");
        money = sc.nextDouble();
        if (money >= 0) {
            balance += money;
            String[] Temp = new String[linqian.length + 1];
            for (int i = 0; i < linqian.length; i++) {
                Temp[i] = linqian[i];
            }
            linqian = Temp;
            linqian[linqian.length - 1] = "收益:"
                    + money + "\t时间" + timeGes.format(times) + "\t余额:" + balance;
        } else {
            System.out.println("输入有误");
        }

    }

    public void xiaof() {
        System.out.println("消费原因:");
        why = sc.next();
        System.out.println("消费金额:");
        money = sc.nextDouble();
        if (balance > money && money >= 0) {
            balance -= money;
            String[] Temp2 = new String[linqian.length + 1];
            for (int i = 0; i < linqian.length; i++) {
                Temp2[i] = linqian[i];
            }
            linqian = Temp2;
            linqian[linqian.length - 1] = "花费:"
                    + money + "\t时间" + timeGes.format(times) + "\t余额:" + balance + "\t消费原因:" + why;
        } else if (balance <= money) {
            System.out.println("喂喂喂,变穷光蛋了");
        } else {
            System.out.println("输入有误");
        }
    }
}
QQ:2219349024
最后更新于 2024-03-21