①:迷你收款
package Z1;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("*******************************");
System.out.println("请选择购买的商品编号:");
System.out.println("1.T恤\t2.网球鞋\t3.网球拍");
System.out.println("*******************************");
String xuan;
double sum = 0;
do {
System.out.println("请输入要购买商品编号");
int a = input.nextInt();
System.out.println("请输入要购买商品数量");
int b = input.nextInt();
switch (a) {
case 1:
sum = 245 * b;
break;
case 2:
sum = 300 * b;
break;
case 3:
sum = 400 * b;
break;
}
System.out.println("是否继续购买(y/n)");
xuan = input.next();
} while (xuan.equals('y'));
System.out.println("折扣:0.8");
System.out.println("应付金额:"+sum*0.8);
System.out.print("交钱:");
double num = input.nextDouble();
System.out.println("找钱:"+(num-sum*0.8));
}
}
②:升级版计算器
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String flag = "y";
do {
System.out.println("=========选择计算========");
System.out.println("1.计算器");
System.out.println("2.面积计算");
System.out.println("3.退出");
System.out.println("=====输入您要选择的功能=====");
int i = input.nextInt();
double result = 0;
switch (i) {
case 1:
do {
System.out.println("=======计算器======");
System.out.println("1.+");
System.out.println("2.-");
System.out.println("3.*");
System.out.println("4./");
System.out.println("5.返回主菜单");
System.out.println("6.退出");
System.out.println("=================");
System.out.println("请选择:");
i = input.nextInt();
if (i < 5) {
System.out.println("请输入第一个数");
double a = input.nextInt();
System.out.println("请输入第二个数");
double b = input.nextInt();
switch (i) {
case 1:
result = a + b;
break;
case 2:
result = a - b;
break;
case 3:
result = a * b;
break;
case 4:
result = a / b;
break;
case 5:
break;
default:
flag = "n";
break;
}
}
System.out.println(result);
} while (i < 5);
case 2:
do {
System.out.println("=======计算面积=====");
System.out.println("1.三角形");
System.out.println("2.圆形");
System.out.println("3.长方形");
System.out.println("4.返回主菜单");
System.out.println("5.退出");
System.out.println("=================");
System.out.println("请选择:");
i = input.nextInt();
switch (i) {
case 1:
System.out.println("请输入三角形的底");
double c = input.nextDouble();
System.out.println("请输入三角形的高");
double d = input.nextDouble();
result = c * d * 0.5;
break;
case 2:
System.out.println("请输入圆的半径");
double e = input.nextDouble();
result = 3.14 * e * e;
break;
case 3:
System.out.println("请输入长方形的底");
double f = input.nextDouble();
System.out.println("请输入长方形的高");
double g = input.nextDouble();
result = f * g;
break;
case 4:
break;
default:
flag = "n";
break;
}
System.out.println(result);
} while (i < 4);
break;
}
} while (flag.equals('y'));
System.out.println("您已退出");
}
}