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

边栏推荐
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- TCP congestion control details | 3 design space
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)
- Characteristic polynomial and constant coefficient homogeneous linear recurrence
- Alibaba P8 painstakingly sorted it out. Summary of APP UI automated testing ideas. Check it out
- Aike AI frontier promotion (7.3)
- Nifi from introduction to practice (nanny level tutorial) - flow
- Client does not support authentication protocol requested by server; consider upgrading MySQL client
- NLP四范式:范式一:非神经网络时代的完全监督学习(特征工程);范式二:基于神经网络的完全监督学习(架构工程);范式三:预训练,精调范式(目标工程);范式四:预训练,提示,预测范式(Prompt工程)
- "The NTP socket is in use, exiting" appears when ntpdate synchronizes the time
猜你喜欢

Remote file contains actual operation

爱可可AI前沿推介(7.3)

Netease UI automation test exploration: airtest+poco

(补)双指针专题

TCP擁塞控制詳解 | 3. 設計空間
![[statement] about searching sogk1997 and finding many web crawler results](/img/1a/8ed3ca0030ea227adcd95e8b306aca.png)
[statement] about searching sogk1997 and finding many web crawler results

8个酷炫可视化图表,快速写出老板爱看的可视化分析报告

Cocos Creator 2. X automatic packaging (build + compile)
![[solved] access denied for user 'root' @ 'localhost' (using password: yes)](/img/71/1ff8ed1d773da99054310f96dca3f8.jpg)
[solved] access denied for user 'root' @ 'localhost' (using password: yes)

Record a jar package conflict resolution process
随机推荐
Thinking about telecommuting under the background of normalization of epidemic | community essay solicitation
AcWing 第58 场周赛
How to set up SVN server on this machine
拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
Construction practice camp - graduation summary of phase 6
word 退格键删除不了选中文本,只能按delete
【剑指 Offer】58 - I. 翻转单词顺序
Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
[combinatorics] summary of combinatorial identities (eleven combinatorial identities | proof methods of combinatorial identities | summation methods)*
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)
Cocos Creator 2.x 自动打包(构建 + 编译)
NLP四范式:范式一:非神经网络时代的完全监督学习(特征工程);范式二:基于神经网络的完全监督学习(架构工程);范式三:预训练,精调范式(目标工程);范式四:预训练,提示,预测范式(Prompt工程)
LeetCode1491. Average value of wages after removing the minimum wage and the maximum wage
Data driving of appium framework for mobile terminal automated testing
Myopia: take off or match glasses? These problems must be understood clearly first
[Jianzhi offer] 58 - ii Rotate string left
First knowledge of database
Mongodb installation and basic operation
【LeetCode】94. Middle order traversal of binary tree
如何在本机搭建SVN服务器