Java's two-cycle code sample

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.