Java基础黑马程序员教材 案例2.3、2.6代码详解

一、2.3超市购物程序

import java.util.Scanner;

public class test2_3 {
     
    //超市购物程序
  static   double cash=0.0;
    public  static double buy(){
     
        System.out.println("你要买什么?输入id");
        Scanner scanner=new Scanner(System.in);
        int id=1;
        id=scanner.nextInt();
        System.out.println("买多少份?");
        int num=scanner.nextInt();
        switch (id){
     
            default:
                System.out.println("输入的不是商品号");
                break;
            case  1:
                cash+=8.8*num;
                break;
            case 2:
                cash+=10.0*num;
                break;
            case 3:
                cash+=18.8*num;
                break;
            case 4:
                cash+=12.5*num;
                break;
            case 5:
                cash+=15.5*num;
        }
        return  cash;
    }
    public static void main(String[] args) {
     
        buy();

        Scanner scanner=new Scanner(System.in);
        String choicey="Y";
        String choicen="N";
        System.out.println("还要买吗?要买输入Y,不买输入N");
        String ch=scanner.nextLine();
        if(ch.equals(choicey)){
     
             cash=buy();
        }
        else if(ch.equals(choicen)){
     
            System.out.println("总共花了"+cash);
            System.exit(1);
        }
        System.out.println("总共花了"+cash);
    }
}

二、2.6登陆注册

import java.util.HashMap;
import java.util.Scanner;

public class test2_6 {
     
 static    HashMap<Integer,String> message=new HashMap<>();
public  static void login(){
     
    System.out.println("这里是登陆界面");
    System.out.println("请输入账号密码");
    Scanner scanner=new Scanner(System.in);
   int number=scanner.nextInt();
    Scanner scanner1=new Scanner(System.in);
    String password=scanner1.nextLine();
    if(message.get(number).equals(password)){
     
        System.out.println("登陆成功");
    }
    else {
     
        System.out.println("密码不对哦,或者账号不存在");
    }
}
public  static void register(){
     
    System.out.println("这里是注册界面");
    System.out.println("请输入账号和密码一行一个,账号不能重复,长度仅限6位数");
    System.out.println("tips:账号纯数字,密码你随意");
    System.out.println("先输入账号吧,宝儿");
    Scanner scanner=new Scanner(System.in);
    int  number=scanner.nextInt();
    System.out.println("输密码");
    Scanner scanner1=new Scanner(System.in);
    String password=scanner1.nextLine();
    message.put(number, password);

}
public static void check(){
     

    System.out.println(message);

}
public  static void exit(){
     
System.exit(0);
}
    public static void main(String[] args) {
     

        while (true){
     
            System.out.println("这里是登陆注册案例");
            System.out.println("请选择要做的操作:");
            System.out.println("1.登陆");
            System.out.println("2.注册");
            System.out.println("3.查看");
            System.out.println("4.退出");
            Scanner scanner=new Scanner(System.in);
            int choice=scanner.nextInt();
            switch (choice){
     
                default:
                    System.out.println("选错了。再来");
                    break;
                case 1:
                    login();
                    break;
                case 2:
                    register();
                    break;
                case 3:
                    check();
                    break;
                case 4:
                    exit();
                    break;
            }
        }

    }
}

你可能感兴趣的:(java)