Java基础黑马程序员教材 案例2.3、2.6代码详解
一、2.3超市购物程序
import java.util.Scanner;
public class test2_3 {//超市购物程序
staticdouble cash=0.0;
publicstatic 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;
case1:
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;
}
returncash;
}
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 { staticHashMap message=new HashMap<>();
publicstatic 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("密码不对哦,或者账号不存在");
}
}
publicstatic void register(){System.out.println("这里是注册界面");
System.out.println("请输入账号和密码一行一个,账号不能重复,长度仅限6位数");
System.out.println("tips:账号纯数字,密码你随意");
System.out.println("先输入账号吧,宝儿");
Scanner scanner=new Scanner(System.in);
intnumber=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);
}
publicstatic 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(抽象类与接口的区别&重载与重写&内存泄漏)
- 事件代理
- Java|Java OpenCV图像处理之SIFT角点检测详解
- java中如何实现重建二叉树
- 数组常用方法一
- Python基础|Python基础 - 练习1
- 【Hadoop踩雷】Mac下安装Hadoop3以及Java版本问题
- Java|Java基础——数组
- RxJava|RxJava 在Android项目中的使用(一)
- java之static、static|java之static、static final、final的区别与应用