――Thank you for opening this article. ――I'm not good at teaching, so I managed to put it together, so please take a look. ――Please write more and more if you have any requests or suggestions. ――Then, please keep in touch with us for a long time.
java=Question_11.java
package Question_04;
public class Question_11 {
public static void main(String[] args) {
int[] english = {98,80,78,85,65,86,90,94,70,92};
int high = english[0];
int low = english[0];
int avg = 0;
for(int i = 0;i < english.length;i++) {
if(high <= english[i]) {
high = english[i];
}else if(low >= english[i]){
low = english[i];
}
avg+=english[i];
}
avg/=english.length;
System.out.println("Average score:"+avg+"point");
System.out.println("highscore:"+high+"point");
System.out.println("Lowest point:"+low+"point");
}
}
――The above program has the following method structure of one main method. --In this program, all the actions of ** "variable declaration" **, ** "calculation part" **, ** "result output" ** are written in the main method.
――Once you have a vague understanding of "whether to divide methods because it is difficult to increase the number of lines", I will explain how to divide them. ――First of all, the programs can be roughly divided as follows.
--In the variable declaration part, the value is stored in the variable to be used.
java=Question_11.java
int[] english = {98,80,78,85,65,86,90,94,70,92};//Store points for 10 people in english
int high = english[0];//Declare variables to compare highest points
int low = english[0];//Declare a variable to compare the lowest points
int avg = 0;//Declare a variable to calculate the average score
--In the calculation part, the points are compared and the highest, lowest, and average points are calculated.
java=Question_11.java
for(int i = 0;i < english.length;i++) {//Loop 10 times (for the number of people) to compare the scores for 10 people
if(high <= english[i]) {//Select the highest point
high = english[i];
}else if(low >= english[i]){//Select the lowest point
low = english[i];
}
avg+=english[i];//Store the total score to calculate the average score
}
--In the result output part, the operation of "Please display" is performed as if it were a problem statement.
java=Question_11.java
System.out.println("Average score:"+avg+"point");
System.out.println("highscore:"+high+"point");
System.out.println("Lowest point:"+low+"point");
java=Question_11.java
package Question_05;
public class Test {
public static void main(String[] args) {
int[] english = {98,80,78,85,65,86,90,94,70,92};
int highscore = english[0];
int lowscore = english[0];
int avgscore = 0;
int highend = high(english,highscore);
int lowend = low(english,lowscore);
int avgend = avg(english,avgscore);
display(highend,lowend,avgend);
}
public static int high(int[] english,int highscore) {
for(int i = 0;i < english.length;i++) {
if(highscore <= english[i]) {
highscore = english[i];
}
}
return highscore;
}
public static int low(int[] english,int lowscore) {
for(int i = 0;i < english.length;i++) {
if(lowscore >= english[i]){
lowscore = english[i];
}
}
return lowscore;
}
public static int avg(int[] english,int avgscore) {
for(int i = 0;i < english.length;i++) {
avgscore+=english[i];
}
avgscore/=english.length;
return avgscore;
}
public static void display(int highend,int lowend,int avgend) {
System.out.println("Average score:"+avgend+"point");
System.out.println("highscore:"+highend+"point");
System.out.println("Lowest point:"+lowend+"point");
}
}
――It can be divided like this.
--Variable declarations made by separating methods can only be called within the method.
--Example: Even if you write the code ```int x = 0; `in the main method, or use it like
System.out.println (x);`
in the high method, x The value of can not be displayed.
--In other words, even if you declare a variable, you can use it only in the method.
--So, call the value using the argument and the return value.
--Variables such as english for storing points and high score for comparing points declared in the variable declaration (main method) are declared for use in the calculation part. --So, refer to these variables as arguments. Then, the calculation result is used as the return value and returned to the variable declaration. By doing this, the calculation result can be handled by the main method. --Similarly, the calculation result is required to output the output result. --So, as an argument, refer to the calculation result from the variable declaration returned as the return value earlier. --The output result can be output by these operations.
――Thank you for your relationship. ――If you have any questions or requests, please write more and more.
Recommended Posts