简体   繁体   English

如何在不打印的情况下使用void方法?

[英]How do I use a void method without printing?

I am having problems understanding a program I need to write for class. 我在理解我需要为课程编写的程序时遇到问题。 My program works as it should, but the problem states that it wants me to not print within my methods. 我的程序可以正常工作,但问题是它要求我不要在我的方法中打印。 I am confused on how I should output my values because my methods must be void according to the problem (which doesn't return anything) and I can't print inside of them. 我对如何输出我的值感到困惑,因为我的方法必须根据问题(它不会返回任何内容)而无效,并且我无法打印它们。

Here is the question: 这是一个问题:

Design and implement a java program (name it ArrayMethods), that defines 4 methods as follows: 设计并实现一个java程序(将其命名为ArrayMethods),它定义了4个方法,如下所示:

int arrayMax (int[] array) int arrayMax(int [] array)

int arrayMin (int[] array) int arrayMin(int [] array)

void arraySquared (int[] array) void arraySquared(int [] array)

void arrayReverse (int[] array) void arrayReverse(int [] array)

Test your methods by creating an array of length 5 within your main method and filling it with random numbers between 1 and 1000. Your program should then display the original array, display the smallest number in the array, display the greatest number in the array, display the revered array, and display the square of each value in the array. 通过在main方法中创建一个长度为5的数组并用1到1000之间的随机数填充它来测试你的方法。然后你的程序应该显示原始数组,显示数组中最小的数字,显示数组中最大的数字,显示受尊重的数组,并显示数组中每个值的平方。 You main method shoudl invoke each method exactly once, with each invocation use the original array as the actual parameter. 你的主要方法shoudl只调用一次每个方法,每次调用都使用原始数组作为实际参数。 No printing within the methods. 方法内没有打印。 Document your code, and organize/space your outputs properly. 记录您的代码,并正确组织/分隔您的输出。 Use escape characters and formatting objects when applicable. 适用时,使用转义字符和格式化对象。

So again my question is: How do I use those methods without printing anything if I can't return a value? 所以我的问题再一次是:如果我不能返回值,如何在不打印任何内容的情况下使用这些方法? If anyone could give me a clue on how to solve this, it would be greatly appreciated. 如果有人能给我一个如何解决这个问题的线索,我将不胜感激。

import java.util.Random;
import java.util.Arrays;
public class ArrayMethods
{
   public static void main (String[] args)
   {
   int[] array = new int[5];
   array[0] = (int)(Math.random() * (1000 - 1)) + 1;
   array[1] = (int)(Math.random() * (1000 - 1)) + 1;
   array[2] = (int)(Math.random() * (1000 - 1)) + 1;
   array[3] = (int)(Math.random() * (1000 - 1)) + 1;
   array[4] = (int)(Math.random() * (1000 - 1)) + 1;
     System.out.println("The values within the array are: " + Arrays.toString(array));
     System.out.println("The maximum value within the array is: " + arrayMax(array));
     System.out.println("The minimum value of the array is: " + arrayMin(array));
     System.out.print("The values within the array (squared) are: ");
     arraySquared(array);
     System.out.print("\n");
     System.out.print("The array reversed is: ");
     arrayReverse(array);
   }
   public static int arrayMax (int[] array)
   {
     int max = 0;
     for (int i = 0; i < array.length; i++)
        {
           if (array[i] > max)
              {
              max = array[i];
              }
        }
     return max;
   }

   public static int arrayMin (int[] array)
   {
     int min = 1000;
       for (int i = 0; i < array.length; i++)
              {
              if (array[i] < min)
                  {
                 min = array[i];
                  }
              }
     return min;
    }

   public static void arraySquared (int[] array)
   {
       int[] array2 = new int[5];
     for (int i = 0; i < array.length; i++)
        {
           array2[i] = array[i] * array[i];
           System.out.print(array2[i]);
              while ( i < array.length - 1)
                 {
                 System.out.print(", ");
                 break;
                 }
        }
   }

  public static void arrayReverse (int[] array)
   {
     for(int i = array.length - 1; i >= 0; i--)
        {
           System.out.print(array[i] + " ");
        }

   }
}

For the void functions, in C# you'd simply use an "out" or "ref" keyword. 对于void函数,在C#中你只需使用“out”或“ref”关键字。 If they insist on Java, Java doesn't have an equivalent, but you can do something like that with an object. 如果他们坚持使用Java,Java就没有等价物,但你可以用对象做类似的事情。

public class PassArray
    {
        public int[] array;

        public PassArray(int[] array)
        {
            this.array = array;
        }
    }

    public static void Reverse(PassArray arrayHolder)
    {
        int[] reversed = new int[arrayHolder.array.Length];

        int j = 0;

        for (int i = arrayHolder.array.Length - 1; i >= 0; i--)
        {
            reversed[j] = arrayHolder.array[i];
            j++;
        }

        arrayHolder.array = reversed;
    }

    static void Main(string[] args)
    {
        int[] toReverse = new[] { 10, 30, 2, 1, 3, 100, 340 };

        PassArray passedObject = new PassArray(toReverse);

        Reverse(passedObject);
        // passedObject.array will now have the reversed array
    }

Obviously this could can be improved but it should at least give the right idea. 显然这可以改进,但至少应该给出正确的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM