I am writing a java program that asks the user to enter the marks(for 2 subjects) of each student(for a total of 4 students). Basically, it is a 2D array operation.
I am stuck at the point where I need to have the user enter the input. I'm thinking the normal method of input.nextInt() won't work. Here's my code so far. Can someone please help?
package Chapter2_Arrays;
import java.util.*;
public class Arrays_ExTwo {
static final int numberOfSubjects=2;
static final int numberOfStudents=4;
static int [][] marks=new int[numberOfSubjects][numberOfStudents];
static Scanner in=new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Welcome to Cincinnati Elementary");
System.out.println("There are "+ " "+ numberOfSubjects+ " "+ "subjects being taught");
System.out.println("There are "+" "+numberOfStudents+" "+" number of students in the class");
System.out.println("Enter the subject number followed by the marks of all the students for the subject");
for(int i=0;i<numberOfSubjects;i++)
{
for(int j=0;j<numberOfStudents;j++)
{
marks[i][j]=in.
}
}
}
}
Try this:
package Chapter2_Arrays;
import java.util.*;
public class Arrays_ExTwo {
static final int numberOfSubjects=2;
static final int numberOfStudents=4;
// byte is a number from -128 to 127; that's big enough for 1-6 marks; you can use char for 'A'-'F' marks.
static byte [][] marks=new byte[numberOfSubjects][numberOfStudents];
static Scanner in=new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Welcome to Cincinnati Elementary");
System.out.println("There are "+ " "+ numberOfSubjects+ " "+ "subjects being taught");
System.out.println("There are "+" "+numberOfStudents+" "+" number of students in the class");
for(int i=0;i<numberOfSubjects;i++)
{
System.out.println("Enter the marks of students for subject no. "+(i+1)+":");
for(int j=0;j<numberOfStudents;j++)
{
marks[i][j]=in.nextByte();
System.out.println("Mark of student no. " + (j+1) + " for subject no. " + (i+1) + "was set to " + marks[i][j]);
}
}
}
}
Try this:
import java.util.Scanner;
public class temp {
public static void main(String[] args) {
int[][] my_array = create2DArray(4, 2);
takeUserInput(my_array);
displayElements(my_array);
}
public static int[][] create2DArray(int numRows, int numCols){
int[][] arrayInput = new int[numRows][numCols];
return arrayInput;
}
public static void takeUserInput(int[][] arrayInput){
Scanner sc = new Scanner(System.in);
for(int i = 0; i < arrayInput.length; i++){
for(int x = 0; x < arrayInput[i].length; x++) {
System.out.print("Enter number for the Index "+ i + "," + x + " ");
arrayInput[i][x] = sc.nextInt();
}
}
}
public static void displayElements(int[][] arrayInput){
for(int i = 0; i < arrayInput.length; i++){
for(int x = 0; x < arrayInput[i].length; x++) {
System.out.println(arrayInput[i][x]);
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.