当前位置:网站首页>For loop averaging
For loop averaging
2022-06-25 21:21:00 【vancomycin】
This cycle adds some conditions , Enter a student's N Our achievements , And finally print out the student's total score and average score .
1. thought : use for A nested loop , First of all, the students' scores are uncertain , So first use for Loop in a i Define the number of subjects for the grade .
2. It should be noted that , Looking for the total score sum Or GPA avg When , Be sure to define two variables to store the total score or average value , You can also write in the loop , But local variables and member variables are different ( This variable , Try to define it as a member variable , Prevent errors in subsequent operations .)
3. The result is for A cumulative process of a cycle , The average is the total score divided by the number of iterations , That is to say i.
See the following for the detailed code :
package com.qfedu.test1;
import java.util.Scanner;
public class Test2 {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println(" Please enter the student's name :");
String name =input.next();
int i= 1;
int avg = 0; // Define an average
int sum=0; // Define a total
for (i = 1; i <=5; i++) {
System.out.println(" Please enter the first "+i+" Grades in the door grades :");
int score = input.nextInt();
sum += score; // Will result Sum is assigned to the total number
avg =sum/i; // The average is the total / frequency
}
System.out.println(name+" Hello! , Your average score is :"+avg);
}
}边栏推荐
- OLED driven learning based on ssd1306 (II): addressing mode of ssd1306
- Summary of several methods for FPS calculation
- [machine learning] machine learning from zero to mastery -- teaching you how to use decision tree to predict contact lens types
- The difference between strcpy and memcpy
- JS__ Inheritance mode, namespace, object enumeration__ Duyi
- Is it legal to open an account for flush stock trading software? Is it safe?
- Data query of server SQL. The most important chapter in database learning
- 股市小白通过网上进行股票开户安全吗?
- couldn‘t be accessed by user ‘_ apt‘
- Idea implements hot deployment
猜你喜欢

QT method of exiting application (exe)

04 disk space management

The beginning of manjaro's journey

Renren mall locates the file according to the route

A simple file searcher

Insert picture in markdown

Volatile qualifier

js(3)

Command 'GCC' failed with exit status 1 when PIP install mysqlclient

IPv4 and IPv6 (powercert animated videos)
随机推荐
Order by keyword syntax problems encountered in SQL Server learning
OBD Position Overview
Molecular dynamics - basic characteristics of molecular force field
1.1-mq visual client preliminary practice
JS__ This, arguments, cloning, ternary operator__ Duyi
Write a "standard" macro min, which inputs two parameters and returns the smaller one
Yolov4 improved version comes out! Yolov4 extended edition! Yolov4 launched scaled-yolov4: scaling cross stage partial network
Using two stacks to realize the function of one queue?
VFS appears when mounting a file system from an SD card: cannot open root device "mmcblk1p2“
Cvpr2019 | the latest cvpr2019 papers are the first to read!
Desktop network error display red ×, Component failed to start
Soft test intermediate simple notes
What is API
Nine built-in objects of JSP and four scopes of Servlet
Alicloud disk mounted locally
股市小白通过网上进行股票开户安全吗?
Those high-frequency and real software test interview questions sorted out by the test director in 7 days, come to get
同花顺app是正规的吗?到底安不安全
How to write an infinite loop
银河证券靠谱吗?开证券账户安全吗?