Java practice (1) (Yang Hui's triangle, guessing numbers, name roll, etc.)

I. Introduction

With the Java foundation, we can solve some simple problems ヾ(✿゚▽゚)ノ

2. Yang Hui Triangle

Recursively print Yang Hui's triangle, line is the row, col is the column, return 1 when the column is 1 or the column is equal to the row

public class YangHui {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入杨辉三角形的行数");
        int line = scanner.nextInt();
        for (int i = 1; i <= line; i++) {
    
    
            for (int j = 1; j <= i; j++) {
    
    
                System.out.print(Tri(i, j));
            }
            System.out.println();
        }
    }
    //递归算法
    public static int Tri(int line,int col){
    
    
        return (col==1||col==line)?1:Tri(line-1,col-1)+Tri(line-1,col);
    }
}

[Explanation] In the Tri() method, the ternary operator is used (satisfied? Satisfied, execute A; not satisfied, execute B). The running result is not beautiful, and the number of spaces can be controlled to modify.

insert image description here

3. Guess the number (random caller)

Guess the number, if you guess wrong, you will be prompted to guess big or small, until you guess right

1. Guess the number
Use Random to randomly generate a number from 0-9, and then input a number from the keyboard through Scanner

public class GuessNumber {
    
    
    public static void main(String[] args) {
    
    
        int randomNum,guessNum;
        Random random=new Random();
        randomNum=random.nextInt(10);
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入一个0-9的数字");
        guessNum=scanner.nextInt();
        while(guessNum!=randomNum){
    
    
            if(guessNum>randomNum)
                System.out.println("给爷继续猜,你猜大了!");
            else
                System.out.println("给爷继续猜,你猜小了!");
            guessNum= scanner.nextInt();
        }
        System.out.println("恭喜你,猜对了!");
    }
}

[Description] 10 in random.nextInt is the range, which means 0-9, excluding 10

2. Roll call device

Enter the names of three students, traverse them, and finally display the people who were named

public class CallName {
    
    
    public static void main(String[] args) {
    
    
        String[] arr=new String[3];
        Scanner scanner=new Scanner(System.in);
        //随机点名器
        showInfo();
        //存储
        for(int i=0;i<arr.length;i++){
    
    
            System.out.println("存储第"+(i+1)+"个姓名");
            arr[i]=scanner.nextLine();
        }
        //遍历
        for(int i=0;i< arr.length;i++){
    
    
            System.out.println("第"+(i+1)+"个学生姓名:"+arr[i]);
        }
        System.out.println("被点到名的同学是:"+randomName(arr));
    }
    //随机点名方法
    public static String randomName(String arr[]){
    
    
        Random random=new Random();
        int index=random.nextInt(arr.length);
        return arr[index];
        }
        //前言介绍
    public static void showInfo(){
    
    
        System.out.println("——————随机点名器——————");
    }
     }

[Description] Similar to guessing the number above, but the range of nextInt becomes the length of the array, and returns the String type array element at the random number (array subscript) through the randomName method

insert image description here

4. Supermarket shopping

If customer A wants to buy good juice, and the supermarket has good juice, then output that A bought good juice in XX supermarket; if he can’t buy it, then output A and bought nothing in XX supermarket! What a waste of time!

Supermarket class (only one encapsulated supermarket name)

//超市
class Market{
    
    
    private String marketName;
    Market(String marketName){
    
    
        this.marketName=marketName;
    }
    public void setMarketName(String marketName) {
    
    
        this.marketName = marketName;
    }
    public String getMarketName() {
    
    
        return marketName;
    }
}

Customer class (customer name and customer demand)

//购物者
class Person{
    
    
    private String name;
    private String personNeed;
    Person(String name,String personNeed){
    
    
        this.name=name;
        this.personNeed=personNeed;
    }
    public String getName() {
    
    
        return name;
    }
    public String getPersonNeed() {
    
    
        return personNeed;
    }
}

Product class (only the product name is encapsulated)

//产品
class Product{
    
    
    private String productName;
    Product(String productName){
    
    
        this.productName=productName;
    }
    public String getProductName() {
    
    
        return productName;
    }
}

The main program SuperMarket (through Arraylist, an upgraded version of the array, artificially add data, and then traverse for comparison)

public class SuperMarket {
    
    
    //前言
    public static  void showInfo(){
    
    
        System.out.println("——————超市购物程序——————");
    }
    //主函数
    public static void main(String[] args) {
    
    
        //展示前言
        showInfo();
        //flag标记是否有这个商品
        boolean flag=false;
        //实例化购物者
        Person person=new Person("虎哥","歌姬证书");
        //实例化超市
        Market market=new Market("东百超市");
        //实例化产品
        ArrayList<Product> productArray=new ArrayList<>();
        Product p1=new Product("好果汁");
        Product p2=new Product("歌姬证书");
        Product p3=new Product("唐老鸭声卡");
        Product p4=new Product("沈阳冰红茶");
        productArray.add(p1);
        productArray.add(p2);
        productArray.add(p3);
        productArray.add(p4);
        for(int i=0;i< productArray.size();i++){
    
    
            String items=(productArray.get(i)).getProductName();
            if(items==person.getPersonNeed()) {
    
    
                flag = true;
                break;
            }
        }
        if(flag)
            System.out.println(person.getName()+" 在 "+market.getMarketName()+" 买到了 "+person.getPersonNeed());
        else
            System.out.println(person.getName()+" 在 "+market.getMarketName()+" 什么也没有买到!白跑了一趟!");
    }
}

Customer name: Brother Hu, demand: good juice, whether it is successful: Yes
insert image description here
Customer name: Brother Dao, demand: lighter, whether it is successful: No
insert image description here

Guess you like

Origin blog.csdn.net/MODAX/article/details/123568057