First, you have an egg or an existing chicken.-Java programming (BASICS) Learning Diary

Source: Internet
Author: User

Tossing the code is really a very time-consuming thing. unconsciously, ten minutes, twenty minutes, thirty minutes...

After-school programming questions allow you to read an uncertain number of exam scores, and determine how many scores are higher than or equal to the average score, and how many scores are lower than the average score. Enter a negative number to indicate that the input is complete. Suppose the highest score is 100 ....

Do you have eggs first or chickens first? Let me answer from a biological point of view, but programming... Amount.

Initialize an array list first, and the length of the array is determined. Therefore, the number of uncertain test scores cannot be satisfied. However, if you want to input scores first and then initialize the array, the first score is not saved. Therefore, the younger brother's countermeasure is to first define and initialize a long array with a length of N, then, initialize another array list2 based on the number of input scores to convert the valid input to the new array. Then, operate the new array.

PS: the front side, the N, can be very large, no matter how you write it, it is a definite number, rather than an infinite number, it seems that there is no limit to the length of this array?

This is too limited. As long as it is not infinite, there is always a risk of overflow when you define the n size ..

PS: If you define a large array and manually enter those scores, you may not be able to write the overflow when your hand cramps occur. However, here we discuss the theory and theoretically overflow.

I don't know if there is any better way. I think so much for the moment.

I am a newbie who reads the sixth chapter and does not have full exercise questions. Do not despise it ......

Package java06.programtest;
// Class Question 4: An array with an uncertain number of inputs
Import javax. Swing. joptionpane;

Public class program_6_4 {
Public static void main (string [] ARGs ){
Int n = 1000;
// Awkwardly, I want to assign a value to index by calculating the total number of input scores, which is used to initialize the array, but no input scores are saved without initializing the array.
Int [] mylist = new int [N]; // currently, I use a manual value assignment to initialize the Array

Int number = 0;
Int I = 0;
For (I = 0; number> = 0; I ++ ){
String inputstring = joptionpane. showinputdialog ("enter a score./N" +
"Negative number to end ~!!! /Nthe allowable Max score is 100 .");
Number = integer. parseint (inputstring );
If (number> = 0)
Mylist [I] = number;

}
I --;
System. Out. println ("score's amount is" + I );

// It is risky to directly use the original array. It is good that the input quantity is equal to the defined array,
// If n is not input enough, the 0 values will calculate the average value. Therefore, we need to block all the default 0 values that are not input.
// The method we use here is to assign the value of the front edge array to the new one.
Int [] mylist2 = new int [I];
Int Index = I-1;
For (int K = 0; k <= index; k ++ ){
Mylist2 [k] = mylist [k];
}
System. Out. println ("/ntest: Average" + computeaverage (mylist) + "average of one thousand ");
System. Out. println ("/naverage" + computeaverage (mylist2 ));
System. Out. println ("/ntest: greater than or equal to the average value:" +
Computecountaboveaverage (computeaverage (mylist), mylist) + "one thousand of which is greater than or equal to the average ");
System. Out. println ("/N is greater than or equal to the average value:" + computecountaboveaverage (computeaverage (mylist2), mylist2 ));
System. Out. println ("/ntest: less than the average value:" +
Computecountbelowaverage (computeaverage (mylist), mylist) + "One thousand smaller than average ");
System. Out. println ("/n less than average:" + computecountbelowaverage (computeaverage (mylist2), mylist2 ));
 
}
// Calculate the average value
Public static double computeaverage (INT [] list ){
Int sum = 0;
Double average = 0;
For (INT I = 0; I <list. length; I ++ ){
Sum + = list [I];
}
// Average = sum/list. length; // the bug is marked first. Pay attention later.
Average = (double) sum/list. length;
Return average;

}
// Calculate the average score or above
Public static int computecountaboveaverage (double average, int [] list ){
Int COUNT = 0;
For (INT I = 0; I <list. length; I ++ ){
If (list [I]> = average)
Count ++;
}
Return count;
}
// Calculate the number of scores below the average score
Public static int computecountbelowaverage (double average, int [] list ){
Int COUNT = 0;
For (INT I = 0; I <list. length; I ++ ){
If (list [I] <average)
Count ++;
}
Return count;
}
 

}

Postscript. Class instance variables and static variables are calledClass variableOrData domain.Variables defined in the method are called local variables. No matter where it is declared, the scope of the class variable is the entire class. Class variables and methods can be declared in any order in the class.

Exception: one data field needs to reference another data field for initialization, And the other data field must be declared first (strict sequence restrictions are specified in the book ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.