2013-10-10 36 views
0

任何人都可以幫助我嗎?我們的老師說他不想在我們的程序中使用數組列表。只有數組,for循環,並且只做。該程序是一個購買程序(添加項目,記錄交易)。這是我想了解的第一步。購買Java part 2

在這個程序中,我想要正確顯示項目代碼和descip。例如code = 123和descrip =是的。輸出顯示,Item code = 123, Item descrpition = yeah.但是,一旦我說是,我把另一個,例如代碼= 456和desription =哦。輸出Item code = 123456, Item description = yeah.oh.

import java.util.Scanner; 

public class Apps { 

    public static void main(String[] args) { 

     Scanner a = new Scanner(System.in); 
     String code = "", des = "", ans; 
     String[] b = new String[1]; 
     String[] aw = new String[1]; 
     int x; 

     do { 

      System.out.print("Item code:"); 
      code += "" + a.next(); 

      System.out.print("des:"); 
      des += "" + a.next(); 

      System.out.println("yes or no:"); 
      ans = a.next(); 

     } 

     while (ans.equals("yes")); 

     for (x = 0; x < 1; x++) { 
      b[x] = code; 
      aw[x] = des; 

      System.out.println("Item code:" + b[x]); 
      System.out.println("Item description:" + aw[x]); 

     } 

    } 

} 
+0

此代碼可能導致無限循環。 – 2013-10-10 13:04:10

+0

請格式化您的代碼。 –

+0

@nikpon這就是我的教練想要的。無限無固定價值。 – Raymond

回答

0

next()處理線的末端,所以當你再次調用next(),它會作爲輸入的輸入(\n)你之前輸入。

你應該在每個next()之後致電nextLine()(因此它會吞下前一個的\n)。

+0

那個先生可以輸出什麼? – Raymond

+0

@downvoter請關注評論。 – Maroun

+0

@雷蒙德我不明白你的問題.. – Maroun

0

這適用於例如:

public static void main(String[] args) { 

    Scanner a = new Scanner(System.in); 
    String ans; 
    int capacity = 100; 
    String[] b = new String[capacity]; 
    String[] aw = new String[capacity]; 
    int itemCount = 0; 
    int x; 

    do {   
     System.out.print("Item code:"); 
     b[itemCount] = a.next(); 

     System.out.print("des:"); 
     aw[itemCount] = a.next(); 

     System.out.println("yes or no:"); 
     ans = a.next(); 
     itemCount++; 
    } while (ans.equals("yes")); 

    for (x = 0; x < itemCount; x++) { 
     System.out.println("Item code:" + b[x]); 
     System.out.println("Item description:" + aw[x]); 
    } 
} 

如果你想確保,即用戶可以添加項目的「無限」的數字,你必須manualy檢查ITEMCOUNT比陣列的容量,當較小它達到了,你必須分配更高容量的新陣列並將值複製到它。

+0

它可以。但他表示數組內的元素不能被修復。 – Raymond

+0

此代碼是合理的。這次真是萬分感謝。 – Raymond

0

這是可能實現這一目標使用集合,如:

import java.util.ArrayList; 
import java.util.Scanner; 

public class Apps 
{ 

    public static void main(String[] args) 
    { 

    Scanner a = new Scanner(System.in); 
    String ans; 

    ArrayList<String> br = new ArrayList<String>(); 
    ArrayList<String> ar = new ArrayList<String>(); 

    do 
    { 
     System.out.print("Item code:"); 
     br.add(a.next()); 

     System.out.print("des:"); 
     ar.add(a.next()); 

     System.out.println("yes or no:"); 
     ans = a.next(); 

    } 

    while (ans.equals("yes")); 

    for (int i = 0; i < br.size(); i++) 
    { 
     System.out.println("Item code:" + br.get(i)); 
     System.out.println("Item description:" + ar.get(i)); 
    } 

    } 

} 

但不能肯定是否可以用陣列來實現,或者

public static void main(String[] args) 
{ 

Scanner a = new Scanner(System.in); 
    String code = "", des = "", ans; 
    int capacity = 100; 
    String[] b = new String[Integer.MAX_VALUE]; 
    String[] aw = new String[Integer.MAX_VALUE]; 
    int itemCount = 0; 
    int x; 

    do {   
     System.out.print("Item code:"); 
     b[itemCount] = a.next(); 

     System.out.print("des:"); 
     aw[itemCount] = a.next(); 

     System.out.println("yes or no:"); 
     ans = a.next(); 
     itemCount++; 
    } while (ans.equals("yes")); 

    for (x = 0; x < itemCount; x++) { 
     System.out.println("Item code:" + b[x]); 
     System.out.println("Item description:" + aw[x]); 
    } 

} 

Interger.MAX_VALUE是可以運行的次數這個但不是無限的,因爲數組大小必須在編譯時定義。

+0

問題在於我們的教師不想放置數組列表。 – Raymond

+0

我試圖使用數組列表。這是可能的。然而,我們的入侵者希望它只能在數組中。 – Raymond

+0

然後你可以運行直到Integer.MAX_VALUE :( – Batty

2

您可以將此代碼用於任意數量的項目。它將數據存儲在字符串中,並在用戶選擇否後分割字符串。

public static void main(String[] args) { 
    Scanner a = new Scanner(System.in); 
    String ans; 
    String itemCounts = ""; 
    String descriptions = ""; 

    do { 
     System.out.print("Item code:"); 
     itemCounts += "" + a.next() + "\n"; 

     System.out.print("des:"); 
     descriptions += "" + a.next() + "\n"; 

     System.out.println("yes or no:"); 
     ans = a.next(); 
    } while (ans.equals("yes")); 

    String[] b = itemCounts.split("\n"); 
    String[] aw = descriptions.split("\n"); 

    for (int i = 0; i < b.length; i++) { 
     System.out.println("Item code:" + b[i]); 
     System.out.println("Item description:" + aw[i]); 
    } 
} 
+0

好吧,讓我試試這個吧。 – Raymond

+0

它的工作原理!這是我想要達到的目標! – Raymond

+3

如果它解決了你的問題,你可以接受它作爲答案。 –