J2SE exercises (1) and j2se exercises

Source: Internet
Author: User

J2SE exercises (1) and j2se exercises

The following are some common java questions:

1.

Code:

Package cn.wwh.www.exe rcise; import java. util. role;/*** role: 1. output N * N multiplication table *** @ author yiye banzhou * @ version 1.0 * @ Creation Time: september 4, 2014 6:09:08 */public class PrintMultTable {public static void main (String [] args) {System. out. print ("Enter the number of rows:"); export scn = new partition (System. in); int num = scn. nextInt (); printNum (num);}/*** @ param inNum * the integer entered on the console **/private static void printNum (int inNum) {if (inNum <1) {System. out. println ("input error! "); Return;} System. out. println ("output"); for (int I = 1; I <= inNum; I ++) {for (int j = 1; j <= I; j ++) {System. out. print (j + "*" + I + "=" + (I * j) + "\ t");} System. out. println ();}}}

2.



Package cn.wwh.www.exe rcise; import java. util. role;/*** role: 2. based on the input start number and end number, output all the prime numbers in this range. *** @ author a leaf boat * @ version 1.0 * @ Creation Time: september 4, 2014 6:26:41 */public class PrintPrime {public static void main (String [] args) {System. out. print ("Enter start data:"); Country scn = new partition (System. in); int beginNum = scn. nextInt (); scn = new partition (System. in); System. out. print ("Enter end data:"); int endNum = scn. nextInt (); // Call the output function outPrime (beginNum, endNum );} /*** @ param beginNum * Start Number * @ param endNum * end number */private static void outPrime (int beginNum, int endNum) {if (beginNum> = endNum) {System. out. println ("incorrect input range! ");} Int count = 0; // used for wrapping int sum = 0; // sum of all prime numbers and for (int I = beginNum; I <endNum; I ++) {if (isPrime (I) {System. out. print (I + "\ t"); sum = I + sum; if (++ count % 8 = 0) System. out. println () ;}} System. out. println ("\ ncount:" + count + "\ tsum:" + sum );} /*** @ param num * integer to be judged * @ return returns true if I is a prime number; otherwise, false */private static boolean isPrime (int num) is returned) {boolean flag = false; if (num <= 1) {return flag;} if (num = 2) {return true ;}for (int I = 2; I <num; I ++) {if (num % I = 0) {return flag;} if (I * I> = num) {flag = true ;}} return flag ;}}


3.

Code:

Package cn.wwh.www.exe rcise; import java. util. role;/*** role: 3. output The number of daffodils * idea: for example, if you enter a three-digit number, the range of the three digits is [123,) * assume that the value is? * A> temp = 123 last = 123/1% 10 = 3 sum = 3*3*3 * B> temp = 12 last = 12% 10 = 2 sum = sum + 2*2*2 * c> temp = 1 last = 1% 10 = 1 sum = sum + 1*1*1 *** @ author * @ version 1.0 * @ creation time: september 4, 2014 8:48:25 */public class PrintNarcissusNum {public static void main (String [] args) {System. out. print ("Enter the number of digits:"); audio scn = new digit (System. in); int numLength = scn. nextInt (); System. out. println (numLength + "the number of bits in the number of BITs:"); // the minimum number of bits in the input int begin = (int) Math. pow (10, numLength-1); for (int I = begin; I <begin * 10; I ++) {if (isNarcissus (I, numLength) {System. out. print (I + "\ t") ;}}/ *** @ param I * @ return returns true if the number of daffodils is used, otherwise, false */private static boolean isNarcissus (int num, int length) {int temp = 0; // The sum of all numbers in the record int sum = 0; // records the numbers in each bit, and records int last = 0 from the right to the left; for (int I = 0; I <length; I ++) {int t = (int) Math. pow (10, I); temp = num/t; last = temp % 10; sum = (int) (sum + Math. pow (last, length);} return sum = num ;}}

4.


Note: The idea of the first method is to use an array to simulate a ring, but the biggest difficulty is the copying of numbers, that is, after the number is eliminated, the remaining number is made up of a ring.

The second method is to directly use a circular linked list, but the greatest difficulty is to format the output result according to the question.

Code:

Method 1:

Package cn.wwh.www.exe rcise. selectmonkey; import java. util. arrays; import java. util. role;/*** function: 5. monkey King election * this kind of practice has vulnerabilities and is not perfect (it is a mistake, please do not refer to it) * I think this idea is correct, but there are some problems with processing the branch, write down here, in the future, I will take a closer look at the ** @ author yiye banzhou * @ version 1.0 * @ creation time: september 5, 2014 12:32:15 */public class SelectKing1 {public static void main (String [] args) {while (true) {System. out. print ("Enter the number of monkeys m:"); then scn = new monkey (System. in); int Tal = scn. nextInt (); scn = new partition (System. in); System. out. print ("Enter the number n (n <m):"); int outNum = scn. nextInt (); int king = getKing (total, outNum); System. out. println ("Number" + king + "Monkey king! ");}} /*** @ Param total * total Number of monkeys * @ param outNum * number to be eliminated * @ return Monkey King Number */private static int getKing (int total, int outNum) {// The number returned by the record, int king = 0; int begin = total; int circle = 1; boolean flag = true; // initialize the array, the array length is total + 1, and a temporary array int [] monkey = new int [total + 1]; int [] temp = new int [total + 1]; // make the data in the array equal to the subscript for (int I = 0; I <total + 1; I ++) {monkey [I] = I ;}while (flag) {// There is still a monkey left. if (Total = 1) {king = monkey [1]; break;} // used for a flag, used to record the position of the maximum N multiple in an array int sign = 0; if (total! = 1) System. out. print ("th" + circle + ","); // set the array position of this n multiple to 0, use sign to record the largest n multiple subscript for (int I = 1; I <total + 1; I ++) {if (I % outNum = 0) {System. out. print ("no." + monkey [I] + "only monkey reported" + I + "quit! "); Monkey [I] = 0; sign = I ;}} int num = 1; if (total> outNum) {// place the sing + 1-total + 1 position for (int I = sign + 1; I <total + 1; I ++) {// if (I> = total + 1) break; if (monkey [I]! = 0) {temp [num ++] = monkey [I] ;}// copy data from a non-0 position to the temp array for (int I = 1; I <sign; I ++) {if (monkey [I]! = 0) {temp [num ++] = monkey [I]; // num-1 monkeys left} // copy the data in the temp array to the monkey array. // first, set all monkeys to 0for (int I = 1; I <total + 1; I ++) {monkey [I] = 0;} monkey = Arrays. copyOfRange (temp, 0, num); if (num-1 = 1 | num = 1) {king = monkey [1]; flag = false;} System. out. println ("currently exited" + (total + 1-num) + "only monkey. "); // The remaining number of monkeys total = num-1; // The number of returned circles (rounds) circle ++; if (total = outNum) {System. out. print ("second" + circle + "Wheel report,"); System. out. print ("no." + monkey [outNum] + "only monkey reported" + outNum + "quit! "); System. out. println (" one monkey is exited currently. "); Monkey [outNum] = 0; total = total-1; circle ++; continue ;}} else {// the maximum flag bit is less than the number of outNumwhile (true) {// int leftNum = total; int loop = 0; int tempNum = 0; for (int I = 1; I <begin * 2; I ++) {tempNum = total * I-outNum; loop = I; if (tempNum> 0) {break;} else if (tempNum = 0) {loop ++; break ;}} num = 1; for (int I = 0; I <loop; I ++) {for (int j = 1; j <total + 1; j ++) {temp [num ++] = monkey [j] ;}} if (te MpNum = 0) {System. out. print ("no." ++ circle + ",");} System. out. print ("no." + temp [outNum] + "only monkey reported" + outNum + "quit! "); System. out. println (" one monkey is exited currently. "); Temp [outNum] = 0; // System. out. println (Arrays. toString (temp); // first, set all monkeys to 0for (int I = 1; I <total + 1; I ++) {monkey [I] = 0 ;} if (tempNum = 0) {// System. out. println ("num:" + num); num = num-1; monkey = Arrays. copyOfRange (temp, outNum, num);} else {monkey = Arrays. copyOfRange (temp, outNum, num);} System. out. println ("num-1-outNum:" + (num-1-outNum); System. out. println ("total" + total); in T m = monkey. length; System. err. println (m); if (total-1! = 1 & total-1! = Num-1-outNum) {for (int I = 1; I <temp. length; I ++) {monkey [m ++] = temp [I]; System. out. println (Arrays. toString (monkey); if (temp [I] = temp [outNum]) {break ;}} total = total-1; // another monkey is left. if (total = 1) {king = monkey [1]; break ;}}} return king ;}}


Method 2:


Package cn.wwh.www.exe rcise. selectmonkey;/*** class: The Role of a monkey entity class *** @ author @ * @ version 1.0 * @ Creation Time: september 5, 2014 10:43:15 */public class Monkey {// Monkey no. public int no; // The current Monkey's next Monkey public Monkey nextMonkey = null; public Monkey (int no) {this. no = no ;}}

Package cn.wwh.www.exe rcise. selectmonkey;/*** class function: Build a monkey ring. *** @ author a boat * @ version 1.0 * @ Creation Time: september 5, 2014 10:46:46 */public class named elinked {// Number of monkeys in the circular circle private int num; // The first Monkey, that is, 1 private Monkey firstMonkey = null; private Monkey tempMonkey = null; // create a number of Monkey circles with num public incluelinked (int num) {this. num = num; for (int I = 0; I <num; I ++) {// if it is the first monkey if (I = 0) {// create a Monkey monkey = New Monkey (I + 1); // point to the first Monkey this. firstMonkey = monkey; this. tempMonkey = monkey;} else if (I = num-1) {// last Monkey // create a monkey Monkey monkey = new Monkey (I + 1); this. tempMonkey. nextMonkey = monkey; this. tempMonkey = monkey; // The Last monkey points to the first monkey this. tempMonkey. nextMonkey = this. firstMonkey;} else {// create a Monkey monkey Monkey = new Monkey (I + 1); this. tempMonkey. nextMonkey = monkey; this. tempMonkey = monkey ;}}}/** * @ Param outNum number to be eliminated **/public void specak (int outNum) {// number of remaining monkeys int last = this. num; // number of records in a circle int count = 0; // number of records int circle = 1; Monkey first = this. firstMonkey; Monkey pre = first; Monkey delete; Monkey prePre = first; System. out. print ("second" + circle + "Wheel report,"); while (last! = 1) {// under number n-1, locate the previous for (int I = 1; I <outNum-1; I ++) {while (prePre. nextMonkey. no! = Pre. no) {prePre = prePre. nextMonkey; if (prePre. nextMonkey. no = pre. no) break;} // System. out. println ("pre. nextMonkey. no "+ pre. nextMonkey. no); // System. out. println ("pre. no "+ pre. no); // System. out. println ("prePre. no "+ prePre. no); if (pre. nextMonkey. no> pre. no & count! = 0 & pre. no <prePre. no) | (pre. nextMonkey. no <pre. no & count! = 0 & pre. no> prePre. no) {System. out. println ("current exit" + count + "only"); // The unit is in a loop. The next monkey report number is greater than the current monkey report number. circle ++; System. out. print ("no." + circle + "report,"); count = 0;} // current = pre. nextMonkey; pre = pre. nextMonkey; // current = current. nextMonkey; while (prePre. nextMonkey. no! = Pre. no) {prePre = prePre. nextMonkey; if (prePre. nextMonkey. no = pre. no) break;} // System. out. println ("pre. nextMonkey. no "+ pre. nextMonkey. no); // System. out. println ("pre. no "+ pre. no); // System. out. println ("prePre. no "+ prePre. no); if (pre. nextMonkey. no> pre. no & count! = 0 & pre. no <prePre. no) | (pre. nextMonkey. no <pre. no & count! = 0 & pre. no> prePre. no) {System. out. println ("current exit" + count + "only"); // The unit is in a loop. The next monkey report number is greater than the current monkey report number. circle ++; System. out. print ("cir" + circle +", "); count = 0 ;}// delete the next node = pre. nextMonkey; pre. nextMonkey = delete. nextMonkey; pre = pre. nextMonkey; System. out. print ("th" + delete. no + "Monkey only reported" + outNum + "no. Quit! "); Count ++; while (prePre. nextMonkey. no! = Pre. no) {prePre = prePre. nextMonkey; if (prePre. nextMonkey. no = pre. no) break;} // System. out. println ("pre. nextMonkey. no "+ pre. nextMonkey. no); // System. out. println ("pre. no "+ pre. no); // System. out. println ("prePre. no "+ prePre. no); if (pre. nextMonkey. no> pre. no & count! = 0 & pre. no <prePre. no) | (pre. nextMonkey. no <pre. no & count! = 0 & pre. no> prePre. no) {System. out. println ("current exit" + count + "only"); // The unit is in a loop. The next monkey report number is greater than the current monkey report number. circle ++; System. out. print ("cir" + circle +", "); count = 0;} last --; if (last = 1) {System. out. println ("currently exited" + count + "only. ") ;}} System. out. println (" no "+ pre. no +" the monkey is the Monkey King! ");} Public static void main (String [] args) {// using elinked test = new using elinked (7); // test. specak (3); // Monkey first = test. getFirstMonkey (); // Monkey temp = first; // test whether the Monkey circle is created correctly. // do {// System. out. println ("monkey report:" + temp. no); // temp = temp. nextMonkey; //} while (temp! = First );}}


Package cn.wwh.www.exe rcise. selectmonkey; import java. util. role;/*** role: 2. using a circular linked list to deal with the Monkey King's question * the greatest difficulty is to format the output result (this is the result of analysis + Continuous testing) ** @ author yiye Binzhou * @ version 1.0 * @ Creation Time: 10:42:38, January 1, September 5, 2014 */public class SelectKing2 {public static void main (String [] args) {while (true) {System. out. print ("Enter the number of monkeys m:"); then scn = new monkey (System. in); int total = scn. nextInt (); scn = new partition (System. in); System. out. print ("Enter the number n (n <m):"); int outNum = scn. nextInt (); // create a total length monkey circle named elinked test = new incluelinked (total); // remove monkey, test. specak (outNum );}}}




Teach a J2SE question

Easy to input
After the input,
If the array is array [] [],
Int s = 0; // sum of the lower right slash
For (int I = 0; I <array. length; I ++ ){
For (int j = 0; j <array [I]. length; j ++ ){
S + = array [I] [j];
}
}
Int h = 0; // sum of the lower left slash
For (int I = 0; I <array. length; I ++ ){
For (int j = array [I]. length-1; j> = 0; j --){
H + = array [I] [j];
}
}

Then s + h is equal to the sum of two diagonal lines.

JAVA question j2se

I tried to answer a few questions. Please correct them later.
Program question: the s string should be read from the main program string array as "java". The loop is to count the number of uppercase and lowercase letters, and the result should be output:

Judgment: 8. Error. The constructor can have multiple
9. Correct. You can use new to declare an Instance Object of the class (the class cannot be an abstract class)
10. Correct
11. Error. double by default
12. Error. JAVA is an object-oriented language.
13. Error. "+" can be spliced, but no "-"
14. Error, // comment for a single line
15. Correct

Fill in the blanks:
The value of (x ++)/3 is (0) after the plus sign of x ++. Then, X is first involved in the Integer Operation, and then automatically added.
X = (X ++)/3 has never encountered this problem, but after analyzing (x ++)/3 to get the integer and then X auto-increment, X is 3 at this time, then, the = value assignment operation assigns the result (x ++)/3 to X, and the final value of X is 0;

Operation:
Since all conditions are given, use
(1) String [] str = {"open", "door", "the", "open", "name"}; // declare the initialization Array
(2) for (int I = 0; I <str. length; I ++) // cyclically counts the number of "open" strings
(3) if ("open". equals (str [I]) // if str [I] is "open", add count to 1
Otherwise, please point out
 

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.