0

I have an assignment which must be like this way I want the user to enter how many modules he has in the college (so this will be in the first array), then I want to know from the user what are the marks he got in the assignment.

I wrote it but I have a problem with the code If anyone can help me, I will be grateful.

public class Main {
  public static void main(String[] args) {
    Scanner Sc = new Scanner(System.in);
    int mo, i, j, a;
    System.out.println(" Enter NO.modules you have");
    mo = Sc.nextInt();
    // create the Array
    int[] tab = new int[mo];
    for (a = 0; a < mo; a++) {
      System.out.println(" Enter the module name : " + (a + 1));
      tab[a] = Sc.nextInt();
    }
    int[][] list = new int[2][mo];
    for (i = 0; i < 2; i++) {
      for (j = 0; j < mo; j++) {
        System.out.println(" Enter the marks of the Assignment : "+(i+1)+","+j);
        list[i][j] = Sc.nextInt();
      }
    }
  }
}
1
  • Hi there Beth. It would be easier for people to answer if you can share the error that you are facing and what have you done to fix it as well. Commented May 10, 2021 at 17:49

2 Answers 2

1

I think you are creating an array of wrong bounds- instead of int [][] list=new int [2][mo] there should be int [][] list=new int [mo][2]; ,because for every module you have marks of first and second assignment, am I right?

Sign up to request clarification or add additional context in comments.

Comments

0

You can try this way

import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner Sc = new Scanner(System.in);
    int mo, i, j, a;
    System.out.println(" how many modules you have in this semester ?");
    mo = Sc.nextInt();
    // creat the Array
    int[] tab = new int[mo];
    for (a = 0; a < mo; a++) {
      System.out.println(" Enter the module name : " + (a + 1));
      tab[a] = Sc.nextInt();
    }
    int[][] list = new int[2][mo];
    for (i = 0; i < 2; i++) {
      for (j = 0; j < mo; j++) {
        System.out.println(" Enter the marks of the " +
          (i + 1) + " Assignment : " + (i + 1) + "," + j);
        list[i][j] = Sc.nextInt();
      }
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.