2013-10-30 23 views
0

/* *(排序學生)編寫一個程序,提示用戶輸入學生的分數數量, *學生的名字,並打印學生的名字在降低 *順序的分數。 */以相同的輸出兩倍

package homework6_17; 

import java.util.Scanner; 

public class Homework6_17 { 
public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 
    System.out.print("Enter number of students: "); 
    int numberOfStudents = input.nextInt(); 
    String[] names = new String[numberOfStudents]; 
    for (int i = 0; i < numberOfStudents; i++) { 
     System.out.println("Enter the name of student: ");    
     names[i] = input.nextLine(); 

    } 
    double[] scores = new double[numberOfStudents]; 
    for (int i = 0; i < numberOfStudents; i++) { 
     System.out.println("Enter the score of student: ");    
     scores[i] = input.nextDouble(); 
    } 

    String temps = ""; 
    double temp = 0; 
    double max = scores[0]; 
    for(int i = 0; i<(scores.length-1); i++){ 
     if(scores[i+1]>scores[i]){ 
      temp=scores[i+1]; 
      scores[i]=scores[i+1]; 
      scores[i+1]=scores[i]; 

      temps = names[i+1]; 
      names[i]=names[i+1]; 
      names[i+1]=names[i]; 
     } 
    } 
    for(int i = 0 ; i<(scores.length-1); i++) 
     System.out.println(names[i]+ " " + scores[i]);   


} 
} 

當我運行這個程序; 運行:

學生輸入數字:3

輸入學生的名字: 輸入學生的名字: 一個

輸入學生的名字: b

進入學生分數: c

Exception in thread "main" java.util.InputMismatchException 

//我得到了「輸入學生的名字:」兩次而不是一次。

回答

0

首先想到的第一件事(不確定這裏是否正確)是您鍵入的數字學生並按「輸入」。它讀取第一個int(3)並讀取「enter」作爲第一個學生的第一個輸入。

也許試試int numberOfStudents = Integer.ParseInt(input.nextLine());? 這樣,新行不會被添加到學生。

0

您只需在您的for循環中爲每個學生打印短語時刪除第一個System.out.print("Enter number of students: ");。因此,您爲第一個學生打印兩次(一次在循環之前,一次在循環中)

+1

此外,如果你想知道爲什麼它打印它的第一個是是System.out.print'的方式()'第二個是'System.out.println()',它打印一個新行。 –

0

在SO中回答作業問題並不是一個好主意。但既然你已經嘗試了一些代碼,這是確定的回答Q.看看:

import java.util.Scanner; 

public class Homework6_17 { 

    public static void main(String[] args) { 

     Scanner input = new Scanner(System.in); 
     System.out.print("Enter number of students: "); 
     int numberOfStudents = input.nextInt(); 
     String[] names = new String[numberOfStudents]; 

     for (int i = 0; i < numberOfStudents; i++) { 
      System.out.println("Enter the name of student #" + (i + 1) + ":"); 
      names[i] = input.next(); 
     } 

     double[] scores = new double[numberOfStudents]; 
     for (int i = 0; i < numberOfStudents; i++) { 
      System.out.println("Enter the score of student " + names[i] + ":"); 
      scores[i] = input.nextDouble(); 
     } 

     String tempName; 
     double tempScore; 
     for (int i = 0; i < numberOfStudents; i++) { 
      for (int k = i + 1; k < numberOfStudents; k++) { 
       if (scores[k] > scores[i]) { 
        tempName = names[i]; 
        tempScore = scores[i]; 
        names[i] = names[k]; 
        scores[i] = scores[k]; 
        names[k] = tempName; 
        scores[k] = tempScore; 
       } 
      } 
     } 

     for (int i = 0; i < numberOfStudents; i++) 
      System.out.println(names[i] + " " + scores[i]); 

    } 
} 
相關問題