2D阵列用户输入

时间:2013-12-16 14:01:56

标签: multidimensional-array 2d

大家好我是java新手,我需要帮助我们老师给我们的这个任务...这是我的程序...目标是每次我选择一个课程和时间输出将显示1和每次用户选择课程或时间表输出填满....对不起,如果我不能解释清楚我的英语很糟糕.....

import java.io.*;
public class Array2D_input {

public static void main(String[] args)throws IOException {
BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
int X=0;
int num=0;
String [] subject={" ","C#Programming","Autocad","Robotics","JavaProgramming"};
String [] time={" ","8:00 - 12:00","12:00 - 4:00","4:00 - 8:00"};

    int [][] N=new int[5][4];
do{
    System.out.println("SUBJECT ENROLLMENT\n");
    System.out.println("Subjects Offered");

    for(int s=1;s<=4;s++)
    {
        System.out.print(" "+s+" - "+subject[s]);
        System.out.println();
    }
        System.out.println();
        System.out.print("YOUR CHOICE :   ");
        int a=Integer.parseInt(in.readLine());
        N[4][a]++;
    for(int t=1;t<=3;t++)
     {
        System.out.print(" "+t+" - "+time[t]);
        System.out.println();   
    }
        System.out.print("TIME SCHEDULE :   ");
        int tm=Integer.parseInt(in.readLine());

        num=num+1;  



        System.out.print("More Entries <Y/N> : ");
        X=in.readLine().charAt(0);  

   }    
  while(X=='Y');    


    //System.out.print("\n\tENROLLMENT SUMMARY\n");
    System.out.print("\t\t         TIME SCHEDULE\n");
    System.out.print("SUBJECTS\t   8:00-12:00\t 12:00-4:00\t     4:00-8:00\t total" );

    for(int s=0; s<5; s++)
    {
        System.out.print(""+subject[s]);
        System.out.println();
        for(int t=0; t<4; t++)
            System.out.print("\t\t\t    "+N[s][t]);
            System.out.println();

    }

 }

}

2 个答案:

答案 0 :(得分:0)

以下是您的任务的修订代码,希望它能为您提供帮助。

import java.io.*;

public class Array2D_input {

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String x = "";
        String[] subject = { "C#Programming", "Autocad", "Robotics", "JavaProgramming" };
        String[] time = { "8:00 - 12:00", "12:00 - 4:00", "4:00 - 8:00" };
        int[][] N = new int[subject.length][time.length];
        // Initializing array with default values
        for (int i = 0; i < subject.length; i++) {
            for (int j = 0; j < time.length; j++) {
                N[i][j] = 0;
            }
        }
        do {
            System.out.println("SUBJECT ENROLLMENT\n");
            System.out.println("Subjects Offered");

            for (int s = 0; s < subject.length; s++) {
                System.out.print(" " + (s + 1) + " - " + subject[s]);
                System.out.println();
            }
            System.out.println();
            System.out.print("YOUR CHOICE :   ");
            int a = readNumber(in, subject.length);
            for (int t = 0; t < time.length; t++) {
                System.out.print(" " + (t + 1) + " - " + time[t]);
                System.out.println();
            }
            System.out.print("TIME SCHEDULE :   ");
            int tm = readNumber(in, time.length);
            N[a - 1][tm - 1]++;

            System.out.print("More Entries <Y/N> : ");
            x = in.readLine();

        } while (x.equalsIgnoreCase("Y"));

        // System.out.print("\n\tENROLLMENT SUMMARY\n");
        System.out.print("\t\t         TIME SCHEDULE\n");
        System.out.print("SUBJECTS\t   8:00-12:00\t 12:00-4:00\t     4:00-8:00\t Total");

        for (int s = 0; s < subject.length; s++) {
            int count = 0;
            System.out.println();
            System.out.println(subject[s]);
            for (int t = 0; t < time.length; t++) {
                System.out.print("\t\t    " + N[s][t]);
                count += N[s][t];
            }
            System.out.print("\t\t    " + count);
        }

    }

    /**
     * Function to read input from the console and also check for max value
     * 
     * @param in
     * @param maxLimit
     * @return
     * @throws IOException
     */
    public static int readNumber(BufferedReader in, int maxLimit) throws IOException {
        int choice = 0;
        try {
            choice = Integer.parseInt(in.readLine());
        } catch (NumberFormatException nf) {
            System.out.println("Enter integer only:");
            choice = readNumber(in, maxLimit);
        }
        if (choice > maxLimit) {
            System.out.println("Enter only given options");
            choice = readNumber(in, maxLimit);
        }
        return choice;
    }
}

答案 1 :(得分:0)

java 2d数组输入示例:

//Coded BY Anurag Goel
//Basic 2DArray Program
import java.util.Scanner;
public class array2d {
public static void main(String args [])
{
int [][] arr =new int[5][5];
System.out.println("Enter student roll no and their subject codes");
Scanner o = new Scanner(System.in);
for(int i=0;i<5;i++)
{
System.out.println("Enter "+(i+1)+"th student subject codes ");
for(int k=0;k<5;k++)
{
System.out.println("Enter "+(k+1)+"th subject code : ");
arr[i][k]=o.nextInt();
}
}
for(int j=0;j<5;j++)
{
System.out.print(""+(j+1)+"th student subject codes ");
for(int l=0;l<5;l++)
{
System.out.print(" "+arr[j][l]+" ");
}
System.out.println("");

}

}
}