如何通过索引更新整数数组索引的元素

时间:2018-09-18 17:32:48

标签: java arrays

我有一个类似{0,0,0,0}的数组,我想将数组从0更新为1,但一次只能更新一个。 如果我要更新数组的索引0,则必须将数组从0更新为1,如果用户要再次更新,则必须将数组中的索引1从0更新为1。

您要更新阵列吗? y / n 如果y,则数组必须为{1,0,0,0}

是否要再次更新,数组必须为{1,1,0,0},如果用户希望再次预订座位,则继续进行。 构建此程序时,我的问题是,当我更新数组时,数组中的所有元素一次都更新为1。我正在使用Java进行编码。

package ferryassign;
import java.util.Scanner;

public class Ferryassign {

public static void businessclass_update(int myarray1[][]) // definition of function must be outside
    {
        for(int r=0;r<2;r++)
        {
            for(int c=0;c<5;c++)
            {
                if (myarray1[1][4]==1 && myarray1[10][4]==1)
                    System.out.println("Ferry Full");
                else if(myarray1[r][c]==0) // this one problem 
                    myarray1[r][c]=1;
                else if(myarray1[1][4]==1)
                    System.out.println("Business Class Full");
                    break;

            }
        }
    }

public static void main(String[] args)
{
    //main function 
                       //0,1,2,3,4
    int myarray1[][]=  {{0,0,0,0,0}, //0 Business seats
                        {0,0,0,0,0}, //1      
                        {},          //2      
                        {0,0,0,0,0}, //3 Economy seats 
                        {0,0,0,0,0},
                        {0,0,0,0,0},
                        {0,0,0,0,0},
                        {0,0,0,0,0},
                        {0,0,0,0,0},
                        {0,0,0,0,0},
                        {0,0,0,0,0}};

    Scanner input = new Scanner(System.in);
    char ans1;
    do {
            System.out.println("Do you want to buy business seats? y/n");
            char ans=input.next().charAt(0);
            switch(ans)
            {
                case 'y':
                    businessclass_update(myarray1);
                    for(int r=0;r<2;r++) // to print seats use for loop
                    {
                        for(int c=0;c<5;c++)
                        {
                            System.out.println(myarray1[r][c]);
                        }
                    }
                    break;
                case 'n':
                    System.out.println("No business seats selected");
            }
            System.out.println("Do you want to book seats again? y/n");
            ans1=input.next().charAt(0);
        }while(ans1=='y');

    }    
}

2 个答案:

答案 0 :(得分:0)

只需注意小括号和括号:

for(int c=0;c<5;c++)
{
    if (myarray1[1][4]==1 && myarray1[10][4]==1)
        System.out.println("Ferry Full");
    else if(myarray1[r][c]==0) { 
        myarray1[r][c]=1; // Since you are iterating it over an array, it will update all the positions 
        break; //With this break you'll do only one time
    }
    else if(myarray1[1][4]==1) {
        System.out.println("Business Class Full");
        break;
    }
}

输出:

Do you want to buy business seats? y/n
y
1
1
0
0
0
1
1
0
0
0
Do you want to book seats again? y/n
y
Do you want to buy business seats? y/n
y
1
1
1
0
0
1
1
1
0
0
Do you want to book seats again? y/n
y
Do you want to buy business seats? y/n
y
1
1
1
1
0
1
1
1
1
0

答案 1 :(得分:0)

为什么要使用2个for循环?而是使用两个全局变量r和c并在每次调用该函数并添加一个席位时对其进行递增。

  int r=0,c=0;             //r and c are global variables

  public static void businessclass_update(int myarray1[][]) 
  {   

           if (myarray1[1][4]==1 && myarray1[10][4]==1)
               System.out.println("Ferry Full");

            else if(myarray1[r][c]==0) 
                {
                         myarray1[r][c]=1;
                         c++;
                         if(c==5)                //If row is full move to next row
                         {
                             c=0;
                             r++;
                         }
                 }

            else if(myarray1[1][4]==1)
                System.out.println("Business Class Full");

}
相关问题