java 對象數組

來源:互聯網
上載者:User

標籤:rom   com   姓名   col   output   pre   str   int   for   

java 對象數組

from [email protected] 

先 用類聲明數組;

再把類的執行個體賦給數組;

 

package works;import java.util.Scanner;public class test {    public static void main(String[] args) {           manage m = new manage();           m.input();           m.output();           m.select_sort();    }     }class student{      int  sno; //學號      String sname;//姓名      int score; //成績           void setdata(int sno,String sname,int score) {          this.sno=sno;          this.sname=sname;          this.score=score;      }      void display() {        System.out.println("學生學號"+sno+"學生姓名"+sname+"成績"+score);      }      int getsno() {          return sno;      }      String sname() {          return sname;      }      int getscore() {              return score;      }  }class manage{     int num=4;     student s[]=new student[num];//聲明對象數組          void input (){         System.out.println("輸入學生學號,學生姓名,學產生績");         Scanner input = new Scanner(System.in);            for(int i=0;i<num;i++) {                 s[i]=new student(); //類的執行個體賦給數組                 s[i].setdata(input.nextInt(),input.next(),input.nextInt());                           }              }     void output() {         for(int i=0;i<num;i++)          s[i].display();               }     void select_sort() { //從小到大         for(int i=0;i<num-1;i++)              for(int j=i+1;j<num;j++) {             if(s[i].sno>s[j].sno) {                 int temp=s[i].sno;                 s[i].sno=s[j].sno;                 s[j].sno=temp;             }         }         for(int i=0;i<num;i++) {             s[i].display();         }     }     }
輸入學生學號,學生姓名,學產生績1003a1001002b1001009c1001000d100學生學號1003學生姓名a成績100學生學號1002學生姓名b成績100學生學號1009學生姓名c成績100學生學號1000學生姓名d成績100學生學號1000學生姓名a成績100學生學號1002學生姓名b成績100學生學號1003學生姓名c成績100學生學號1009學生姓名d成績100

 

java 對象數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.