| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 513 人关注过本帖
标题:计算器问题
取消只看楼主 加入收藏
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
结帖率:41.67%
收藏
 问题点数:0 回复次数:0 
计算器问题
这个计算器程序就是如果用户输入“add 5" 就+5, ”mul 5" 就程5, 按“help"就弹出指令信息, 按”reset"就归零, 按“exit"就推出
目前我的程序+ - * / 都没问题, 问题就在按help, exit, reset的时候,程序就坏掉了, 希望大家帮我指出问题出在哪了。谢谢

import java.util.Scanner;//Needed for the Scanner class
import *;
import java.util.StringTokenizer;

public class HwTwo
{
   public static void main(String[] args)
    {
        double result=0, number, flag=0;
        String a, b, c, i;
        char d;
        boolean spaceKeyPressed = false;
        
        Scanner keyboard=new Scanner (System.in);
        System.out.println("Total is " + result);
        do
        {
            System.out.print("Command? ");
        
            b=keyboard.nextLine();
            c=b.trim();
        
            StringTokenizer str = new StringTokenizer(c, "add mul sub");
        
            number=Double.parseDouble(str.nextToken());   
                           
            if (c.startsWith("a")||c.startsWith("A"))
            {
                result+=number;
            }
        
            else if (c.startsWith("e")||c.startsWith("E"))
            {
                flag=1;
            }
            else if (c.startsWith("h")||c.startsWith("H"))
            {
                System.out.println("add     Add an int to total");
                System.out.println("exit    Exit the program");
                System.out.println("help    Display this text");
                System.out.println("mul     Multiply total by an int");
                System.out.println("reset   Set total to zero");
                System.out.println("sub     Subtract an int from total");
            }

            else if (c.startsWith("m")||c.startsWith("M"))
            {
                result*=number;
            }
            
            else if (c.startsWith("r")||c.startsWith("R"))
            {
                result=0;
            }
            
            else if (c.startsWith("s")||c.startsWith("S"))
            {
                result-=number;
            }

            System.out.println("total is "+result);
        }while (flag==0.0);
    }
}
搜索更多相关主题的帖子: 计算器 
2010-04-04 09:10
快速回复:计算器问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034166 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved