返回类型ARRAY并打印数组的值 [英] return type ARRAY and print the value of the array

查看:82
本文介绍了返回类型ARRAY并打印数组的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习JAVA。



我在方法中创建了一个数组,现在我必须返回(作为返回类型ARRAY 数组到main方法和另一个用户定义的方法从main方法和用户定义的方法打印该数组的值,以符合要求。



直到现在我已经完成了从另一种方法打印数组的值



请查看我的代码。



我该怎么做?



I am learning JAVA.

I have created an array inside a method and now I have to return(as return type ARRAY) that array to main method and another user-defined method and print the values of that array from the main method and the user-defined method accroding to requirement.

Till now I have completed to print the values of the array from another method.

Please have a look at my code.

How can I do that?

import java.io.*;
import java.util.*;
public class java1
{
	public void GetPoints()
	{
		Scanner input = new Scanner(System.in);

        //allow user  input;
        System.out.println("Please enter n points");
		int num = input.nextInt();
		
		int array[] = new int[num];
		
		for (int i = 0 ; i < array.length; i++ )
		{
           array[i] = input.nextInt();
        }		
		//return  array;
        printArray(array);
	}
	
	public void printArray(int arr[])
	{
        
		int n = arr.length;
		System.out.println("These are the numbers you have entered.");

        for (int i = 0; i < n;i=i+2) {
            System.out.print(arr[i] + " ");
			System.out.print("(X:"+arr[i] + " "+",Y:"+arr[i+1] + " "+")");			
        }
    }
	public static void main(String args[])
	{
		java1 test = new java1();
		test.GetPoints();
	}
}

推荐答案

所以只需保存GetPoints methoid返回的值,并调用printArray来自你的主要方法:

So just save the value that the GetPoints methoid returns, aand call printArray from your main method:
int array[] = test.GetPoints();
printArray(array);

你所要做的就是将GetPoints方法的返回类型设置为一个int数组 - 此时它是无效的(即它什么都不返回)并返回你的评论中的数组。

All you have to do is set the return type for the GetPoints method to an array of ints - at the moment it is void (i.e. it returns nothing) and return the array as in your comments.


这篇关于返回类型ARRAY并打印数组的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆