20165235 Qi Ying Java Fourth Week exam summary

Source: Internet
Author: User
Tags array length

20165235 Qi Ying Java Fourth Week exam summary after class exercise P29 P45
  • Code writing
  •   Import java.util.*;class example2_5{public static void Main (String args[]) {int start = 0,end, middle; int a [] = {12,45,67,89,123,-45,67}; int N = A.length;     for (int i=0;i<n;i++) {for (int j=i+1;j<n;j++) {if (A[j] < a[i]) {int t = a[j];     A[J] = A[i];   A[i] = t; }}} Scanner Scanner = new Scanner (system.in); System.out.println ("Enter an integer, the program determines whether the number is in the array:"); int number = Scanner.nextint (); int count = 0; end = N; Middle= (Start+end)/2;    while (Number!=a[middle]) {if (Number>a[middle]) start = middle;    else if (number < A[middle]) end = middle;    Middle = (start+end)/2;    count++;   if (COUNT>N/2) break;    } if (COUNT>N/2) System.out.printf ("%d buzaishuzuzhong.\n", number);   Else System.out.printf ("%d zaishuzuzhong.\n", number); }}
    public class Example3_7{   public static void main (String args[]){ int sum =0,i,j; for(i=1;i<=10;i++){   if(i%2==0){   continue; } sum=sum+i;} System.out.println("sum="+sum);  for(j=2;j<=100;j++){ for(i=2;i<=j/2;i++){     if(j%i==0)       break; }   if(i>j/2){ System.out.println(""+j+"是素数"); }   } }}
  • In this exam because my upload failed, resulting in the failure to hand in the results of the experiment, may be the reason for using QQ. This code in the second postmenstrual has been knocked well, as follows
  • P29 Code Connection
  • P45 code connection.
  • Run as follows:
  • Recursion and looping

    This experiment allows us to achieve 1 by programming! +2! +3! +.....+n! The function. The code is as follows:

  public class a{public static void Main (String args[]) {int [] A = new int [args.length];      for (int i =0;i<args.length;i++) {a[i] = Integer.parseint (Args[i]);      } int sum = 0; if (args.length<1) {System.out.println ("wrong input!          ");        System.exit (0);        } for (int i =1;i<=a[0];i++) {sum + = fact (i); } if (sum<=0) {System.out.println ("wrong input!          ");        System.exit (0);  } System.out.println (sum);        } public static int fact (int n) {if (n = = 0) return 1;     else return n * fact (n-1); }}
    • This program uses recursion to implement the factorial and, where the a[i] = Integer.parseInt(args[i]) input data is stored in the array, in the recursive operation only call a[0] in the value to calculate, if the array length is less than 1 or enter a negative number by System.exit(0) exiting the program.
    • The following is the run:
    • The following is the code cloud Codes connection.

      Summary of exam Contents
    • Debug the code with JDB, jdb -classpath .:./bin A 3 and then set a breakpoint on the function, use it, next or use it to step run one step.
    • If you want to print the value of sum, you can use it print sum to observe.

      The 23rd chapter Code of the programming question
    • The 34th chapter is the programming code connection.
    • Code run result diagram:

    • -

20165235 Qi Ying Java Fourth Week exam summary

Related Article

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.