当前位置:网站首页>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] combinatorial identities (review of eight combinatorial identities | product of combinatorial identities 1 | proof | use scenario | general method for finding combinatorial numbers)
- 探索Cassandra的去中心化分布式架构
- Unreal_DataTable 实现Id自增与设置RowName
- [combinatorics] combinatorial identity (sum of combinatorial identity products 1 | sum of products 1 proof | sum of combinatorial identity products 2 | sum of products 2 proof)
- Add color to the interface automation test framework and realize the enterprise wechat test report
- PHP中register_globals参数设置
- How programming apes grow rapidly
- There are several APIs of airtest and poco that are easy to use wrong in "super". See if you have encountered them
- Chinese translation of Tagore's floating birds (1~10)
- 利用MySQL中的乐观锁和悲观锁实现分布式锁
猜你喜欢
NSQ source code installation and operation process
8个酷炫可视化图表,快速写出老板爱看的可视化分析报告
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
Multithread 02 thread join
[combinatorics] non descending path problem (number of non descending paths with constraints)
[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display
Threejs Part 2: vertex concept, geometry structure
There are several APIs of airtest and poco that are easy to use wrong in "super". See if you have encountered them
QT serial port UI design and solution to display Chinese garbled code
为抵制 7-Zip,列出 “三宗罪” ?网友:“第3个才是重点吧?”
随机推荐
Chinese translation of Tagore's floating birds (1~10)
[statement] about searching sogk1997 and finding many web crawler results
于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
Why does the std:: string operation perform poorly- Why do std::string operations perform poorly?
AcWing 第58 场周赛
Acwing game 58
面试官:JVM如何分配和回收堆外内存
TCP congestion control details | 3 design space
[Jianzhi offer] 57 - ii Continuous positive sequence with sum s
Golang decorator mode and its use in NSQ
How to initialize views when loading through storyboards- How is view initialized when loaded via a storyboard?
Develop team OKR in the way of "crowdfunding"
(Supplement) double pointer topic
Thinking about telecommuting under the background of normalization of epidemic | community essay solicitation
Zebras are recognized as dogs, and Stanford found the reason why AI made mistakes
一台服务器最大并发 tcp 连接数多少?65535?
Client does not support authentication protocol requested by server; consider upgrading MySQL client
Interviewer: how does the JVM allocate and recycle off heap memory
在ntpdate同步时间的时候出现“the NTP socket is in use, exiting”
Custom plug-in construction and use of QT plug-in