| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 513 人关注过本帖
标题:计算器问题
只看楼主 加入收藏
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
结帖率:41.67%
收藏
 问题点数:0 回复次数:2 
计算器问题
这个计算器程序就是如果用户输入“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
hsh_1987
Rank: 5Rank: 5
等 级:职业侠客
威 望:4
帖 子:92
专家分:343
注 册:2010-3-30
收藏
得分:0 
如果按程序运行不出错的话,
StringTokenizer str = new StringTokenizer(c, "add mul sub reset help exit");或StringTokenizer str = new StringTokenizer(c, "aehmrs");//只要把你条件句出现的字符写上去就行了

 

努力很重要,决心比努力更重要...........
2010-04-04 12:26
dadongzicool
Rank: 6Rank: 6
等 级:贵宾
威 望:11
帖 子:209
专家分:474
注 册:2009-3-20
收藏
得分:0 
package cn.dadongzicool.scjp.test8;

import java.util.Scanner;
import java.util.StringTokenizer;

public class HwTwo
{
   public static void main(String[] args)
    {
        double result=0, number = 0, 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, " ");//这里修改了一下 用空格作为分隔符
            
            str.nextToken();
            while(str.hasMoreTokens()){
                number=Double.parseDouble(str.nextToken());  
            }//这里修改了下 当输入help reset 等时该字符串只有一个语言单词 此时while循环不执行
             //当输入add 5之类的命令时,str。nextToken()先取走了单词add 然后执行while循环 把5赋值给number
            
            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);
    }
}

本人出售软件专业毕业设计(包括论文,代码)题目众多,价格从优联系方式:QQ
1635008514(寻找高校代理)
2010-04-05 11:51
快速回复:计算器问题
数据加载中...
 
   



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

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