Using Java to understand program logic (9)

Source: Internet
Author: User

public class One

{
public static void main (string[] Args)

{
8 15 27 39 44 56
33
Scanner input = new Scanner (system.in);
int[] one = {8, 15, 27, 39, 44, 56};
System.out.print ("the original character sequence is:");
for (int i = 0; i < one.length; i++)

{
System.out.print (one[i] + "");
}
System.out.print ("\ N Please enter a number:");
int a = Input.nextint ();//33
int index = one.length;//3//saves The index of a save LOCATION//3
for (int i = 0; i < one.length; i++)

{
If (one[i] >= a)//39 >= 33

{
index = i;//find where a is stored
Break;//jump out of the loop
}
}
One:8 15 27 39 44 56
Two:8 15 27 39 44 56 88
The new array, where the array is inserted into the completed sequence
int[] "the New Int[one.length + 1];
for (int i = 0; i < two.length; i++)

{
If (i < INDEX)//6 < 6

{
two[i] = one[i];
}

else if (i = = INDEX)//6 = = 6

{
two[i] = a;//88
}

ELSE//I>INDEX//6 > 3

{
two[i] = one[i-1];
}
}
System.out.print ("after Insert completes array:");
for (int i = 0; i < two.length; i++)

{
System.out.print (two[i] + "");
}
}
}

I++: first use and then accumulate

++i: add first and then use

int i=10;
System.out.println (I++);//10
System.out.println (++I);//12
System.out.println (I--);//12
System.out.println (I.);//10
int A=++i + I--;//11 + 11
System.out.println (A);//22
System.out.println (I);//10

Multiplication tables:

public class Three

{
public static void main (string[] Args)

{
For (int I=1;i<=9;i++)//outer Loop Line

{
For (int j=1;j<=i;j++)//inner Loop column

{
System.out.print (j+ "*" +i+ "=" + (i*j) + "\ t");
}
System.out.println ();//line Break
}
}
}
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
....
1*9=9 2*9=18 ..... ..... 9*9=81

Right triangle

public class

{
public static void main (string[] Args)

{
Scanner input=new Scanner (system.in);
System.out.print ("please Enter the number of lines:");
int Row=input.nextint ();
For (int I=1;i<=row;i++)//outer Loop Line i=3

{
For (int j=1;j<=2*i-1;j++)//memory Loop column j=3

{
System.out.print ("*");
}
System.out.println ();//line Break
}
Inverted Right angle
For (int I=row;i>=1;i--)//outer Loop Line i=3

{
For (int j=1;j<=2*i-1;j++)//memory Loop column j=3

{
System.out.print ("*");
}
System.out.println ();//line Break
}
}
}

Isosceles Triangle

public class Three

{
public static void main (string[] Args)

{
Isosceles Triangle
5
For (int I=5;i>=1;i--)//line

{
For (int k=1;k<=5-i;k++)//space per line

{
System.out.print ("");
}
For (int J=1;j<=2*i-1;j++)//column

{
If (j==1 | | j==2*i-1 | | i==5)

{
System.out.print ("*");
}

else//printing spaces

{
System.out.print ("");
}
}
System.out.println ();//line Break
}
For (int I=2;i<=5;i++)//line

{
For (int k=1;k<=5-i;k++)//space per line

{
System.out.print ("");
}
For (int J=1;j<=2*i-1;j++)//column

{
If (j==1 | | j==2*i-1 | | i==5)

{
System.out.print ("*");
}

else//printing spaces

{
System.out.print ("");
}
}
System.out.println ();//line Break
}
}
}
*
***
*****
*******
*********

int A=10;//int32//common
Long B=10;//int64
Double d=8.88d;//64 dual-precision floating-point Type//common
Float E=7.77F;//32 single-precision Floating-point type

Simple Color Ball:

public class Four

{
public static void main (string[] Args)

{

Top 6 digits: 1-33 random number (cannot be duplicated)
Last: 1-16 Random Numbers
8 22 33 16 17 18+15
int[] one=new int[7];//store 7 digit color Ball number
4 33 17 10 5 31 0
for (int i = 0; i < one.length-1; i++)//first 6 bits 1-33 random number i=5

{
While (true)

{
int rand= (int) (math.random () *33+1);//1-33 random number
The Boolean ok=false;//default means Rand does not repeat in front of 6 bits
for (int j = 0; J < one.length-1; J + +)

{
If (one[j]==rand)//repeat

{
Ok=true;//repeated.
Break
}
}
If (!ok)//do not repeat

{
one[i]=rand;
Break;//jumps out of the whole while
}
}
}
one[one.length-1]= (int) (math.random () *16+1);//1-16 random number
System.out.println ("color Ball as follows:");
for (int i = 0; i < one.length; i++)

{
System.out.print (one[i]+ "\ t");
}
}
}

Hourglass (solid) (inverted triangle, Positive triangle):

public class Shalou

{
public static void main (string[] Args)

{
int i = 9;//9 line
For (int y = 0; y < (i + 1)/2; Y++)

{
For (int x = 0; x < i; + +)
If (x >= y && x <= i-y-1)
System.out.print ("*");
Else
System.out.print ("");
System.out.print ("\ n");
}
For (int y = (i + 1)/2; y < i; y++)

{
For (int x = 0; x < i; + +)
If (x <= y && x >= i-y-1)
System.out.print ("*");
Else
System.out.print ("");
System.out.print ("\ n");
}
}
}

*********
*******
*****
***
*
***
*****
*******
*********

Hundred Money to buy hundred chickens:

public class One

{
public static void main (string[] Args)

{
for (int i = 0; I <=; i++)

{
for (int j = 0; J <=, J + +)

{
For (int k = 0; k <=; k++)

{
If (i + 9 * j + k = = && i + j + k = = 100)

{
System.out.println ("rooster" + i + "hen" + j + "chick" + k);
}
}
}
}
}
}

Inverted triangle:

public class Daosanjiao

{
public static void main (string[] Args)

{
int hang = 3;
for (int i = 0; i < hang; i++)

{
For (int a = 0; a < i; A++)

{
System.out.print ("");
}
for (int B = 1; b <= 2 * hang-2 * i-1; B++)

{
System.out.print ("*");
}
System.out.print ("\ n");
}
}
}

Using Java to understand program logic (9)

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.