2013-02-03 83 views
0

編寫一個允許用戶輸入多個值的程序,然後該程序應該將以磅(GBP)輸入的值轉換爲多種不同的貨幣。菜單應該是這個樣子:將arraylist中的每個數字相乘

  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; 
      } 
     } 
    } 
} 
+2

什麼問題。 ? – Arpit

+0

如何乘以每個數字內數組列表等 用戶鍵入數字內部數組列表 陣列看起來像這樣:[20,30,40] 和後我需要像每個數字結果顯示乘以2例如 [40,60,80] –

+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");

+0

用戶輸入數字(以磅計) ArrayList num = new ArrayList (); 然後用戶例如選擇轉換爲歐元,並將arraylist中的EACH值乘以歐元匯率(例如0.94)並顯示給用戶arraylist,但以歐元爲單位 –

+0

有什麼方法可以做到我用字符串提問?以及不能使用整數,因爲必須收集像0.676567等數字... –

+0

示例輸入?在實際問題的代碼在to,我收集所有數字到數組唯一的位,我奮鬥的是將arraylist乘以2,當我這樣做時總是錯誤... –

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)

+0

用戶輸入數字(以磅爲單位) ArrayList num = new ArrayList (); 然後用戶例如選擇轉換爲歐元,並且arraylist內的EACH值乘以歐元匯率(例如0.94)並向用戶顯示列表,但是以歐元 –

相關問題