0

编写一个允许用户输入多个值的程序,然后该程序应将输入的以英镑 (GBP) 为单位的值转换为多种不同的货币。菜单应如下所示:

  1. 输入值并输入 -1 停止
  2. 欧元
  3. 美元
  4. 日元
  5. 卢比
  6. 出口

选择2,3,4或5时,程序应显示在原始磅和转换的货币中输入的每个值,以及总数和转换总数。

public class Money {
    public static void main(String[] args) {
        System.out.println("Enter values and type -1 to stop"); 
        System.out.println("'1' = Euros"); 
        System.out.println("'2' = Dollars"); 
        System.out.println("'3' = Yen"); 
        System.out.println("'4' = Rupees"); 
        System.out.println("'5' = Exit"); 

        int count=0;
        String exit = "-1", euro="E";

        Scanner scan = new Scanner(System.in);

        System.out.println();
        System.out.print("Convert to: ");

        while (!scan.hasNext("[E, D, Y, R, X]+")) {
            System.out.print("Invalid value, enter Surname again: ");
            scan.next();
        }

        String exTo = scan.next();

        ArrayList<String> num = new ArrayList<String>();

        while (true) {
            count++;
            System.out.print("|| Enter values "+count+" in (P)Pounds : ");

            while (!scan.hasNextDouble()) {
                System.out.print("Invalid value, try again: ");
                scan.next();
            }
            num.add(scan.next());

            if(num.contains(exit)){
                String ar[]=num.toArray(new String[num.size()]);
                ar=num.toArray(ar);

                System.out.println("Result: "+ num);

                scan.close();
                break;
            }
        }
    }
}
4

2 回答 2

0

试试这个 :

import java.util.*;
class Money {
    public static void main(String[] args) {
        System.out.println("Enter values and type -1 to stop"); 
        System.out.println("'1' = Euros"); 
        System.out.println("'2' = Dollars"); 
        System.out.println("'3' = Yen"); 
        System.out.println("'4' = Rupees"); 
        System.out.println("'5' = Exit"); 
    int count=0;
    String exit = "-1", euro="E";

    Scanner scan = new Scanner(System.in);

    System.out.println();
    System.out.print("Convert to: ");

    while (!scan.hasNext("[E, D, Y, R, X]+")) {
        System.out.print("Invalid value, enter Surname again: ");
        scan.next();
    }

    String exTo = scan.next();

    ArrayList<String> num = new ArrayList<String>();

    while (true) {
        count++;
        System.out.print("|| Enter values "+count+" in (P)Pounds : ");

        while (!scan.hasNextDouble()) {
            System.out.print("Invalid value, try again: ");
            scan.next();
        }
        num.add(scan.next());

        if(num.contains(exit)){
            for(int i=0;i<num.size();i++){
            num.set(i,Integer.toString((Integer.parseInt(num.get(i))*2)));
            }
            System.out.println("Result: "+ num);

            scan.close();
            break;
        }
    }
}
}

删除最后一个输入 (-1)

在打印之前添加这一行num.remove("-1");

于 2013-02-03T12:49:29.293 回答
0

如何将数组列表中的每个数字相乘,例如用户类型在数组列表数组中的数字看起来像这样:[20,30,40] 并且在我需要结果显示之后,例如每个数字乘以 2 [40,60,80 ]

如何遍历列表并进行计算?

for (Integer i : yourList)
  i *= 2;

或者你可以尝试使用番石榴,Lists.transform(list, func)如果你想做更复杂的计算。

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Lists.html#transform(java.util.List , com.google.common.base.Function)

于 2013-02-03T12:50:12.407 回答