2014-09-25 61 views
0

這種類型的輸出是否有任何其他編碼或代碼格式?我想再次請求你的幫助。先謝謝你!這是關於數組的。我試過這個使用矢量,但我真的不明白。請真的需要幫助。這種類型的輸出的任何其他代碼

import java.io.*; 
import java.util.ArrayList; 
public class DataArrays 
{ 
    public static DataInputStream a = new DataInputStream(System.in); 
    public static void main(String args[])throws Exception 
    { 
     ArrayList<Integer> prodNum = new ArrayList<Integer>(100); 
     ArrayList<String> prodName = new ArrayList<String>(100); 
     ArrayList<Integer> prodPrice = new ArrayList<Integer>(100); 
     ArrayList<Integer> prodPay = new ArrayList<Integer>(100); 

     for(int x=1;x<=100;x++) 
     {  
      System.out.println("1 - Maintenance"); 
      System.out.println("2 - Transaction"); 
     System.out.println(""); 

     System.out.print("Enter code: "); 
     int code = Integer.parseInt(a.readLine()); 
     System.out.println(""); 

     int y = 1; 
     if(code==1) 
     { 
      System.out.print("") ; 
      System.out.println("Product number: "+ x); 
      prodNum.add(x);    //this brings 1st input to array element #0 which is not needed 
      prodNum.add(x); 
      System.out.print("Product name: "); 
      String prodname = a.readLine(); 
      prodName.add(prodname);  //this brings 1st input to array element #0 which is not needed 
      prodName.add(prodname); 
      System.out.print("Price: "); 
      int prodprice = Integer.parseInt(a.readLine()); 
      prodPrice.add(prodprice); //this brings 1st input to array element #0 which is not needed 
      prodPrice.add(prodprice); 
      System.out.print("Payment: "); 
      int prodpay = Integer.parseInt(a.readLine()); 
      prodPay.add(prodpay);  //this brings 1st input to array element #0 which is not needed 
      prodPay.add(prodpay); 
      System.out.println(""); 
      System.out.println("Start New Transaction:"); 
      System.out.println(""); 
     } 
     else if(code==2) 
     { 
      System.out.println("Display List of Products-Prices"); 
      System.out.print("Enter product number: "); 
      int i = Integer.parseInt(a.readLine()); 
      i = prodNum.get(i);   //this gets the data stored in the arraylist. Assuming it starts with 1 and above 
      prodNum.set(1, i); 
      System.out.println(""); 

      System.out.println("Product name: "+ prodName.get(i)); 
      System.out.println("Product price: "+ prodPrice.get(i)); 
      System.out.println("Product payment: "+ prodPay.get(i)); 
      System.out.println(""); 
      System.out.println("Start New Transaction:"); 
     } 
     else if(code>=3) 
     { 
      System.out.println("Invalid");   
      System.out.println("Restart"); 
      System.out.println(""); 
     } 
     else if(code==0) 
     { 
      System.out.println("Program will end"); 
      break; 
    }}}} 
+0

您能否詳細說明您的問題是什麼? – Robert 2014-09-25 12:48:36

+0

這是什麼#Axiom_cc。請描述該問題 – Ajit 2014-09-25 12:50:08

+0

我的代碼在這裏沒有任何問題。我只想做另一種類型的編碼,它會有這樣的輸出。 – 2014-09-26 03:30:54

回答

1

看起來像是要添加產品或顯示它們。

不是維護4個不同的ArrayList,而是使用類Product並維護1個列表。

class Product { 
    Integer prodNum; 
    String prodName; 
    Integer prodPay; 
    Integer prodPrice; 
} 


List<Product> listOfProducts = new ArrayList<Product>(); 

此外,'if'代碼的4個塊可以用switch語句替換。

+0

好吧我試着使用開關併爲產品添加一個類。謝謝 – 2014-09-26 03:31:52

相關問題