Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 存储在数组中的用户输入加倍。数组大小未知,应动态增长_Java_Arrays - Fatal编程技术网

Java 存储在数组中的用户输入加倍。数组大小未知,应动态增长

Java 存储在数组中的用户输入加倍。数组大小未知,应动态增长,java,arrays,Java,Arrays,因此,我有一个任务,在这个任务中,我从控制台中的用户那里获取双倍,然后将这些双倍存储在一个数组中。这些双精度实际上是坐标,我将在后面使用 程序应该能够从用户那里获取未知数量的双倍。我遇到的问题是允许数组大小动态增长。我们不能使用arrayList或任何java库集合类。以下是我到目前为止的情况: import java.util.Scanner; public class testMain{ public static void main(String[] args){ Scan

因此,我有一个任务,在这个任务中,我从控制台中的用户那里获取双倍,然后将这些双倍存储在一个数组中。这些双精度实际上是坐标,我将在后面使用

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

  }
}
import java.util.Scanner;
公共类testMain{
公共静态void main(字符串[]args){
扫描仪用户输入=新扫描仪(System.in);
布尔调试=真;
//两个数组是我存储坐标的地方
double[]coord1=新的double[3];
double[]coord2=新的double[3];
//用户命令数组
字符串[]命令={“随机”、“退出”、“帮助”};
for(int i=0;i=coord1.length){
对于(int j=0;j<10;j++){
coord1[j]=j+10;
}
双新项目[]=新双[20];
数组副本(coord1,0,newItems,0,10);
coord1=新项目;
}
coord1[i]=指数;
}
if(debug==true){
打印阵列(coord1);
}
}
公共静态void打印数组(双arr[]{
双n=阵列长度;
对于(int i=0;i
我似乎不知道如何识别何时到达coord1的末尾来执行代码以增加大小并继续循环以获得更多的双倍


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

您的问题是使用
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();
}
Scanner userInput=新扫描仪(System.in);
//两个数组是我存储坐标的地方
double[]coord1=新的double[3];
double[]coord2=新的double[3];
//用户命令数组
字符串[]命令={“随机”、“退出”、“帮助”};
System.out.println(“输入坐标:”);
字符串输入=userInput.nextLine();
int-arrayIndex=0;
而(!input.equals(“exit”)){
//将输入转换为双精度
System.out.println(“输入坐标:”);
double userDouble=double.parseDouble(输入);
//处理需要调整数组大小的情况
if(arrayIndex>=coord1.length){
double[]newCoord1=新的double[coord1.length*2];
对于(int-copyIndex=0;copyIndex

请记住,当需要调整大小时,这将使数组大小加倍。这意味着,如果数组大小加倍为6,并且用户只输入5个值,那么在数组末尾将有两个空值(零)。如果这是一个问题,您可以修改它。

您可以使用一段时间来循环,直到用户键入命令“退出”。下面是一个在将每个项添加到数组之前向数组动态添加元素的示例

    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]);
    }

相关:你可能想要
if(i==coord1.length){
(注意
i
不是
coord[i]
),
i
是指标(0,1,2,3,4,…),而
coord[i]
是索引-i的内容(所以在你的情况下是坐标)@Battybm很乐意帮忙!请花时间投票并接受答案,因为它已经解决了=]
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