S
S
savoid2020-12-22 20:06:04
Java
savoid, 2020-12-22 20:06:04

Working with two-dimensional arrays, how to fix the code?

Output an array of 10 columns and 10 rows. Depending on the entered digit (from 1 to 2), output the desired version of the array. If the number is 1, then display the first option, if the number is 2, then display the second option. I've been sitting on this problem long enough and I feel like I've gone in the wrong direction. Help!

Here's what I got. Obviously not what I need. Already confused! Help x2!

import java.util.Scanner;

public class triangles {
  static int [][]  mas = new int [10][10];
  static Scanner sc = new Scanner(System.in);
  static int k;
  
  public static void PrintMasiv() {
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) 
      System.out.print(mas[i][j] + "\t");
      System.out.println();
    }
  }
  
  public static void VariantA() {
    int C = 1;
    System.out.println(" ");
    System.out.println("Variant A:");
    for (int i=0; i<=9; i++)
        for (int j=9-i; j>=7-i; j--) 
             if (j>=0) {
                mas[i][j] = C; C=C+1;
            }
    PrintMasiv();

  }

  public static void VariantB() {
    System.out.println(" ");
    System.out.println("Variant B:");
    for (int j=9; j>=1; j--)
           for (int i=9; i>=10-j; i--)
               mas[i][j] = 19-i-j;
    PrintMasiv();

    }
  
  public static void main(String[] args) {
    
    System.out.println(" ");
    System.out.print("Vvedite nomer varianta (variant A - 1, variant B - 2): ");
    
    do {
      while (!sc.hasNextInt()) {
        System.out.println(" ");
        System.out.println("Oshibka!");
        sc.next();
        System.out.print("Vvedite nomer varianta (variant A - 1, variant B - 2): ");
      }
        k = sc.nextInt();
       
        if (k > 2 || k < 1) {
        	System.out.println(" ");
        	System.out.println("Nomer vne diapazona, vvedite eshe raz!");
        	System.out.print("Vvedite nomer varianta (variant A - 1, variant B - 2): ");
        }
    }while (!(k >= 1 && k <= 2));
    sc.close();
        
    System.out.println(" ");
    System.out.println("Nachalnij massiv:");
    System.out.println(" ");
    PrintMasiv();
    if (k == 1) {
      VariantA();
    } else if (k == 2) {
      VariantB();
    }
  }
}


From the picture: On the left is the output of the first option, on the right is option B, that is, the second.
5fe2273629621585218949.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question