English 中文(简体)
如何改进我的货币兑换方案?
原标题:How to improve my currency exchange program?
  • 时间:2010-11-09 21:51:49
  •  标签:
  • java

我撰写了一份 Java货币兑换方案,其中我为用户设计了3种外汇,从美元改为美元。 当用户投入“000”时,我想打破<代码>。

import java.lang.StringBuffer;
import java.io.IOException;

class Project6_5
{
    public static void main(String args[])
    {
        int usa, amount, stop;
        System.out.println("This is a Currencies Exchange program");
        System.out.println("From US Dollar to Foreign Currency");        //Explaining to user
        stop = 0;
        while(true/*stop!=999*/)
        {
            try
            {
                System.out.print("What is your amount of US dollars?");
                System.out.println("                        000 to end Program");
                usa = Integer.parseInt(GCS());
                System.out.println("What currency would you prefer?");
                System.out.println("Avaliable currencies are:");
                System.out.println("1 for Japanese Yen, 2 for Taiwanese Dollar, 3 for Euro");
                amount = Integer.parseInt(GCS());
                if( amount == 1 )
                {
                    System.out.println("Exchange rate");
                    System.out.println("80.75 Yen for 1 Dollar");
                    System.out.println(" ");
                    System.out.println("Your Yen is "+ usa*80.75);
                    System.out.println("________________________________________");
                }
                if( amount == 2 )
                {
                    System.out.println("Exchange rate");
                    System.out.println("30.125 NTD for 1 Dollar");
                    System.out.println(" ");
                    System.out.println("Your NTD is "+ usa*30.125);
                    System.out.println("________________________________________");
                }
                if( amount == 3 )
                {
                    System.out.println("Exchange rate");
                    System.out.println("1.4201 Euro for 1 Dollar");
                    System.out.println(" ");
                    System.out.println("Your Euro is "+ usa*1.4201);
                    System.out.println("________________________________________");
                }
                if(usa==000)
                {
                    System.out.println("End Program");
                    return;
                }       
            }
            catch(NumberFormatException NFE)  //catch if integer s not number
            {
                System.err.println("ERROR");
                System.err.println("Type in Integer only");
            }
            catch(Exception E)  //catch Genaral Error
            {
                System.err.println("ERROR");
            }
        }
    }//end of Main
    public static String GCS()  //GetConsoleString  User-Input
    {
        int noMoreInput = -1;
        char enterKeyHit =  
 ;
        int InputChar;
        StringBuffer InputBuffer = new StringBuffer(100);

        try
        {
            InputChar = System.in.read();
            while(InputChar != noMoreInput)
            {
                if((char)InputChar != enterKeyHit)
                {
                    InputBuffer.append((char) InputChar);
                }
                else
                {
                    InputBuffer.setLength(InputBuffer.length()-1);
                    break;
                }
                InputChar = System.in.read();
            }
        }//close Try(21)
        catch(IOException IOX)
        {
            System.err.println(IOX);
        }
        return InputBuffer.toString();
    }//end of GCS
}
最佳回答

修订这一法典:

            if(usa==000) 
            { 
                System.out.println("End Program"); 
                return; 
            }        

下面是:

            usa = Integer.parseInt(GCS());  

因为如果用户在返回后直接进入<代码>000。

我建议替换<代码>return。 注

            System.exit(0);

这表明你明确打算退出该方案,这是正常的终止。

问题回答

每当你看到 yourself写同样的法典体时,几乎一劳永逸,你也许会把这一条 block成一种方法。

根据你的法典:

if( amount == 1 ) 
{ 
    System.out.println("Exchange rate"); 
    System.out.println("80.75 Yen for 1 Dollar"); 
    System.out.println(" "); 
    System.out.println("Your Yen is "+ usa*80.75); 
    System.out.println("________________________________________"); 
} 
if( amount == 2 ) 
{ 
    //all the same as above except for 30.125 instead of 80.75
    //and NTD instead of Yen 
} 
//...

这种方法应采用改变的数值,然后重复你的发言,产生变数,而不是硬编码编号和货币类型。

请注意,您的方案目前不符合您的规格,因为您在查询之前将用户输入<>Integer,因此用户输入的的任何舱位。 Integer.parse 斜线/代码对<代码>0(而不仅仅是“000”)进行评价,将使你的方案得以返回。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签