Java Programming Ideas Fourth Edition fourth chapter personal practice

Source: Internet
Author: User
Tags bitwise operators

Fourth. Exercise 1: (1) write a program that prints 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 that generates 25 random numbers of type int, and for each random number, use the If-else statement to classify it as greater than, less than, or equal to a randomly generated value 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+ "equals" +n2);}}


Exercise 3: (1) Modify Exercise 2 to include the code in a while infinite loop. Then run it until you break it with 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.next Int (); if (flag) {Num[i]=n;compare (num[0],num[1]);} Else{flag=true;num[i]=n;} i=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+ "equals" +n2);}}


Exercise 4: (3) write a program that uses two nested for loops and the remainder operator (%) to detect and print a prime number (an integer that can be divisible only by itself and 1 and not 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 Prime"); return false;}



Exercise 5: (4) Repeat Exercise 10 in chapter 3rd, instead of using the integer.tobinarystring () method, use the ternary and bitwise operators to display the binary 1 and 0.

Java Programming Ideas Fourth Edition fourth chapter personal practice

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.