数组和循环结构

时间:2015-09-23 11:30:55

标签: java arrays for-loop

static Scanner scan = new Scanner(System.in);
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
    System.out.println("Enter number of arrays: ");
    int noOfArrays = scan.nextInt();
    String students[] = new String[noOfArrays];

    System.out.println("----------------------");
    while (true){
    System.out.println("Enter Student names and scores:");

    for (int idx = 0; idx < students.length; idx++){
    int scores[] = new int[noOfArrays];
        System.out.println("\t"+ (idx+1)+ ". ");
        String studName = sc.nextLine();
        students[idx] = studName;
        for (int indx = 0; indx < scores.length; indx++){
            System.out.println("\t Score: ");
            int score = sc.nextInt();
            scores[indx] = score;            
        }            
        }
    }

}

我需要获得此输出,有人可以帮助我吗?

Enter number of arrays: 3
"----------------------
Enter Student names and scores:
    1. Name
    Score: 81
    2. Name2
    Score: 82
    3. Name3
    Score: 83

2 个答案:

答案 0 :(得分:0)

问题出在这里

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <thead>
    <th><input type='checkbox' id = 'mainChkBox'/>Click here<th>
  </thead>
  <tbody>
   <tr>  <td><input type='checkbox'/><td></tr>
    
   <tr>  <td><input type='checkbox'/><td></tr>
    
   <tr>  <td><input type='checkbox'/><td></tr>
    
   <tr>  <td><input type='checkbox'/><td></tr>
    
   <tr>  <td><input type='checkbox'/><td></tr>
    
   <tr>  <td><input type='checkbox'/><td></tr>
    
   <tr>  <td><input type='checkbox'/><td></tr>
    
  </tbody>
  <table>

for (int indx = 0; indx < scores.length; indx++){ System.out.println("\t Score: "); int score = sc.nextInt(); 会一直迭代loop,但scores.length未定义。

答案 1 :(得分:0)

  

您的代码有很多错误。

public static void main(String[] args) throws IOException {

           Scanner scan = new Scanner(System.in);
           Scanner sc = new Scanner(System.in);
           System.out.print("Enter number of arrays: ");
            int noOfArrays = scan.nextInt();
            String students[] = new String[noOfArrays];
            int idx=0;
            System.out.println("----------------------");

            System.out.println("Enter Student names and scores:");
            int scores[]=new int[noOfArrays];
            for ( idx = 0; idx < students.length; idx++){
                System.out.print("\t"+ (idx+1)+ ". Name ");
                sc.next(); // this is to skipping next line
                students[idx] = sc.nextLine();
                System.out.print("\tScore: ");
                    scores[idx] =sc.nextInt();        

            }

            for (int i=0;i<students.length;i++) {
                System.out.println("Name="+students[i]+"\tScore="+scores[i]  );// for printing student name and score
            }

       }

输出:

Enter number of arrays: 3
----------------------
Enter Student names and scores:
    1. Name raja
    Score: 54
    2. Name gita
    Score: 99
    3. Name sita
    Score: 88