没有处理非法输入...
import java.util.Scanner;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SmallChange {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
boolean sysOn = true;
int balance = 0;
String breakdown = "";
do {
clearScreen();
System.out.println("-----------------SmallChange------------------\n"
+ "\t 1 Breakdown\n"
+ "\t 2 Proceeds\n"
+ "\t 3 Expenditure\n"
+ "\t 4 Exit\n"
+ "-----------------SmallChange------------------");
System.out.println("Please select the required operation(from 1 to 4):");
int op = in.nextInt();
switch(op) {
case 1:
clearScreen();
System.out.println("------------------Breakdown-------------------");
System.out.println(breakdown);
System.out.println("------------------Breakdown-------------------");
System.out.println("Press Enter to return...");
in.nextLine();
in.nextLine();
break;
case 2:
clearScreen();
System.out.println("-------------------Proceed--------------------");
System.out.println("Please input the proceed:");
int proceed = in.nextInt();
balance += proceed;
String proceedBD = "\nProceed:" + proceed + "\t" + getCurrentTime() + "\t"
+ "Balance:" + balance;
breakdown += proceedBD;
System.out.println("Operation sucessful!");
System.out.println("Press Enter to return...");
in.nextLine();
in.nextLine();
break;
case 3:
clearScreen();
System.out.println("-------------------Expenditure--------------------" );
System.out.println("Please input the expenditure item:");
String item = in.next();
System.out.println("Please input the expenditure:");
int expenditure = in.nextInt();
balance -= expenditure;
String expenditureBD = "\n" + item + ":" + expenditure + "\t" + getCurrentTime() + "\t"
+ "Balance:" + balance;
breakdown += expenditureBD;
System.out.println("Press Enter to return...");
in.nextLine();
in.nextLine();
break;
case 4:
sysOn = false;
System.out.println("System is exiting...");
default:
break;
}
} while(sysOn);
in.close();
System.out.println("Exit success!");
}
public static void clearScreen() {
try {
String operatingSystem = System.getProperty("os.name");
if (operatingSystem.contains("Windows")) {
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "cls");
Process process = pb.inheritIO().start();
process.waitFor();
} else {
ProcessBuilder pb = new ProcessBuilder("clear");
Process process = pb.inheritIO().start();
process.waitFor();
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
public static String getCurrentTime() {
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
return simpleDateFormat.format(date);
}
}