Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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_Loops - Fatal编程技术网

Java 基于整数输入请求一定数量的输入

Java 基于整数输入请求一定数量的输入,java,arrays,loops,Java,Arrays,Loops,我真的不知道如何正确地问这个问题,但我被分配了一个任务,要求我向一个用户请求输入(在本例中,这是一个羊的数目)。然后,在他们输入一个数字后,比如说5,我应该要求用户根据他们输入的整数指定一个x和y坐标值。我目前的理论是将原始数字存储在一个数组中,然后根据它们的值询问用户有多少个坐标(或2个数字的集合) 我真的不知道如何开始,但我将附上我的代码到目前为止 private int x, y; public static void main(String[] args) { Scanner

我真的不知道如何正确地问这个问题,但我被分配了一个任务,要求我向一个用户请求输入(在本例中,这是一个羊的数目)。然后,在他们输入一个数字后,比如说5,我应该要求用户根据他们输入的整数指定一个x和y坐标值。我目前的理论是将原始数字存储在一个数组中,然后根据它们的值询问用户有多少个坐标(或2个数字的集合)

我真的不知道如何开始,但我将附上我的代码到目前为止

private int x, y;

public static void main(String[] args) {
    Scanner sheepScanner = new Scanner(System.in); //Allows for user input
    System.out.println("Enter a number of Sheep"); //Asks user for input


    int numSheep = sheepScanner.nextInt(); //reads in number of sheep and stores it in variable numSheep
    int totalSheep[] = new int[numSheep]; //creates an array called totalSheep based on numSheep
    //System.out.println(totalSheep);
        for (int i = 1; i < totalSheep.length; i++) {
            totalSheep[i]=Integer.parseInt(); //literally no idea what's going here
private int x,y;
公共静态void main(字符串[]args){
Scanner sheepScanner=新扫描仪(System.in);//允许用户输入
System.out.println(“输入绵羊数量”);//要求用户输入
int numSheep=sheepScanner.nextInt();//读取绵羊数量并将其存储在变量numSheep中
int totalSheep[]=new int[numSheep];//基于numSheep创建名为totalSheep的数组
//System.out.println(totalSheep);
for(int i=1;i
根据我的理解,您需要做的是从用户那里获取输入并将其存储在向量/数组中。然后根据输入的值运行一个循环并将其添加到地图中

HashMap<Integer,String> map= new HashMap<>();
HashMap map=newhashmap();
然后运行for循环,直到数字为

for(int i=0;i<numberOfSheep;i++)
{
double x-Cordinate="";//store your values here
double y-Cordinate="";//store your values here
String co-ordinates= x-Cordinate +","+y-Cordinate;
map.put(i,co-ordinates);
}
for(int i=0;i在最后一行中使用此选项

totalSheep[i]=i;

因此,如果我正确理解了您要做的,基本上就是要求用户提供一些羊,然后要求每个羊提供(这只羊的)x和y坐标

因此,您要做的第一件事是询问绵羊的数量并将其存储到
numSheep
,这看起来很好

然后创建一个数组,这里真正需要的可能是一个二维数组,比如

int[][] coordinates = new int[sheepNum][2];
因此,它将是一个包含5个数组的数组,每个数组的长度为2。这些将是(x,y)坐标,与用户输入的数量相同。如果用户输入5,此数组将包含5组(x,y)坐标

然后从0开始循环到这个数组的长度,这也很好

for (int i = 1; i < coordinates.length; i++) { ... }
for(inti=1;i
请注意,
coordinates.length
只是
sheepNum
,因为数组包含
sheepNum
元素(x-y对)

在这个循环中有变量
i
,它从
0
运行到
sheepNum
,它是当前羊的数量

所以在这个循环中,你需要做的就是填充数组

因此,读取另外两个整数(绵羊编号
i
x
y

并将这些值存储到
坐标[i][0]
坐标[i][1]


完成:)

那么,从循环中的信息来看,这会是什么样子呢?因此,基本上,唯一的区别只是一个二维数组,而不是1。在这之后,我在循环中会做什么?totalSheep[I]=Integer.parseInt()'不起作用。坐标[I][0]=sheepScanner.nextInt();坐标[I][1]=sheepScanner.nextInt());所以用户输入了这只羊的x和y。没什么了。您只是将坐标初始化为一个整数吗?