2017-02-16 3 views
0

したがって、私はコンソールでユーザーから倍精度を取り入れて配列にそれらの倍精度を格納する割り当てを持っています。これらの倍数は実際に私が後で使用する座標になります。ユーザー入力は、配列に格納されるdouble型です。配列のサイズは不明であり、動的に拡張する必要があります

プログラムは、ユーザから未知の量のダブルを取ることができます。私が抱えている問題は、配列サイズを動的に拡大できることです。 arrayListやjavaライブラリのコレクションクラスは使用できません。

import java.util.Scanner; 
public class testMain{ 
    public static void main(String[] args){ 


    Scanner userInput = new Scanner(System.in); 
    boolean debug = true; 
    //Two array's where i'll store the coordinates 
    double[] coord1 = new double[3]; 
    double[] coord2 = new double[3]; 
    //Array for user commands 
    String[] commands = { "random", "exit", "help"}; 


    for(int i = 0; i < coord1.length; i++){ 
     System.out.println("Enter Coordinates: "); 
     double index = userInput.nextDouble(); 
     //If more doubles needed for array we want to resize array 
     if(coord1[i] >= coord1.length){ 
     for(int j = 0; j < 10; j++){ 
      coord1[j] = j + 10; 
     } 
     double newItems[] = new double[20]; 
     System.arraycopy(coord1, 0, newItems, 0 ,10); 
     coord1 = newItems; 
     } 


     coord1[i] = index; 

    } 
    if(debug == true){ 
     printArray(coord1); 
    } 
    } 

    public static void printArray(double arr[]){ 

    double n = arr.length; 

    for (int i = 0; i < n; i++) { 
     System.out.print(arr[i] + " "); 
    } 

    } 
} 

私はサイズを大きくし、より多くの倍のためのループを継続するためにコードを実行するためにcoord1の終わりに到達したときに認識する方法を見つけ出すように見えることはできません。ここに私はこれまで持っているものです。

その後、コンソールからの空のエントリがループを終了し、配列を表示する必要があります。

+0

関連:http://stackoverflow.com/questions/1647260/java-dynamic-array-sizes?rq=1あなたはおそらく 'たい –

+0

場合(I == coord1 (i、i)はインデックス(0,1,2,3,4、...)であり、 'coord [i]'はindex-iのコンテンツ(あなたの場合は座標) –

答えて

0

あなたの問題は、forループを使用していることです。ループは配列のすべての要素をループしているだけです。したがって、配列にある要素より多くの要素を入力する機会は決してありません。 whileループを使用すると、すべての要素に対して1回ループする代わりに、ユーザーが「終了」するまでループすることができます。ここで

作業をする必要があり、あなたのコードの若干の変更である:

Scanner userInput = new Scanner(System.in); 

//Two array's where i'll store the coordinates 
double[] coord1 = new double[3]; 
double[] coord2 = new double[3]; 

//Array for user commands 
String[] commands = { "random", "exit", "help"}; 

System.out.println("Enter Coordinates: "); 
String input = userInput.nextLine(); 
int arrayIndex = 0; 
while (!input.equals("exit")) { 

    //convert input to a double 
    System.out.println("Enter Coordinates: "); 
    double userDouble = Double.parseDouble(input); 

    //handle case where array needs to be resized 
    if (arrayIndex >= coord1.length) { 
     double[] newCoord1 = new double[coord1.length * 2]; 
     for (int copyIndex = 0; copyIndex < coord1.length; copyIndex++) { 
      newCoord1[copyIndex] = coord1[copyIndex]; 
     } 
     coord1 = newCoord1; 
    } 

    //store the value 
    coord1[arrayIndex] = userDouble; 
    arrayIndex = arrayIndex + 1; 

    //take new input 
    input = userInput.nextLine(); 
} 

は、サイズ変更が必要なときに、これは、配列のサイズを倍になることに注意してください。つまり、配列のサイズが6倍になり、ユーザーが5つの値しか入力しないと、配列の最後に2つの空の値(ゼロ)が表示されます。それが問題の場合は、それを変更することができます。

+0

これは完全に機能しました。本当にありがとう! – Battybm

+0

@Battybmは喜んでお手伝いします! upvoteに時間をかけて、それがうまくいってから答えを受け入れてください] – nhouser9

0

ユーザーがコマンド "exit"を入力するまでループを繰り返すことができます。ここでは、各項目が配列に追加される前に配列に要素を動的に追加する例を示します。私はカンマ区切りの値を使用し

String[] number; 
    String userInput = ""; 
    String[] commands = { "random", "exit", "help"}; 
    double[] coord1 = new double[0]; 
    double[] coord2 = new double[0]; 
    Scanner scan = new Scanner(System.in); 
    do { 
     System.out.println("Enter Coordinates (ex 1,3)\n"); 
     userInput = scan.nextLine(); 

     if(!userInput.equals(commands[1])) 
     { 
      number = userInput.split(","); 
      if(number.length != 2 || !number[0].matches("\\d+") || !number[1].matches("\\d+")) 
      { 
       System.out.println("Error: Invalid Input! Try again"); 
      } 
      else 
      { 
       //our index to place our numbers will be the current length of our array 
       int index = coord1.length; 
       double[] temp = new double[coord1.length + 1]; 
       //copy the content to the same array but just 1 size bigger 
       System.arraycopy(coord1, 0, temp, 0, coord1.length); 
       coord1 = temp; 
       temp = new double[coord2.length + 1]; 
       System.arraycopy(coord2, 0, temp, 0, coord2.length); 
       coord2 = temp; 
       //now use our index to place the numbers in the correct locations 
       coord1[index] = Double.parseDouble(number[0]); 
       coord2[index] = Double.parseDouble(number[1]); 
      } 
     } 

    } while (!userInput.equals(commands[1])); 

    for(int i = 0; i < coord1.length; i++) 
    { 
     System.out.println(i + " - X: " + coord1[i] + " Y: " + coord2[i]); 
    } 

注意は、彼らが同時に座標を入力することができ、利用者の生活と私の人生を容易にします。

出力例

Enter Coordinates (ex 1,3) 
1,5 
Enter Coordinates (ex 1,3) 
4,7 
Enter Coordinates (ex 1,3) 
8,9 
Enter Coordinates (ex 1,3) 
gffd 
Error: Invalid Input! Try again 
Enter Coordinates (ex 1,3) 
3 6 
Error: Invalid Input! Try again 
Enter Coordinates (ex 1,3) 
0,0 
Enter Coordinates (ex 1,3) 
exit 

0 - X: 1.0 Y: 5.0 
1 - X: 4.0 Y: 7.0 
2 - X: 8.0 Y: 9.0 
3 - X: 0.0 Y: 0.0 
関連する問題