如何从数组方法中创建新数组?

huangapple go评论59阅读模式
英文:

How to create new array from an array method?

问题

以下是您的代码的翻译部分:

package ImposterRandomizer;
import java.util.*;

public class ImposterRandomizer {

    public String[] imposterChoose(int y, String[] playerList) {
        double imposter1;
        double imposter2;
        double imposterNumbers[]= new double[2];
        String imposterFinal[]= {"name1","name2"};

        //如果玩家数量大于等于6
        if (y >= 6) {
            imposterNumbers[0] = Math.round((Math.random()*y));
        }

        //如果玩家数量少于6,需要2个imposter
        else {
            imposterNumbers[0] = Math.round(Math.random()*y);
            imposterNumbers[1] = Math.round(Math.random()*y); 

            //检查imposter数量是否相同
            while (imposterNumbers[0] == imposterNumbers[1]) {
                imposterNumbers[1] = Math.round(Math.random()*y);
            }
        }
        for(int x = 0; x < y; x++) {
            int count = 0;

            if(x == imposterNumbers[0]) {
                imposterFinal[count] = playerList[x];
                count++;
            }
            if(x == imposterNumbers[1]) {
                imposterFinal[count] = playerList[x];
                count++;
            }
            return new String[] {imposterFinal[0], imposterFinal[1]};
        }
    }

    public static void main(String[] args) {
        //声明变量
        Scanner input = new Scanner(System.in);

        String[] imposterFinal = new String[2];

        //系统输出顺序
        System.out.println("请输入以逗号分隔的玩家名称:");
        String nameList = input.nextLine();
        String[] playerNames = nameList.split(",");

        //imposter分配器
        imposterFinal = imposterChoose(playerNames.length, playerNames);
        System.out.println();
    }
}

在第53行,您得到一个消息,提示“赋值的左侧必须是一个变量。”我不确定您是否使用了正确的方法来调用一个创建数组的方法,或者是否在代码的其他地方出现了错误。

提前感谢您的帮助!

英文:

I've been trying to create a small console program inspired by Among Us, however, I am stuck.

Here is my code:

package ImposterRandomizer;
import java.util.*;
public class ImposterRandomizer {
public String[] imposterChoose(int y, String[] playerList) {
double imposter1;
double imposter2;
double imposterNumbers[]= new double[2];
String imposterFinal[]= {&quot;name1&quot;,&quot;name2&quot;};
//if player count is 6 or less
if (y&gt;=6) {
imposterNumbers[0] = Math.round((Math.random()*y));
}
//if player count is greater than 6, 2 imposters are required
else {
imposterNumbers[0] = Math.round(Math.random()*y);
imposterNumbers[1] = Math.round(Math.random()*y); 
//check if imposter numbers are the same
while (imposterNumbers[0]==imposterNumbers[1]) {imposterNumbers[1] = Math.round(Math.random()*y);}
}
for(int x = 0; x &lt; y; x++) {
int count = 0;
if(x==imposterNumbers[0]) {
imposterFinal[count] = playerList[x];
count++;
}
if(x==imposterNumbers[1]) {
imposterFinal[count] = playerList[x];
count++;
}
return new String[] {imposterFinal[0], imposterFinal[1]};
}
}
public static void main(String[] args) {
//declaring varaibles
Scanner input = new Scanner(System.in);
String[] imposterFinal = new String[2];
//sysout print sequence
System.out.println(&quot;Please enter player names separated by a comma: &quot;);
String nameList = input.nextLine();
String[] playerNames = nameList.split(&quot;,&quot;);
//imposter assigner
imposterFinal[] = imposterChoose(playerNames.length, playerNames[]);
System.out.println();
}
}

Basically I'd like to input a list in one line, split the line by commas and put into a new array. I'd then like to use this array later on to do other things, like print out who the two chosen "imposters" are.

On line 53 I get a message that says "The left-hand side of an assignment must be a variable." I am not sure if I am using the right way of calling a method to create an array, or if I'm mistaken somewhere else in the code.

Thanks in advance!

答案1

得分: 0

首先,您需要将 imposterChoose 改为静态,因为您从静态方法中调用它。

public static String[] imposterChoose(int y, String[] playerList) {

其次,将以下部分进行编辑:

imposterFinal[] = imposterChoose(playerNames.length, playerNames[]);

修改为:

imposterFinal = imposterChoose(playerNames.length, playerNames);
英文:

First you need to make imposterChoose to be static, because you call it from a static method

public static String[] imposterChoose(int y, String[] playerList) {

Second, edit

imposterFinal[] = imposterChoose(playerNames.length, playerNames[]);

To

imposterFinal = imposterChoose(playerNames.length, playerNames);

答案2

得分: 0

以下是翻译好的部分:


1 当引用数组时,不要使用 [] 语法。

> imposterFinal[] = imposterChoose(playerNames.length, playerNames[]);

需要改为,

imposterFinal = imposterChoose(playerNames.length, playerNames);


2 非静态方法不应从静态方法中调用

>public String[] imposterChoose(int y, String[] playerList) {\\...}

您正在从主方法中调用此方法,而主方法是一个静态方法。因此,您可以将此方法也设置为静态方法。


3 方法必须返回在方法定义中已定义的内容

请注意以下方法。

> &gt; public String[] imposterChoose(int y, String[] playerList) { &gt; &gt; // ... &gt; &gt; for (int x = 0; x &lt; y; x++) { &gt; &gt; //... &gt; &gt; return new String[] { imposterFinal[0], imposterFinal[1] }; &gt; } &gt; } &gt;

在这里,您只在 for 循环内部进行返回!因此,只有在运行 for 循环时才会返回内容。

也就是说,如果始终满足 x &lt; y,则此方法将不会进入 for 循环,也不会返回任何内容。

至少它必须返回一个 null

public static String[] imposterChoose(int y, String[] playerList) {
    
    // ...

    for (int x = 0; x &lt; y; x++) {

      //...

      return new String[] { imposterFinal[0], imposterFinal[1] };
    }

    return null;
}

4 当从数字创建 String[] 时,您必须正确地将其转换为字符串。

> &gt; imposterNumbers[0] = Math.round(Math.random() * y); &gt; imposterNumbers[1] = Math.round(Math.random() * y); &gt; &gt; new String[] { imposterFinal[0], imposterFinal[1] }; &gt;

在这里,您直接将 Math.round() 的输出用作字符串,这可能是一个语法错误。您可以这样写,

new String[] { imposterFinal[0].toString(), imposterFinal[1].toString() };
英文:

There are few syntax errors can be noted in your code.


1 When referring an array you must not put [] syntax.

> imposterFinal[] = imposterChoose(playerNames.length, playerNames[]);

must be changed to,

imposterFinal = imposterChoose(playerNames.length, playerNames);


2 Non-static methods must not be called from static methods

>public String[] imposterChoose(int y, String[] playerList) {\\...}

You are calling this method from the main method, which is a static method. So you can simply make this method static too.


3 A method must return something as defined in method definition

Please take a note at the following method.

>
&gt; public String[] imposterChoose(int y, String[] playerList) {
&gt;
&gt; // ...
&gt;
&gt; for (int x = 0; x &lt; y; x++) {
&gt;
&gt; //...
&gt;
&gt; return new String[] { imposterFinal[0], imposterFinal[1] };
&gt; }
&gt; }
&gt;

Here, you are only returning inside the for loop! So, it will only return something if the for loop is ran.

i.e. If always x &lt; y then this method will not enter to the for loop and will not return anything.

At least it must return a null.

public static String[] imposterChoose(int y, String[] playerList) {
// ...
for (int x = 0; x &lt; y; x++) {
//...
return new String[] { imposterFinal[0], imposterFinal[1] };
}
return null;
}

4 When creating a String[] from numbers, you must convert them to strings properly.

>
&gt; imposterNumbers[0] = Math.round(Math.random() * y);
&gt; imposterNumbers[1] = Math.round(Math.random() * y);
&gt;
&gt; new String[] { imposterFinal[0], imposterFinal[1] };
&gt;

Here you are directly using outputs from Math.round() as strings which may be a syntax error. So you can just say like,

new String[] { imposterFinal[0].toString(), imposterFinal[1].toString() };

huangapple
  • 本文由 发表于 2020年9月29日 10:52:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64112188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定