当前位置:网站首页>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 .

边栏推荐
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
- [proteus simulation] 74hc595+74ls154 drive display 16x16 dot matrix
- PHP CI(CodeIgniter)log级别设置
- Hong Kong Polytechnic University | data efficient reinforcement learning and adaptive optimal perimeter control of network traffic dynamics
- 14 topics for performance interviews between superiors and subordinates (4)
- Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
- 一台服务器最大并发 tcp 连接数多少?65535?
- [proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display
- [combinatorics] non descending path problem (number of non descending paths with constraints)
- QT serial port UI design and solution to display Chinese garbled code
猜你喜欢

Colab works with Google cloud disk

Mysql 将逗号隔开的属性字段数据由列转行

NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线

Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)

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

(Supplement) double pointer topic

Multithread 02 thread join

消息队列消息丢失和消息重复发送的处理策略

A survey of state of the art on visual slam
![[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display](/img/46/c7f566f8fd46d383b055582d680bb7.png)
[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display
随机推荐
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)
ThreeJS 第二篇:顶点概念、几何体结构
TCP congestion control details | 3 design space
(Supplement) double pointer topic
Top k questions of interview
Mysql 将逗号隔开的属性字段数据由列转行
爱可可AI前沿推介(7.3)
Client does not support authentication protocol requested by server; consider upgrading MySQL client
Interviewer: how does the JVM allocate and recycle off heap memory
Record a jar package conflict resolution process
Uploads labs range (with source code analysis) (under update)
Record windows10 installation tensorflow-gpu2.4.0
斑马识别成狗,AI犯错的原因被斯坦福找到了
Is it safe to open an account with tongdaxin?
Golang decorator mode and its use in NSQ
于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
Expression of request header in different countries and languages
面试官:JVM如何分配和回收堆外内存
Characteristic polynomial and constant coefficient homogeneous linear recurrence
Develop team OKR in the way of "crowdfunding"