将arraylist中的每个数字相乘

时间:2013-02-03 12:36:17

标签: java arraylist multiplying

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

  1. 输入值并输入-1以停止
  2. 欧元
  3. 卢比
  4. 退出
  5. 当选择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;
                }
            }
        }
    }
    

2 个答案:

答案 0 :(得分: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");

之前添加此行

答案 1 :(得分: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)