2017-02-16 47 views
0

所以我有一个任务,在控制台中从用户处取双打,然后将这些双打存储在数组中。这些双打实际上是我稍后会用到的坐标。用户输入存储在数组中的双数。数组大小未知,应该动态增长

该程序应该能够从用户采取未知数量的双打。我遇到的问题是允许数组大小动态增长。我们不能使用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] + " "); 
    } 

    } 
} 

我似乎无法弄清楚如何识别何时到达坐标1月底执行代码,以增加规模,并继续循环更多的双打。

稍后当用户完成后,控制台的空条目应退出循环并显示数组。

+0

相关:http://stackoverflow.com/questions/1647260/java-dynamic-array-sizes?rq=1 – 2017-02-16 16:41:35

+0

你可能想'如果(我==坐标1 .length){'(注意'i'不是'coord [i]'),'i'是指数(0,1,2,3,4,...),'coord [i]'是index-i的内容(在你的情况下如此坐标) – 2017-02-16 16:42:11

回答

0

你的问题是你使用for循环,它只循环你的数组的每个元素。所以,它永远不会让用户有机会输入比数组中更多的元素。使用while循环,以便循环,直到用户输入“exit”为止,而不是为每个元素循环一次。

这里是你的代码稍加修改,应该工作:

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个值,那么在数组的末尾将会有几个空值(零)。如果这是一个问题,你可以修改它。

+0

这个工作完美。非常感谢! – Battybm

+0

@Battybm高兴地帮忙!请花点时间来注册并接受答案,因为它解决了=] – 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