---------------------------------------------------------------------------------------------------
序列:
1.ATM自动银行系统
2.数组的遍历 后冒泡降序和冒泡升序
3.9x9口诀表的倒三角
4.用户名和密码登录验证
5. 创建一个手机类[一个标准类的写法]
------------------------------------------------------------------------------------------------------
1.ATM自动银行系统(switch do-while)
import java.util.Scanner;
public class BankSystem {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int choice;
do {
System.out.println("_____________________欢迎使用ATM自动银行服务_____________________");
System.out.println(" 1.开户 2.存款 3.取款 4.转账 5.查询余额 6.修改密码 0.退出 ");
System.out.println("______________________________________________________________");
System.out.println("请输入操作编号:");
choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("开户");
break;
case 2:
System.out.println("存款");
break;
case 3:
System.out.println("取款");
break;
case 4:
System.out.println("转账");
break;
case 5:
System.out.println("查询余额");
break;
case 6:
System.out.println("修改密码");
break;
case 0:
System.out.println("退出");
break;
default:
System.out.println("您输入的数字不合法,请您重新输入!");
break;
}
} while (choice<0 || choice>6);
}
}
2.数组的遍历 后冒泡降序和冒泡升序 数组遍历,冒泡排序详解
public class BubbleArray {
public static void main(String[] args) {
int[] arr = {12,32,3,43,34,23};
System.out.println("排序前对数组进行遍历:");
PrintArray(arr);
System.out.println("数组遍历后进行冒泡降序排列:");
BubbleJiang(arr);
PrintArray(arr);
System.out.println("数组遍历后进行冒泡升序排列:");
BubbleSheng(arr);
PrintArray(arr);
}
//冒泡降序排序
public static void BubbleJiang(int[] arr){
for(int i = 0;i<arr.length-1;i++){
for(int j = 0;j<arr.length-1-i;j++){
if(arr[j]<arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
//冒泡升序排序
public static void BubbleSheng(int[] arr){
for(int i = 0;i<arr.length-1;i++){
for(int j = 0;j<arr.length-1-i;j++){
if(arr[j]>arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
//遍历数组
public static void PrintArray(int[] arr){
System.out.print("[");
for(int x = 0;x<arr.length;x++){
if(x==arr.length-1){
System.out.println(arr[x]+"]");
}else {System.out.print(arr[x]+", ");}
}
}
}
3.9x9口诀表的倒三角
public class KouJue_9$9 {
public static void main(String[] args) {
//正三角
// for(int a=1;a<=9;a++){
// for(int b = 1;b<=a;b++){
// System.out.print(" "+b+"*"+a+"="+(a*b)+"\t");
// }
// System.out.println();
// }
//倒三角
for(int a=9;a>=1;a--){
for(int b = 1;b<=a;b++){
System.out.print(" "+b+"*"+a+"="+(a*b)+"\t");
}
System.out.println();
}
}
}
4.用户名和密码登录验证(String类型比较特殊,比较相等一般用equals())
mport java.util.Scanner;
public class YanZheng {
public static void main(String[] args) {
System.out.println("程序开始");
login();
System.out.println("程序结束");
}
public static void login(){
Scanner input = new Scanner(System.in);
System.out.println("请输入用户名:");
String Username = input.next();
System.out.println("请输入密码:");
String Password = input.next();
check(Username,Password);
}
public static void check(String Uname,String Pwd){
if("lhb".equals(Uname) && "990826".equals(Pwd)){//String比较特殊,所以用的equals()
System.out.println("登录成功!");
}else
{System.out.println("登陆失败!");}
}
}
5. 创建一个手机类
[一个标准类的写法------>包含无参构造方法和有参构造方法以及get xxx()和set xxx()方法]
属性有:品牌,颜色,价格,内存 行为有:打电话 ,发短信,玩游戏 进行封装(private)
/*
创建一个手机类
属性有:品牌,颜色,价格,内存
行为有:打电话 ,发短信,玩游戏
进行封装
*/
class Phone{
private String brand;//品牌
private String color;//颜色
private int price;//价格
private String memory;//内存
//无参构造函数
public Phone(){ }
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getMemory() {
return memory;
}
public void setMemory(String memory) {
this.memory = memory;
}
//有参构造函数方法
public Phone(String brand,String color,int price,String memory){
this.brand= brand;
this.color =color;
this.price= price;
this.memory=memory;
}
//成员方法
public String call(){
return "打电话给警察叔叔";
}
public void SendMsg(){
System.out.println("给女朋友发短信");
}
public void PlayGame(String game){
System.out.println("和女朋友玩"+game);
}
}
public class PhoneTest {
public static void main(String[] args) {
//创建一个手机对象
Phone p =new Phone();
p.setBrand("iPhone12 pro");
p.setColor("星空紫");
p.setPrice(12999);
p.setMemory("256G");
System.out.println("手机的品牌为:"+p.getBrand()+"\n"+"手机的颜色为:"+p.getColor()+"\n"
+"手机的价格为:"+p.getPrice()+"\n"+"手机的内存大小为:"+p.getMemory()+"\n");
Phone p1 = new Phone("小米11 pro","缤纷蓝",49999,"16G");
System.out.println("手机的品牌为:"+p1.getBrand()+"\n"+"手机的颜色为:"+p1.getColor()+"\n"
+"手机的价格为:"+p1.getPrice()+"\n"+"手机的内存大小为:"+p1.getMemory()+"\n");
String c = p.call();
System.out.println(c);
p.SendMsg();
p.PlayGame("王者荣耀");
}
}