2014-09-25 48 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