亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

[初學JAVA]輸入時有BUG,不知道怎么解決求大神幫忙解釋下。

[初學JAVA]輸入時有BUG,不知道怎么解決求大神幫忙解釋下。

曲胡弓上的緬懷星光 2016-03-10 20:10:20
package 實驗報告2;import java.util.Scanner;public class student { // 創建Sutdent類 public static class Student { int stuScore; String stuName; } public static void main(String[] args) { Student[] stu = new Student[10]; Scanner input = new Scanner(System.in); // 錄入用戶輸入信息 for (int i = 1; i < stu.length; i++) { stu[i] = new Student(); System.out.println("請輸入第" + i++ + "學生姓名:"); stu[i].stuName = input.next(); System.out.println("請輸入學生姓名:"); stu[i].stuScore = input.nextInt(); } //輸出學生信息 System.out.println("學生信息為:"); for (int i = 0; i < stu.length; i++) { System.out.print(stu[i].stuName + stu[i].stuScore); } // 使用for循環,對學生信息進行排序 for (int i = 0; i < stu.length - i - 1; i++) { if (stu[i].stuScore < stu[i + 1].stuScore) { int temp; temp = stu[i].stuScore; stu[i].stuScore = stu[i + 1].stuScore; stu[i + 1].stuScore = temp; } } //輸出排序后學生信息 System.out.println("排序后學生信息為:"); for (int i = 0; i < stu.length; i++) { System.out.print(stu[i].stuName + stu[i].stuScore); } }}
查看完整描述

2 回答

已采納
?
不再熱情似火

TA貢獻15條經驗 獲得超6個贊

錄入、輸出學生信息和后面的輸出排序的后的學生信息,你的i < stu.length全=全部都超出了數組范圍,改成

for(int i=0;i<stu.length-1;i++){

}

排序也出了錯誤,冒泡排序用2個for循環進行排序

查看完整回答
反對 回復 2016-03-13
?
六道骸

TA貢獻30條經驗 獲得超6個贊

冒泡排序你只將stuScore 的信息進行了對調,stuName 卻沒有。

查看完整回答
反對 回復 2016-03-11
  • 2 回答
  • 0 關注
  • 1612 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號