2015-09-23 91 views
-1
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 
+1

那么,究竟是不是为你工作?另外:考虑你的标签,这是Java还是C++? – Stultuske

+1

你有什么问题?因为你的代码在'java.'中,所以删除'C++'标记 – Rustam

+0

我建议首先确定你的* *得到了什么输出。 – user2079303

回答

0

你的代码有很多bug。

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 
+0

感谢兄弟,你是一个非常大的帮助。这节省了我在班上的另一个学期。非常感谢你 :) –

0

的问题是在这里

for (int indx = 0; indx < scores.length; indx++){ 
     System.out.println("\t Score: "); 
     int score = sc.nextInt(); 

loop迭代,直到scores.lengthscores是不确定的。