java中的数组使用不同的方法

时间:2023-02-09 16:17:56

I have 3 different questions/requirements. I have attempted them all, but I feel very lost and not sure what's wrong with my code. I will first include the requirement and then put my code. Any help is appreciated!

我有3个不同的问题/要求。我已经尝试了所有这些,但我感到非常迷茫并且不确定我的代码有什么问题。我将首先包括要求然后把我的代码。任何帮助表示赞赏!

1.Write a class/static method that takes an array of doubles as a parameter and returns the sum of all of the elements in the array.

1.编写一个类/静态方法,该方法将一个双精度数组作为参数,并返回数组中所有元素的总和。

public static void mystery(int[] a);
 for (int i = 0; i < a.length - 1; i++);
    if (a[i] < a[i + 1]);
        a[i] = a[i + 1];
}

2.Write a class/static method that takes an array of int as a parameter and returns an array of type boolean of the same length as the parameter. For each element in the parameter array for which the value is odd the returned array should have a true value for the corresponding element in its array. Likewise, even valued elements in the parameter array should be matched by corresponding false values in the returned array.

2.编写一个类/静态方法,该方法将int数组作为参数,并返回与参数长度相同的boolean类型的数组。对于值为奇数的参数数组中的每个元素,返回的数组应该具有其数组中相应元素的true值。同样,参数数组中的有价值元素也应该与返回数组中的相应false值匹配。

public class Practice
{
    public static void main(String[] args)
    {
        int[] myArray = {1, 2, 3, 4};
        array(myArray);
        for(int i = 0; i < myArray.length; i++)
            System.out.println(myArray[i]);
    } 
    public static void array(int[] a) {

        int n = a.length;
        int zero = a[0];
        a[0] = a[n-1];
        a[n-1] = zero;
    }
}

3.Write a class/static method that takes an array of Strings as a parameter and returns an array of Strings of the same length as the original array. The string in each element of the returned array will be reversed from the original string in the original array. So, if s[1]="the", then r[1]="eht".

3.编写一个类/静态方法,它将一个字符串数组作为参数,并返回一个与原始数组长度相同的字符串数组。返回数组的每个元素中的字符串将与原始数组中的原始字符串相反。所以,如果s [1] =“the”,那么r [1] =“eht”。

public class Homework6  {
public static void Main()  {
    Array myArray=Array.CreateInstance( typeof(String), 9 );
    myArray.SetValue( "The", 0 );
    myArray.SetValue( "green", 1 );
    myArray.SetValue( "bag", 2 );
    Console.WriteLine( "The Array initially contains the following values:" );
    PrintIndexAndValues( myArray );
    Array.Reverse( myArray );
    Console.WriteLine( "After reversing:" );
    PrintIndexAndValues( myArray );
}
public static void PrintIndexAndValues( Array myArray )  {
    for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
        System.out.println( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );
}
}

1 个解决方案

#1


3  

Ok, this is clearly homework so I'm only going to provide hints - the entire point is for you to figure this out. Here's some help :-)

好吧,这显然是功课,所以我只是提供提示 - 整个要点是让你想出来的。这里有一些帮助:-)

  1. Write a class/static method that takes an array of doubles as a parameter and returns the sum of all of the elements in the array.
  2. 编写一个类/静态方法,它将一个双精度数组作为参数,并返回数组中所有元素的总和。

For this, you show a method signature public static void mystery(int[] a). Here are a few hints:

为此,您显示方法签名public static void mystery(int [] a)。以下是一些提示:

  1. does this take an array of doubles?
  2. 这需要一系列双打吗?

  3. if you calculate a sum of a bunch of doubles, what data type would the result be? e.g. mydouble1 + mydouble2 + mydouble3 == ???? Your method signature claims the return type is void, but that's not the data type I would expect from this addition of doubles, would you?
  4. 如果计算一堆双精度的总和,结果会是什么数据类型?例如mydouble1 + mydouble2 + mydouble3 == ????你的方法签名声称返回类型是无效的,但这不是我期望从这个双倍添加的数据类型,你呢?

  5. The code you showed is trying to store the sum inside an array. Seems like it would make more sense to create a new variable inside the method called sum, store the result inside this new variable, and then return that variable
  6. 您展示的代码是尝试将总和存储在数组中。看起来在称为sum的方法中创建一个新变量更有意义,将结果存储在这个新变量中,然后返回该变量

  7. Try very hard to avoid putting a semicolon after an if statement at this point in your coding experience. Specifically, the semicolon here can cause you a ton of problems, so remove it: if (a[i] < a[i + 1]);
  8. 在你的编码经验中,尽量避免在if语句之后添加分号。具体来说,这里的分号会引起很多问题,所以删除它:if(a [i]

2.Write a class/static method that takes an array of int as a parameter and returns an array of type boolean of the same length as the parameter.

2.编写一个类/静态方法,该方法将int数组作为参数,并返回与参数长度相同的boolean类型的数组。

You've defined a method public static void array(int[] a) to accomplish this. Some hints:

您已经定义了一个方法public static void array(int [] a)来完成此任务。一些提示:

  1. the requirement is to return an array of boolean. You're returning a void. This is the same mistake you make in the first problem - perhaps review how method signatures look in java, specifically how you define the type of the return value
  2. 要求是返回一个布尔数组。你回来了一个虚空。这是你在第一个问题中犯的同样错误 - 也许回顾一下java中方法签名的外观,特别是你如何定义返回值的类型

  3. your code here is not remotely doing what the requirement asks, looks like this is leftover code from somewhere else. Start by figuring out what return type you need for an array of booleans, and then create an empty boolean array, then try to figure out how to fill in that array of boolean from the array of integers you get passes
  4. 你的代码不是远程执行要求所要求的,看起来这是来自其他地方的剩余代码。首先找出布尔数组所需的返回类型,然后创建一个空的布尔数组,然后尝试弄清楚如何从你得到的整数数组中填充布尔数组

3.Write a class/static method that takes an array of Strings as a parameter and returns an array of Strings of the same length as the original array.

3.编写一个类/静态方法,它将一个字符串数组作为参数,并返回一个与原始数组长度相同的字符串数组。

  1. Ok, you definitely don't yet know how to define an array of strings in java. Google "java string array" and read the first result. Try to first write a method signature (e.g. just the public return_stuff my_method_name(input_stuff my_argument)) first that properly replaces return_stuff and input_stuff what a java string array
  2. 好的,你肯定还不知道如何在java中定义一个字符串数组。谷歌“java字符串数组”并读取第一个结果。首先尝试先写一个方法签名(例如只是公共return_stuff my_method_name(input_stuff my_argument)),然后正确地替换return_stuff和input_stuff一个java字符串数组

EDIT: making this a community wiki so others can update as OP changes their code problem. Feel free to contribute, but try to remember OP is clearly working out the basics of this problem, he/she probably doesn't need to understand coding conventions until at least lesson 2 :-)

编辑:使这个社区维基,以便其他人可以更新,因为OP改变了他们的代码问题。随意贡献,但尝试记住OP显然正在解决这个问题的基础知识,他/她可能不需要了解编码约定,直到至少第2课:-)

#1


3  

Ok, this is clearly homework so I'm only going to provide hints - the entire point is for you to figure this out. Here's some help :-)

好吧,这显然是功课,所以我只是提供提示 - 整个要点是让你想出来的。这里有一些帮助:-)

  1. Write a class/static method that takes an array of doubles as a parameter and returns the sum of all of the elements in the array.
  2. 编写一个类/静态方法,它将一个双精度数组作为参数,并返回数组中所有元素的总和。

For this, you show a method signature public static void mystery(int[] a). Here are a few hints:

为此,您显示方法签名public static void mystery(int [] a)。以下是一些提示:

  1. does this take an array of doubles?
  2. 这需要一系列双打吗?

  3. if you calculate a sum of a bunch of doubles, what data type would the result be? e.g. mydouble1 + mydouble2 + mydouble3 == ???? Your method signature claims the return type is void, but that's not the data type I would expect from this addition of doubles, would you?
  4. 如果计算一堆双精度的总和,结果会是什么数据类型?例如mydouble1 + mydouble2 + mydouble3 == ????你的方法签名声称返回类型是无效的,但这不是我期望从这个双倍添加的数据类型,你呢?

  5. The code you showed is trying to store the sum inside an array. Seems like it would make more sense to create a new variable inside the method called sum, store the result inside this new variable, and then return that variable
  6. 您展示的代码是尝试将总和存储在数组中。看起来在称为sum的方法中创建一个新变量更有意义,将结果存储在这个新变量中,然后返回该变量

  7. Try very hard to avoid putting a semicolon after an if statement at this point in your coding experience. Specifically, the semicolon here can cause you a ton of problems, so remove it: if (a[i] < a[i + 1]);
  8. 在你的编码经验中,尽量避免在if语句之后添加分号。具体来说,这里的分号会引起很多问题,所以删除它:if(a [i]

2.Write a class/static method that takes an array of int as a parameter and returns an array of type boolean of the same length as the parameter.

2.编写一个类/静态方法,该方法将int数组作为参数,并返回与参数长度相同的boolean类型的数组。

You've defined a method public static void array(int[] a) to accomplish this. Some hints:

您已经定义了一个方法public static void array(int [] a)来完成此任务。一些提示:

  1. the requirement is to return an array of boolean. You're returning a void. This is the same mistake you make in the first problem - perhaps review how method signatures look in java, specifically how you define the type of the return value
  2. 要求是返回一个布尔数组。你回来了一个虚空。这是你在第一个问题中犯的同样错误 - 也许回顾一下java中方法签名的外观,特别是你如何定义返回值的类型

  3. your code here is not remotely doing what the requirement asks, looks like this is leftover code from somewhere else. Start by figuring out what return type you need for an array of booleans, and then create an empty boolean array, then try to figure out how to fill in that array of boolean from the array of integers you get passes
  4. 你的代码不是远程执行要求所要求的,看起来这是来自其他地方的剩余代码。首先找出布尔数组所需的返回类型,然后创建一个空的布尔数组,然后尝试弄清楚如何从你得到的整数数组中填充布尔数组

3.Write a class/static method that takes an array of Strings as a parameter and returns an array of Strings of the same length as the original array.

3.编写一个类/静态方法,它将一个字符串数组作为参数,并返回一个与原始数组长度相同的字符串数组。

  1. Ok, you definitely don't yet know how to define an array of strings in java. Google "java string array" and read the first result. Try to first write a method signature (e.g. just the public return_stuff my_method_name(input_stuff my_argument)) first that properly replaces return_stuff and input_stuff what a java string array
  2. 好的,你肯定还不知道如何在java中定义一个字符串数组。谷歌“java字符串数组”并读取第一个结果。首先尝试先写一个方法签名(例如只是公共return_stuff my_method_name(input_stuff my_argument)),然后正确地替换return_stuff和input_stuff一个java字符串数组

EDIT: making this a community wiki so others can update as OP changes their code problem. Feel free to contribute, but try to remember OP is clearly working out the basics of this problem, he/she probably doesn't need to understand coding conventions until at least lesson 2 :-)

编辑:使这个社区维基,以便其他人可以更新,因为OP改变了他们的代码问题。随意贡献,但尝试记住OP显然正在解决这个问题的基础知识,他/她可能不需要了解编码约定,直到至少第2课:-)