商品录入查询
package day0604; import java.util.Scanner; public class Test1 { static Item[] items = new Item[5]; public static void main(String[] args) { String[] name = {"商品1","商品2","商品3","商品4","商品5"}; double[] price = {123.33,234.33,2423.32,2423.42,23423.22}; int[] num = {234,234,2,23432,342}; for (int i = 0; i < items.length; i++) { items[i] = new Item(); items[i].name = name[i]; items[i].price = price[i]; items[i].number = num[i]; } outer: while(true) { menu(); int i = new Scanner(System.in).nextInt(); switch (i) { case 1: f1();break; case 2: f2();break; case 3: f3();break; case 4: f4();break; case 5: break outer; } } } private static void menu() { System.out.println("1:商品列表"); System.out.println("2:商品录入"); System.out.println("3:商品查询"); System.out.println("4:统计信息"); System.out.println("5:退出"); System.out.print("选择:"); } private static void f4() { double totalPrice = 0; double allPrice = 0; double maxTotalPrice = 0; double maxPrice = 0; for (int i=0;i<items.length;i++) { totalPrice += items[i].price*items[i].number; allPrice += items[i].price; if(items[i].price > maxPrice) maxPrice = items[i].price; if(items[i].price*items[i].number > maxTotalPrice) maxTotalPrice = items[i].price*items[i].number; } System.out.println("总价:"+totalPrice); System.out.println("单价均价:"+allPrice/items.length); System.out.println("最高总价:"+maxTotalPrice); System.out.println("最高单价:"+maxPrice); } private static void f3() { System.out.print("输入要查询的商品名:"); String s = new Scanner(System.in).nextLine(); for (int i=0;i<items.length;i++) { if(s.equals(items[i].name)) { System.out.println(items[i]); return; } } System.out.println("找不到商品"); } private static void f2() { for (int i=0;i<items.length;i++) { System.out.println("录入第"+(i+1)+"件商品:"); System.out.print("名称:"); items[i].name = new Scanner(System.in).nextLine(); System.out.print("价格:"); items[i].price = new Scanner(System.in).nextDouble(); System.out.print("数量:"); items[i].number = new Scanner(System.in).nextInt(); } f1(); } private static void f1() { for(int i=0;i<items.length;i++) { System.out.println(items[i]); } } } package day0604; public class Item { String name; double price; int number; @Override public String toString() { return "商品名称:"+this.name+"价格:"+this.price+"数量:"+this.number; } }
随机抽奖
package day0605; import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class Test1 { public static void main(String[] args) { String s = "A;0/B;0/C;0"; String[] split = s.split("/"); Student[] t = new Student[split.length]; for (int i = 0; i < split.length; i++) { String[] temp = split[i].split(";"); Student stu = new Student(); stu.name = temp[0]; stu.times = Integer.parseInt(temp[1]); t[i] = stu; } System.out.print("按回车抽奖:"); while (true) { new Scanner(System.in).nextLine(); Student[] r = new Student[2]; for (int i = 0; i < r.length; i++) { int j; do { j = new Random().nextInt(t.length); }while (t[j].flag); t[j].flag = true; t[j].times++; r[i] = t[j]; } for (int i = 0; i < r.length; i++) { System.out.println(r[i]); r[i].flag = false; } } } } package day0605; public class Student { String name; int times; boolean flag; @Override public String toString() { return name+";"+times; } }
AK47
package day0606; import java.util.Random; public class AK47 { int bullets = 30; public void shoot() { if(bullets == 0) { System.out.println("没有子弹了"); return; } int d = new Random().nextInt(9)+1; if(d>bullets) d = bullets; bullets -= d; System.out.println("发射了"+d+"发子弹,还剩余"+bullets); } public void reload() { bullets = 30; } } package day0606; import java.util.Scanner; public class Test1 { public static void main(String[] args) { AK47[] a = {new AK47(),new AK47()}; System.out.print("按回车发射"); while (true) { String s = new Scanner(System.in).nextLine(); if(s.equals("load1")) a[0].reload(); else if(s.equals("load2")) a[1].reload(); else { a[0].shoot(); a[1].shoot(); } } } }