2017-10-13 25 views
1

我不确定要在我的二维数组中的某个位置查找字符时需要使用什么。我所试图做的是让用户输入的行数和列数和获取价值的通讯员到二维数组中的两个变量,检索,然后它的阵列尝试使用java中的行号和列号在二维数组中找到一个字符

 import java.util.*; 
public class twoDimension { 
    public static void printRow(int[] row) { 

     for (int i : row) { 
      System.out.print(i); 
      System.out.print("\t"); 
      } 
     System.out.println(); 
     } 

    public static void main(String[] args) { 
     int twoDm[][]= new int[3][4]; 

     for (int i = 0; i < twoDm.length; i++) { 
      for (int j = 0; j < twoDm[i].length; j++) { 
       twoDm[i][j] = (int)(Math.random()*10); // we decided to randomise the numbers in the array. 
       } 
      } 

     for(int[] row : twoDm) { 
      printRow(row); 
      } 

     System.out.println("Enter a number with the format 'xy'. x is the row number and y the column number. Whenever you want to quit out, input 'quit'."); 
     String xy; 
     while (!xy.equalsIgnoreCase("quit")) { 
      Scanner scanner = new Scanner(System.in); 
      String xy = scanner.nextLine(); 
      char x = xy.charAt(0); 
      char y = xy.charAt(1); 
      // fetching the character in the array and displaying it 
      // setting it to 0 
      // displaying the array again 
      } 
     } 
    } 

中设置为0 PS:我设法找到了如何在没有逗号或括号的情况下显示数组,但我不确定是否要移除显示中的空格。

+0

通过“空格在显示“你的意思是由'System.out.print(”\ t“)''创建的标签吗? –

+0

是的,我做,对不起 – Dracose

回答

1

如果你把xy坐标整数:

int xCoor = Character.getNumericValue(x); 
int yCoor = Character.getNumericValue(y); 

那么你就可以访问和/或使用这些坐标修改数组:

twoDm[xCoor][yCoor] = 0; 
+0

编译器带来了这个:char不能转换为字符串 \t \t \t int xCoor = Integer.parseInt(x); – Dracose

+1

@Dracose对不起,我认为'x'和'y'是'String'的值。我更新了我的答案来解决这个问题。 –

+1

@Dracose如果这个答案有效,请确保接受它以显示它帮助你的社区。 –

相关问题