A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 645420297 中级黑马   /  2013-4-10 15:56  /  1284 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 645420297 于 2013-4-21 10:47 编辑
  1. class FamilyAccount
  2. {
  3. public static void main(String[] args)
  4. {
  5. int balance = 10000;
  6. String details="收支\t账户金额\t收支金额\t说明";
  7. boolean loopFlag=true;
  8. int h = 0;
  9. String s1="";
  10. do
  11. {

  12. System.out.println("\n--------------------家庭收支 记账软件--------------------");
  13. System.out.println();
  14. System.out.println("\t"+ "1 收支明细");
  15. System.out.println("\t"+ "2 登记收人");
  16. System.out.println("\t"+ "3 登记支出");
  17. System.out.println("\t"+ "4 退 出");
  18. System.out.println();
  19. System.out.println();
  20. System.out.print("\t"+"请选择(1-4):");
  21. //从键盘上读取1,2,3,4四个字符
  22. char c=Utility.readMenuSelection();
  23. switch(c){
  24. case'1':
  25. System.out.println("--------------------当前收支明细记录---------------------");
  26. System.out.println( details+"\n"+"收入"+"\t"+balance+"\t\t"+h+"\t\t"+s1);
  27. System.out.println();
  28. System.out.println();
  29. break;
  30. case'2':
  31. System.out.print("本次收人金额:");
  32. int n=Utility.readNumber();
  33. //账户金额累加
  34. //怎样用更简单的方法把已经累加的结果返回到details中?
  35. balance+=n;
  36. h=balance-10000;
  37. System.out.print("本次收支说明");
  38. String s=Utility.readString();
  39. s1+=s;
  40. break;
  41. case'3':
  42. System.out.print("本次支出金额:");
  43. int z=Utility.readNumber();//读取支出的金额
  44. System.out.println("本次支出说明");
  45. String o=Utility.readString();
  46. break;
  47. case'4':
  48. System.out.print("确认是否退出(Y/N):");
  49. char m=Utility.readConfirmSelection();
  50. //如果Y,将标志位反转,当运行到While循环末尾时
  51. //条件不成立,故跳出循环
  52. if (m=='Y')
  53. {
  54. loopFlag=false;
  55. }
  56. break;
  57. }
  58. } while (loopFlag);
  59. }
  60. }

复制代码

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

3 个回复

倒序浏览
问题就在代码中 这是引用的功能
  1. import java.util.*;

  2. public class Utility {
  3.     private static Scanner scanner = new Scanner(System.in);
  4.    
  5.         public static char readMenuSelection() {
  6.         char c;
  7.         for (; ; ) {
  8.             String str = readKeyBoard(1);
  9.             c = str.charAt(0);
  10.             if (c != '1' && c != '2' && c != '3' && c != '4') {
  11.                 System.out.print("选择错误,请重新输入:");
  12.             } else break;
  13.         }
  14.         return c;
  15.     }

  16.     public static int readNumber() {
  17.         int n;
  18.         for (; ; ) {
  19.             String str = readKeyBoard(4);
  20.             try {
  21.                 n = Integer.parseInt(str);
  22.                 break;
  23.             } catch (NumberFormatException e) {
  24.                 System.out.print("数字输入错误,请重新输入:");
  25.             }
  26.         }
  27.         return n;
  28.     }
  29.     }

  30.     public static char readConfirmSelection() {
  31.         char c;
  32.         for (; ; ) {
  33.             String str = readKeyBoard(1).toUpperCase();
  34.             c = str.charAt(0);
  35.             if (c == 'Y' || c == 'N') {
  36.                 break;
  37.             } else {
  38.                 System.out.print("选择错误,请重新输入:");
  39.             }
  40.         }
  41.         return c;
  42.     }

  43.     private static String readKeyBoard(int limit) {
  44.         String line = "";

  45.         while (scanner.hasNext()) {
  46.             line = scanner.nextLine();
  47.             if (line.length() < 1 || line.length() > limit) {
  48.                 System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
  49.                 continue;
  50.             }
  51.             break;
  52.         }

  53.         return line;
  54.     }
  55. }
复制代码
回复 使用道具 举报
本帖最后由 王军行 于 2013-4-10 16:37 编辑

第二张代码31行多一个“}”

第一页s1+=s; 中s 未定义
Utility类readString()方法没有

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

回复 使用道具 举报
如果问题未解决,请继续追问,如果问题解决了,请将问题分类改为“已解决”,谢谢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马