Java programming ideology version 4 Chapter 4 Personal exercises and programming ideology version 4

Source: Internet
Author: User
Tags bitwise operators

Java programming ideology version 4 Chapter 4 Personal exercises and programming ideology version 4
Chapter 4 Exercise 1: (1) write a program and print the values from 1 to 100

public static void main(String[] args) {test(100);}public static void test(int num){int i=0;while(i<num){System.out.print(++i);if(i!=100){System.out.print("、");}}System.out.println();int j=0;do{System.out.print(++j);if(j!=100){System.out.print("、");}}while(j<100);System.out.println();for(int k=0;k<100;){System.out.print(++k);if(k!=100){System.out.print("、");}}}


Exercise 2: (2) write a program to generate a random number of 25 int types. For each random number, use the if-else statement to classify it as greater than or less, or is equal to the value randomly generated immediately following it.
Public static void main (String [] args) {test ();} public static void test () {int num [] = new int [25]; for (int I = 0; I <25; I ++) {Random r = new Random (); int n = r. nextInt (); num [I] = n; if (I> 0) {compare (num [I-1], num [I]) ;}} public static void compare (int n1, int n2) {// System. out. println (n1 + "," + n2 + "\ n n1> n2:" + (n1> n2) + "\ t" + "n1 = n2: "+ (n1 = n2) +" \ t "+" n1 <n2: "+ (n1 <n2); if (n1> n2) {System. out. println (n1 + "greater than" + n2);} else if (n1 <n2) {System. out. println (n1 + "less than" + n2);} else {System. out. println (n1 + "equal to" + n2 );}}


Exercise 3: (1) Modify Exercise 2 and include the code in a while infinite loop. Run it until it is interrupted on the keyboard (usually by pressing Ctrl + c ).
Public static void main (String [] args) {test ();} public static void test () {int num [] = new int [2]; int I = 0; boolean flag = false; while (true) {Random r = new Random (); int n = r. nextInt (); if (flag) {num [I] = n; compare (num [0], num [1]);} else {flag = true; num [I] = n;} I = 0? 1:0 ;}} public static void compare (int n1, int n2) {// System. out. println (n1 + "," + n2 + "\ n n1> n2:" + (n1> n2) + "\ t" + "n1 = n2: "+ (n1 = n2) +" \ t "+" n1 <n2: "+ (n1 <n2); if (n1> n2) {System. out. println (n1 + "greater than" + n2);} else if (n1 <n2) {System. out. println (n1 + "less than" + n2);} else {System. out. println (n1 + "equal to" + n2 );}}


Exercise 4: (3) write a program and use two nested for loops and the remainder operator (%) to detect and print prime numbers (only divisible by itself and 1, the integer that cannot be divisible by other numbers ).
Public static void main (String [] args) {test (33);} public static boolean test (int num) {for (int I = 2; I <num; I ++) {if (num % I = 0) {System. out. println (num + "not a prime number"); return true ;}} System. out. println (num + "is a prime number"); return false ;}



Exercise 5: (4) repeat exercise 10 in chapter 1. Do not use the Integer. toBinaryString () method. Instead, use the ternary and bitwise operators to display the values of binary 1 and 0.

Answer: JAVA programming ideology version 4th chapter 4 exercise 5

I am also very interested in this question. I hope you can share it later.
 
Who has the answer to the fourth edition of java programming ideas?

No one answered the question. Join the QQ group about JAVA or join the group about this book directly. They have answers. I am also a fan. I suggest using this!

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.