1. Calculate the average of three classes
Import Java.util.Scanner;
public class avgscore{
public static void Main (sting[] args) {
Int[] score = new int[]; Result array
int classnum = 3; Number of Classes
Double sun = 0.0; Total Score
double[] Average = new double[classnum];//average score array
Cycle Input Student Scores
Scanner input = new Scanner (system.in);
for (int i =0; i<classnum; i++) {//Outer loop control class
sum = 0.0//sum of scores zeroed
System.out.println ("Please enter" + (i+1) + "Student's score:");
for (int j=0; j<score.length; J + +) {//Inner loop control number of participants per class
System.out.print ("the" + (j+1) + "Student's score:");
SCORE[J] = Input.nextint ();
sum = sum+score[j];//score Accumulation
}
Average[i] = sum/score.length;//average divide calculation
System.out.println ("the" + (i+1) + "class participants ' average score is:" +average[i]+ "\ n");
}
}
}
2. Print a rectangular code example
public class rtriangle{
Public satic void Main (string[] args) {
System.out.println ("Print rectangle");
for (int i = 1; i<= 5; i++) {//Print line I
for (Int J =1; j<=5;j++) {//print five * numbers
System.out.print ("*");
}
System.out.println ("\ n"); Line break
}
}
}
3. Bubble sort
Package Cn.jbit.ifdemo;
Import Java.util.Scanner;
/*
* Bubble sort 5 students score
**/
public class sortscore{
public static void Main (string[] args) {
Int[] Scores=new int[5];
Scanner input=new Scanner (system.in)
System.out.println ("Please enter the results of 5 students:");
Get student Results
for (int i = 0; i<scores.length;i++) {
System.out.pint ("Please enter" + (i+1) + "Student Score:");
Scores[i]=input.nextint ();
}
Bubble Sort Student Score
for (int i=0; i<scores.length-1; i++) {
for (int j=0; j<score.length-1-i; J + +) {
if (Scores[j]<scores[j+1]) {
int TEMP=SCORES[J];
SCORES[J]=SCORES[J+1];
Scores[j+i] =temp;
}
}
}
System.out.println ("Post-sorting results:");
for (int score:scores) {
System.out.print (score+ "\ t");
}
}
}
Java's two-cycle code sample