JAVA实现小鲨鱼记账系统
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String details = "";
int balance = 0;
System.out.println("------欢迎使用系统-------");
System.out.println("1,收支平衡\n2,登记收入\n3,登记支出\n4,退出");
System.out.println("请输入你要选择的功能");
Scanner sc = new Scanner(System.in);
while (true) {
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("账单明细");
System.out.println();
System.out.println(details);
break;
case 2:
System.out.println("登记收入");
System.out.print("输入收入金额:");
int money = sc.nextInt();
System.out.print("\n请输入收入原因");
String reason = sc.next();
balance += money;
details += "收入说明:" + reason + ",收入金额:" +
money + ",余额:" + balance + '\n';
System.out.print(details);
break;
case 3:
System.out.println("登记支出");
System.out.print("输入支出金额:");
int pay = sc.nextInt();
System.out.print("\n请输入收入原因:");
String payReason = sc.next();
balance -= pay;
details += "收入说明:" + payReason + ",收入金额:" +
pay + ",余额:" + balance + '\n';
System.out.print(details);
break;
case 4:
System.out.println("欢迎使用小鲨鱼系统\n退出");
return;//注意整个程序停止
default:
System.out.println("输入命令错误");
break;//只能跳出当前循环
}
}
}
}
笔记:注意return 终止程序;
因为next方法遇到了空格停止接收,所以只输出了abcd,nextLine方法开始从空格处开始接收直到遇到了第一个回车那么结束接收,说明nextLine方法可以接收空格,next方法遇到空格等会停止接收