2017-10-08 6 views
-1

私は非常に奇妙な配列を印刷しています。コードと出力は以下の通りです。
アレイ印刷が非常に妙に

public class AdvDotComLauncher { 
    public static void main(String[] args) { 
     AdvDotComTable table = new AdvDotComTable(); 
     table.createTable(5,5, 5); 
    } 
} 

下記の問題のある配列のコード。

import java.util.ArrayList; 


public class AdvDotComTable { 
    public void createTable(int size, int dotComAmount, int dotComSize) { 
     //Holds the DotComs that will be randomly put onto the map 
     String[] dotComs = {"Pets.com", "Amazon.com", "Target.com", "Apple.com", "Microsoft.com", "Steampowered.com"}; 
     //Holds the rows and columns 
     ArrayList<Character> row = new ArrayList<Character>(); 
     ArrayList<Integer> column = new ArrayList<Integer>(); 
     column.add(0); 
     char[] theAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); 
     //Makes sure the Dot Coms will fit 
     if (dotComAmount >= dotComs.length || dotComAmount/dotComSize > size) { 
      System.out.println("There are too many dot coms! Please enter different amounts."); 
     } 
     //Used in the while loop to make sure that the correct amount of columns are created 
     int done = size * size; 
     //Used to get an index value in theAlphabet 
     int theAlphabetIndex = 0; 
     //Used to add columns 
     int number = 0; 
     //Used to tell when to create a column 
     int size2 = size; 
     //Starts creating rows and columns 
     while (done > 0) { 
      row.add(theAlphabet[theAlphabetIndex]); 
      if (size2 == 0) { 
       size2 = size; 
       column.add(number); 
       number++; 
       theAlphabetIndex++; 
      } 
      System.out.println(row.get(number) + "" + column.get(number)); 
      size2--; 
      done--; 
     } 
     System.out.println("The table has been created with " + dotComAmount + " Dot Coms, a table size of " + size + ", and a Dot Com size of " + dotComSize + "."); 
    } 
} 

出力:

A0 
A0 
A0 
A0 
A0 
A0 
A0 
A0 
A0 
A0 
A1 
A1 
A1 
A1 
A1 
A2 
A2 
A2 
A2 
A2 
A3 
A3 
A3 
A3 
A3 
The table has been created with 5 Dot Coms, a table size of 5, and a Dot Com size of 5. 

期待される出力はこれです:

A0 
B0 
C0 
D0 
E0 
A1 
B1 
C1 
D1 
E1 
A2 
B2 
C2 
D2 
E2 
A3 
B3 
C3 
D3 
E3 
A1 
B4 
C4 
D4 
E4 

私はこの問題を引き起こしているもの見当がつかないし、どのような援助がいただければ幸いです。あなたのプログラムで

+0

[デバッガとはどのようにして問題を診断するのに役立ちますか?](https://stackoverflow.com/q/25385173/5221149)---私たちがあなたに与える最も良い支援は、あなたのコードをデバッグする方法を学ぶので、あなたのためにデバッグするのを待つ必要はなく、自分で問題を見つけることができます。あなたはあまりにも多くのことを学びます。 – Andreas

+0

'column.add(number);' と 'number ++;'の行を入れ替えて数字を修正できます。私はあなたが手紙を使って何をしようとしているのかについて完全に混乱しています。 –

答えて

0

ルックは再びtheAlphabetIndexは、あなたが0に設定し、増加されることはありませんが、それはあなたが0のインデックスであるだけA(..)を取得している5. theAlphabetIndex=0たようですsize2の値として増加していません。

+0

あなたは間違っています。私はSystem.out.println( "テスト")を置く。 ifステートメント内のステートメントが印刷されました。また、size2はループが通過するたびに減少します... – Lyfe

+0

なぜa0(10回)、a1(5回)などが得られますか?あなたのwhileループだけが適切に25回実行されます。 –