返回两个或更多值

时间:2017-04-17 04:40:50

标签: java arrays

我正在学习java,我需要一些关于返回多个值的帮助。我想方设法做但却感到困惑。希望你们能帮助我。我试图在菜单中做出一个选项,允许用户用新的数字和新的评级替换当前的球衣号码。我删除了代码的其他部分以缩短它。谢谢。

import java.util.Scanner;

public class Roster {

final static int numArray = 5;
static int [] numJersey = new int [numArray];
static int [] numRating = new int [numArray];
static int i = 0;

public static void main (String[] args) {
    Scanner scnr = new Scanner(System.in);

    int jersey = 0;
    int newRating = 0;
    int newJersey = 0;
    char chooseOption;
    boolean quit = false;



 public static int replacePlayer(int jersey, int newJersey, int newRating){

    for(i=0; i<numArray;++i){
        if(numJersey[i]==jersey){
            numJersey[i] = newJersey;
            numRating[i] = newRating;
        }
    }

    return newJersey, newRating;
}

}

3 个答案:

答案 0 :(得分:1)

您可以替换数组中的详细信息,如下所示:

public static void replacePlayer(int jersey, int newJersey, int newRating){
  for(i=0; i<numArray;++i){
      if(numJersey[i]==jersey){
        numJersey[i] = newJersey;//replace existing jersy with new jersy
        numRating[i] = newRating;//replace existing rating with new rating
      }
   }
}

答案 1 :(得分:0)

您不能返回多个值,但您可以通过创建具有2个属性的类来处理它,为该属性分配适当的值并将其返回。希望这个帮助

答案 2 :(得分:0)

Konva.Image

请注意,在发布的代码中,您不会调用//return no value (void) public static void replacePlayer(int jersey, int newJersey, int newRating){ for(i=0; i<numArray;++i){ if(numJersey[i]==jersey){ numJersey[i] = newJersey; //numJersey newRating are class variables so they will be updated numRating[i] = newRating; } } } ,因此它无效。