自分で家計簿を書きます。
第一歩はまず需要を分析して何かの変数int balance=10000が必要です。 String details=「収支\t口座金額\t収支金額」tは 明「+」「 bollan loopFlags=true;先に枠を書いて循環で選択枠を印刷し、キーボードから選択項目を入力して終了するには、対応するメカニズムが必要です。
package com.xatu.edu;
public class shouzhi {
public static void main(String[] args) {
// ,10000
//
//
//
// 1
// 2
int balance = 10000;
String details = " \t \t \t "+"
";
boolean loopFlag = true;
while (loopFlag) {
System.out.println("----------------- -----------------");
System.out.println(" 1 ");
System.out.println(" 2 ");
System.out.println("3 ");
System.out.println("4 ");
System.out.println(" (1-4):");
char userInput = Utility.readMenuSelection();
switch (userInput) {
case '1':
System.out.println(details);
break;
case '2':
System.out.println(" ");
int money = Utility.readNumber();
System.out.println(" ");
String info = Utility.readString();
balance=balance+money;
String str =" \t"+balance+"\t"+money+"\t"+info+"
";
details +=str;
break;
case '3':
break;
case '4':
System.out.println(" ,");
char confirm = Utility.readConfirmSelection();
if(confirm=='Y') {
loopFlag = false;}
break;
default:
break;
}
}
}
}
package com.xatu.edu;
import java.util.*;
public class Utility {
private static Scanner scanner = new Scanner(System.in);
public static char readMenuSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1);
c = str.charAt(0);
if (c != '1' && c != '2' && c != '3' && c != '4') {
System.out.print(" , :");
} else break;
}
return c;
}
public static int readNumber() {
int n;
for (; ; ) {
String str = readKeyBoard(4);
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print(" , :");
}
}
return n;
}
public static String readString() {
String str = readKeyBoard(8);
return str;
}
public static char readConfirmSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1).toUpperCase();
c = str.charAt(0);
if (c == 'Y' || c == 'N') {
break;
} else {
System.out.print(" , :");
}
}
return c;
}
private static String readKeyBoard(int limit) {
String line = "";
while (scanner.hasNext()) {
line = scanner.nextLine();
if (line.length() < 1 || line.length() > limit) {
System.out.print(" ( " + limit + ") , :");
continue;
}
break;
}
return line;
}
}