当前位置:网站首页>Daily code 300 lines learning notes day 10
Daily code 300 lines learning notes day 10
2022-07-03 16:35:00 【Leeyz_ one】
1. Comprehensive use
Students' grades are stored in a matrix , Where the line represents the student , List the accounts . Such as : The first 0 Line representation 0 A student's math 、 Chinese language and literature 、 English scores . requirement :
- Random generation of student scores , Interval is [50, 100].
- Find the best 、 The worst classmate . But students who fail in the course will not participate in the evaluation .
2. Generation of random numbers
java.util.Random In bag , It can be used int nextInt(int n) , Returns a pseudo-random number , It is taken from the random number generator sequence 、 stay 0( Include ) And the specified value ( barring ) Evenly distributed between int value .
Random number generation seems to have no range , If you want to generate a range of random numbers , The following format is often used :
x=Arrays.nextInt( Maximum - minimum value )+ minimum value ;
3. Code segment
package demo3;
import java.util.Arrays;
import java.util.Random;
public class Task1 {
public static void main(String[] args) {
int n = 10;// Number of students
int m = 4;// Number of accounts
int lowerBound = 40;// Set minimum score
int upperBound = 100;// Maximum score
int thresholed = 60;// Pass grade threshold
// Random
Random tempRandom = new Random();
int[][] data = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
data[i][j] = lowerBound + tempRandom.nextInt(upperBound - lowerBound);// Generate 50-65 Random numbers in the range
}
} // Random
System.out.println("The data is:\r\n" + Arrays.deepToString(data));
// Calculate the total score , And will 60 The total score of less than points is converted into 0
int[] totalScores = new int[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (data[i][j] < thresholed) {
totalScores[i] = 0;
break;
} // Of if
totalScores[i] += data[i][j];
} // Of for j
} // Of for i
System.out.println("\rThe total scores are:\r\n" + Arrays.toString(totalScores));
// Best and Worst
int tempBestIndex = 100;
int tempWorstIndex = 100;
int tempBestScore = 0;
int tempWorstScore = 100000;
for (int i = 0; i < n; i++) {
if (totalScores[i] == 0) {
continue;
} // of if
// Sort replace maximum , And reset i To indicate the best students
if (tempBestScore < totalScores[i]) {
tempBestScore = totalScores[i];
tempBestIndex = i;
} // of if
// Sort replace minimum , And reset i To indicate the worst students
if (tempWorstScore > totalScores[i]) {
tempWorstScore = totalScores[i];
tempWorstIndex = i;
} // of if
} // of for
// Final output
if (tempBestIndex == -1) {
System.out.println("Cannot find best student. All students have failed.");
} else {
System.out.println("\rThe best student is No." + tempBestIndex + " with scores: "
+ Arrays.toString(data[tempBestIndex]));
} // Of if
if (tempWorstIndex == -1) {
System.out.println("Cannot find worst student. All students have failed.");
} else {
System.out.println("The worst student is No." + tempWorstIndex + " with scores: "
+ Arrays.toString(data[tempWorstIndex]));
} // of else
}// of main
}// of Task1
The final code running result :

4. summary :
In this code , At first, random numbers are generated for In circulation , take i and j The initial values of are all set to 1, This leads to randomly generated scores , Everyone's first subject has become 0 了 . Output the best result at the end , In the code segment with the worst score , Didn't notice the parentheses , Throw it to the front for In circulation , This leads to the output of several results in the cycle .

边栏推荐
- [combinatorics] non descending path problem (outline of non descending path problem | basic model of non descending path problem | non descending path problem expansion model 1 non origin starting poi
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (I)
- 什么是质押池,如何进行质押呢?
- ThreeJS 第二篇:顶点概念、几何体结构
- 爱可可AI前沿推介(7.3)
- 斑马识别成狗,AI犯错的原因被斯坦福找到了
- arduino-esp32:LVGL项目(一)整体框架
- Unreal_DataTable 实现Id自增与设置RowName
- Myopia: take off or match glasses? These problems must be understood clearly first
- Svn usage specification
猜你喜欢

跟我学企业级flutter项目:简化框架demo参考

NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon

Stm32f103c8t6 firmware library lighting

One article takes you to understand machine learning

NLP四范式:范式一:非神经网络时代的完全监督学习(特征工程);范式二:基于神经网络的完全监督学习(架构工程);范式三:预训练,精调范式(目标工程);范式四:预训练,提示,预测范式(Prompt工程)

What is the maximum number of concurrent TCP connections for a server? 65535?

TCP擁塞控制詳解 | 3. 設計空間

MySQL converts comma separated attribute field data from column to row

The mixlab editing team is recruiting teammates~~

Processing strategy of message queue message loss and repeated message sending
随机推荐
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
[combinatorics] non descending path problem (outline of non descending path problem | basic model of non descending path problem | non descending path problem expansion model 1 non origin starting poi
Extraction of the same pointcut
切入点表达式
Explore Netease's large-scale automated testing solutions see here see here
Advanced Mathematics (Seventh Edition) Tongji University exercises 2-1 personal solutions
特征多项式与常系数齐次线性递推
在ntpdate同步时间的时候出现“the NTP socket is in use, exiting”
[Jianzhi offer] 57 - ii Continuous positive sequence with sum s
Mixlab编辑团队招募队友啦~~
高等数学(第七版)同济大学 习题2-1 个人解答
Remote file contains actual operation
手机注册股票开户安全吗 开户需要钱吗
Nifi from introduction to practice (nanny level tutorial) - flow
"The NTP socket is in use, exiting" appears when ntpdate synchronizes the time
爱可可AI前沿推介(7.3)
【LeetCode】94. Middle order traversal of binary tree
Multithread 02 thread join
Eleven requirements for test management post
Deep understanding of grouping sets statements in SQL