英文:
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[]= {"name1","name2"};
//if player count is 6 or less
if (y>=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 < 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("Please enter player names separated by a comma: ");
String nameList = input.nextLine();
String[] playerNames = nameList.split(",");
//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 方法必须返回在方法定义中已定义的内容
请注意以下方法。
> > public String[] imposterChoose(int y, String[] playerList) { > > // ... > > for (int x = 0; x < y; x++) { > > //... > > return new String[] { imposterFinal[0], imposterFinal[1] }; > } > } >
在这里,您只在 for
循环内部进行返回!因此,只有在运行 for
循环时才会返回内容。
也就是说,如果始终满足 x < y
,则此方法将不会进入 for
循环,也不会返回任何内容。
至少它必须返回一个 null
。
public static String[] imposterChoose(int y, String[] playerList) {
// ...
for (int x = 0; x < y; x++) {
//...
return new String[] { imposterFinal[0], imposterFinal[1] };
}
return null;
}
4 当从数字创建 String[] 时,您必须正确地将其转换为字符串。
> > imposterNumbers[0] = Math.round(Math.random() * y); > imposterNumbers[1] = Math.round(Math.random() * y); > > new String[] { imposterFinal[0], imposterFinal[1] }; >
在这里,您直接将 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.
>
> public String[] imposterChoose(int y, String[] playerList) {
>
> // ...
>
> for (int x = 0; x < y; x++) {
>
> //...
>
> return new String[] { imposterFinal[0], imposterFinal[1] };
> }
> }
>
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 < 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 < 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.
>
> imposterNumbers[0] = Math.round(Math.random() * y);
> imposterNumbers[1] = Math.round(Math.random() * y);
>
> new String[] { imposterFinal[0], imposterFinal[1] };
>
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() };
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论