Application cases of Java classes and objects and methods

1. Code and renderings

1. Class and object cases and renderings

Case: Correctly simulate and implement user password management: input the old user name and password, if they are correct, only have the authority to update; obtain the new password from the keyboard and update; if it is incorrect, a prompt will be given.

The code is as follows (example):

package day06.work6;

import java.util.Scanner;

public class TestUsers {
    
    
    public static void main(String[] args) {
    
    
        //创建一个管理员账号对象
        Users u = new Users();
        //给对象的属性赋值
        u.name="admin";
        u.psw="111111";
        //接收用户输入的账号和密码
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String name=sc.next();
        System.out.println("请输入密码:");
        String psw = sc.next();
        //调用修改密码方法
        u.updatePsw(name,psw);
    }
}

package day06.work6;

import java.util.Scanner;

public class Users {
    
    
    String name;
    String psw;

    public void updatePsw(String name1,String psw1){
    
    
       Scanner sc = new Scanner(System.in);
        //比较,修改密码
        if (name.equals(name1)&&psw.equals(psw1)){
    
    
            System.out.println("请输入新密码:");
            psw = sc.next();
            System.out.println("修改密码成功,您的新密码为:"+psw);
        }else {
    
    
            System.out.println("用户名和密码不匹配,您没有权限修改信息!");
        }
    }
}


Insert picture description here
Insert picture description here

2. Method cases and renderings

Case: Existing TV product price guessing activities, the rules of the activity: a product name appears randomly, the user guesses its value, if the guess is correct within the specified number of times, the product can be obtained. Simulate quiz activities.
The code is as follows (example):

package day0708.work001;

import java.util.Scanner;

public class TestGoods {
    
    
    //随机出一个商品
    private static Goods init() {
    
    
        //1.创建3个商品
        Goods g1 = new Goods("公主电动车", 2000);
        Goods g2 = new Goods("王子洗衣机", 3000);
        Goods g3 = new Goods("白马微波炉", 500);
        //2.存入一个对象数组
        Goods [] gs={
    
    g1,g2,g3};
        //3.随机返回一个商品
        int index= (int) Math.floor(Math.random()*3);//0,1,2
        return gs[index];
    }

    //猜价格
    private static void guess(Goods goods) {
    
    
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入"+goods.name+"的价格:");
        int i=0;
        do {
    
    
            int X = sc.nextInt();
            if(goods.price==X){
    
    
                System.out.println(goods.name+"拿走吧!");
                return;
            }else if(goods.price>X){
    
    
                if(i<3)
                    System.out.println("再大点!");
                else
                    break;
            }else{
    
    
                if(i<3)
                    System.out.println("再小点!");
                else
                    break;
            }
            System.out.println("再猜一次吧:");
            i++;
        }while (i<4);
        System.out.println("4次内没有猜对,下次努力吧!");
    }


    public static void main(String[] args) {
    
    
        //1.随机出一个商品
        Goods g =init();
        //2.猜价格
        guess(g);
    }



}


package day0708.work001;

public class Goods {
    
    
    String name;
    int price;

    public Goods(String name, int price) {
    
    
        this.name = name;
        this.price = price;
    }

    public Goods() {
    
    
    }
}

Insert picture description here
Insert picture description here

to sum up

The above is the content of classes and objects and methods, mainly using the technology of creating objects to call methods by classes.

Guess you like

Origin blog.csdn.net/StruggleBamboo/article/details/111070155