当前位置:网站首页>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 .
边栏推荐
- Golang decorator mode and its use in NSQ
- [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)
- Zebras are recognized as dogs, and Stanford found the reason why AI made mistakes
- Golang 装饰器模式以及在NSQ中的使用
- (Supplement) double pointer topic
- Mixlab编辑团队招募队友啦~~
- 【剑指 Offer 】57 - II. 和为s的连续正数序列
- 相同切入点的抽取
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- Eleven requirements for test management post
猜你喜欢
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
Idea configuration plug-in
NLP四范式:范式一:非神经网络时代的完全监督学习(特征工程);范式二:基于神经网络的完全监督学习(架构工程);范式三:预训练,精调范式(目标工程);范式四:预训练,提示,预测范式(Prompt工程)
[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display
探索Cassandra的去中心化分布式架构
Cocos Creator 2.x 自动打包(构建 + 编译)
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
[combinatorics] non descending path problem (number of non descending paths with constraints)
[proteus simulation] 74hc595+74ls154 drive display 16x16 dot matrix
TCP拥塞控制详解 | 3. 设计空间
随机推荐
First knowledge of database
NLP四范式:范式一:非神经网络时代的完全监督学习(特征工程);范式二:基于神经网络的完全监督学习(架构工程);范式三:预训练,精调范式(目标工程);范式四:预训练,提示,预测范式(Prompt工程)
中南大学|通过探索理解: 发现具有深度强化学习的可解释特征
程序猿如何快速成长
切入点表达式
于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
[combinatorics] non descending path problem (number of non descending paths with constraints)
Aike AI frontier promotion (7.3)
斑马识别成狗,AI犯错的原因被斯坦福找到了
【剑指 Offer 】64. 求1+2+…+n
Golang anonymous function use
Unity project optimization case 1
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)
How programming apes grow rapidly
NSQ source code installation and operation process
斑馬識別成狗,AI犯錯的原因被斯坦福找到了
Thread pool executes scheduled tasks
Colab works with Google cloud disk
Acwing game 58
Using optimistic lock and pessimistic lock in MySQL to realize distributed lock