二維數組學生成績java
㈠ java編程 保存十個同學的三門功課成績使用二維數組
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
int[][] a = new int[10][3];
String str[] = { "A", "B", "C" };
Scanner scan = new Scanner(System.in);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
System.out.print("輸入第" + (1 + i) + "位同學的" + str[j] + "的分數:");
a[i][j] = scan.nextInt();
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
System.out.println("第" + (1 + i) + "位同學的" + str[j] + "的分數為:"+a[i][j]);
}
}
}
}
㈡ java二維數組怎麼定義
java中使用 [][] 來定義二維數組,定義數組時也可同時初始化。
兩種初始化形式:
格式1、動態初始化
數據類型 數組名 [ ][ ] = new 數據類型[m][n]
數據類型 [ ][ ] 數組名 = new 數據類型[m][n]
數據類型 [ ] 數組名 [ ] = new 數據類型[m][n]
舉例:int [ ][ ] arr=new int [5][3]; 也可以理解為「5行3例」。
格式2、 靜態初始化
數據類型 [ ][ ] 數組名 = {{元素1,元素2....},{元素1,元素2....},{元素1,元素2....}.....};
舉例:int [ ][ ] arr={{22,15,32,20,18},{12,21,25,19,33},{14,58,34,24,66},}。
㈢ 一個年級有m個班,每個班有n個學生,編寫java程序用二維數組保存學生成績,計算學生平均成績
publicclassScoreDemo{
publicstaticvoidmain(String[]args){
intm=2;//班級個數
intn=3;//每個班級的人數
int[][]ary=newint[m][n];
inputScore(ary);//輸入每個人的分數,保存到數組
printAvg(ary);//列印平均分
}
privatestaticvoidprintAvg(int[][]ary){
intindex=0;//計算人數
inttotal=0;//累加總分
for(inti=0;i<ary.length;i++){
intclassTotal=0;//班級總分
for(intj=0;j<ary[i].length;j++){
index++;
intscore=ary[i][j];
classTotal+=score;
}
total+=classTotal;
System.out.println("---------"+(i+1)+"班考試情況---------");
System.out.println("班級總分"+classTotal+" 班級人數"+ary[i].length);
System.out.println((i+1)+"班級平均分"+classTotal*1.0/ary[i].length);
}
System.out.println("---------年級考試情況-------");
System.out.println("年級總分"+total+" 年級人數"+index);
System.out.println("年級平均分:"+total*1.0/index);
}
privatestaticvoidinputScore(int[][]ary){
Scannerinput=newScanner(System.in);
for(inti=0;i<ary.length;i++){
System.out.println("-------"+"班級"+(i+1)+"-------");
for(intj=0;j<ary[i].length;j++){
System.out.print("第"+(j+1)+"號的成績:");
intscore=input.nextInt();
ary[i][j]=score;
}
}
}
}
運行測試
-------班級1-------
第1號的成績:80
第2號的成績:90
第3號的成績:100
-------班級2-------
第1號的成績:100
第2號的成績:100
第3號的成績:90
---------1班考試情況---------
班級總分270 班級人數3
1班級平均分90.0
---------2班考試情況---------
班級總分290 班級人數3
2班級平均分96.66666666666667
---------年級考試情況-------
年級總分560 年級人數6
年級平均分:93.33333333333333
㈣ Java二維數組輸出成績
packageclient;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JComboBox;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
{
privateJFramejf;
privateJPaneljp;
privateJLabelname,course;
privateJComboBoxname1,course1;
privateJButtonok;
privatescoretheScore[]=newscore[3];
publicstudent(){
jf=newJFrame();
jp=newJPanel();
name=newJLabel("姓名");
course=newJLabel("科目");
jp.setLayout(newGridLayout(3,2));
StringtheName[]={"大雄","靜香","康夫","all"};
StringtheCourse[]={"數學","英語","計算機","all"};
name1=newJComboBox(theName);
course1=newJComboBox(theCourse);
ok=newJButton("查詢");
ok.addActionListener(this);
jp.add(name);
jp.add(name1);
jp.add(course);
jp.add(course1);
jp.add(ok);
jf.setLayout(newGridLayout(1,1));
jf.add(jp);
jf.setSize(400,150);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicvoidinputData()//輸入數據
{
this.theScore[0]=newscore("大雄",10,12,15);
this.theScore[1]=newscore("靜香",88,98,75);
this.theScore[2]=newscore("康夫",85,75,90);
}
publicstaticvoidmain(String[]args){
studentstu=newstudent();
stu.inputData();
}
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
if(e.getSource()==ok){
Stringcontent="";
if(this.name1.getSelectedIndex()==3)//輸出全部人的
{
if(this.course1.getSelectedIndex()==3)//全部分數
{
content+="姓名數學英語計算機 ";
for(inti=0;i<this.theScore.length;i++){
content+=(this.theScore[i].getName()+""
+this.theScore[i].getMath()+""
+this.theScore[i].getEnglish()+""
+this.theScore[i].getComputer()+" ");
}
JOptionPane.showMessageDialog(null,content);
return;
}else{
if(this.course1.getSelectedIndex()==0){
content+="姓名數學 ";
for(inti=0;i<this.theScore.length;i++){
content+=(this.theScore[i].getName()+""
+this.theScore[i].getMath()+" ");
}
}elseif(this.course1.getSelectedIndex()==1){
content+="姓名英語 ";
for(inti=0;i<this.theScore.length;i++){
content+=(this.theScore[i].getName()+""
+this.theScore[i].getEnglish()+" ");
}
}else{
content+="姓名計算機 ";
for(inti=0;i<this.theScore.length;i++){
content+=(this.theScore[i].getName()+""
+this.theScore[i].getComputer()+" ");
}
}
JOptionPane.showMessageDialog(null,content);
return;
}
}
//System.out.println("nameindex="+name1.getSelectedIndex()+"courseindex="+course1.getSelectedIndex());
else{
if(this.course1.getSelectedIndex()==3){
content+="姓名數學英語計算機 ";
intindex=this.name1.getSelectedIndex();
content+=(this.theScore[index].getName()+""
+this.theScore[index].getMath()+""
+this.theScore[index].getEnglish()+""
+this.theScore[index].getComputer()+" ");
JOptionPane.showMessageDialog(null,content);
return;
}else{
inti=this.name1.getSelectedIndex();
if(this.course1.getSelectedIndex()==0){
content+="姓名數學 ";
content+=(this.theScore[i].getName()+""
+this.theScore[i].getMath()+" ");
}elseif(this.course1.getSelectedIndex()==1){
content+="姓名英語 ";
content+=(this.theScore[i].getName()+""
+this.theScore[i].getEnglish()+" ");
}else{
content+="姓名計算機 ";
content+=(this.theScore[i].getName()+""
+this.theScore[i].getComputer()+" ");
}
JOptionPane.showMessageDialog(null,content);
return;
}
}
}
}
}
classscore{//保存每個人信息的類
privateStringname;
privatefloatMath;
privatefloatEnglish;
privatefloatComputer;
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicfloatgetMath(){
returnMath;
}
publicvoidsetMath(floatmath){
Math=math;
}
publicfloatgetEnglish(){
returnEnglish;
}
publicvoidsetEnglish(floatenglish){
English=english;
}
publicfloatgetComputer(){
returnComputer;
}
publicvoidsetComputer(floatcomputer){
Computer=computer;
}
publicscore(Stringname,floatMath,floatEnglish,floatComputer){
this.name=name;
this.Math=Math;
this.English=English;
this.Computer=Computer;
}
}
=================
㈤ java如何創建二維數組進行學生姓名,學號,成績三個信息的存儲,使用for循環語句進行二維數組的遍
1、分析題目,需要存儲姓名學號成績,因此,二維數組需要定義成字元串類型
姓名學號成績
012
0[][][]
1[][][]
2[][][]
數組的每列存儲內容是固定的,一行代表一個學生,遍歷過程嵌套for循環就可以遍歷全部學生,
intn=input;
Stringstu[][]=newString[n][2];//學生個數不定
for(inti=0;i<n;i++){
//i是行,一行就是一個「學生」,如i=0,是第一個學生
for(intj=0;j<3,j++)
{//j,是列,是每個學生的信息,0是姓名,1是學號,2是成績
//循環過程是i=0是,遍歷[0][0],[0][1],[0][2],也就依次是姓名學號成績
System.out.print(stu[i][j]);
}
System.out.println("");//輸完一個學生,換行
}
2、源代碼如下
publicstaticvoidmain(Stringargs[])
{
System.out.println("請輸入學生個數");
Scannerinput=newScanner(System.in);
intn=input.nextInt();
Stringstu[][]=newString[n][3];//學生個數不定
//輸入信息
for(inti=0;i<n;i++){
//i是行,一行就是一個「學生」,如i=0,是第一個學生
System.out.println("請輸入第"+(i+1)+"個學生的信息:");
for(intj=0;j<3;j++)
{//j,是列,是每個學生的信息,0是姓名,1是學號,2是成績
//循環過程是i=0是,遍歷[0][0],[0][1],[0][2],也就依次是姓名學號成績
switch(j){
case0:
System.out.print("姓名:");
stu[i][j]=input.next();
break;
case1:
System.out.print("學號:");
stu[i][j]=input.next();
break;
case2:
System.out.print("成績:");
stu[i][j]=input.next();
break;
default:
break;
}
}
}
//輸出信息
for(inti=0;i<n;i++){//循環同理
for(intj=0;j<3;j++)
{//j,是列,是每個學生的信息,0是姓名,1是學號,2是成績
//循環過程是i=0是,遍歷[0][0],[0][1],[0][2],也就依次是姓名學號成績
switch(j){
case0:
System.out.print("姓名:");
System.out.print(stu[i][j]);
break;
case1:
System.out.print("學號:");
System.out.print(stu[i][j]);
break;
case2:
System.out.print("成績:");
System.out.print(stu[i][j]);
break;
default:
break;
}
}
System.out.println("");//輸完一個學生,換行
}
}
3、運行效果如圖
㈥ 用java二維數組完成兩個同學三科成績,計算每位同學的總分, 各科的平均分
簡單寫了下,應該符合題主的要求:
publicstaticvoidcompute(){
版//總分
int[]sum=newint[]{0,0};
//平均分
float[]avgs=newfloat[]{0,0,0};
//兩名學生的分數權
int[][]scores=newint[][]{{99,98,97},{59,60,58}};
for(inti=0;i<scores.length;i++){
for(intj=0;j<3;j++){
sum[i]+=scores[i][j];
avgs[j]+=scores[i][j];
}
}
System.out.println("學霸總分:"+sum[0]);
System.out.println("學渣總分:"+sum[1]);
System.out.print("平均分依次為:");
for(floatavg:avgs){
System.out.print(avg/2+",");
}
}
㈦ java二維數組先輸入學生姓名成績 然後再輸入學生姓名提取出學生成績
二維數組可以做,你需要去遍歷,太麻煩了,
用Map 但是用map有個問題,如果是姓名重復是不能存儲的(Key不能重復),所以應該用學生號來取姓名跟成績。
㈧ java二維數組輸入學生姓名輸出學生成績
二維數組可以做,你需要去遍歷,太麻煩了,
用map
但是用map有個問題,如果是姓名重復是不能存儲的(key不能重復),所以應該用學生號來取姓名跟成績。
㈨ 怎樣利用Java二維數組製作一個25名同學4個學科的成績表
double[][] score = new double[4][25];
for(int i=0;i<4;i++) {
for(int j=0;j<25;j++) {
score [i][j] = 100;
System.out.print("第"+j+"個學生的第"+i+"個學科的成績是"+score [i][j]);
}
}
大概就是這個意思,你可版以再加個存學生名權字的String數組,和存學科名字的String數組;
例如String[] studentName = new String[25];
studentName[0] = "XiaoMing";
......
String[] subjectName = new String[4];
subjectName[0] = "English";
......
System.out.print("第"+j+"個學生的第"+i+"個學科的成績是"+score [i][j]);就可以改成
System.out.print(studentName[j]+「的"+subjectName[i]+"成績是"+score [i][j]);
㈩ java二維數組輸入學生姓名輸出學生成績
使用 java.util.Scanner scanner=new java.util.Scanner(System.in) 可以很方便接收輸入
~~~~~