Learn Java syntax from the topic

Source: Internet
Author: User
Tags pow

I. INPUT and OUTPUT

1. Enter the radius of the circle to calculate and output the circumference and area of the circle:

ImportJava.util.Scanner; Public classzuoye01_circle { Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); Instantiate input System.out.print ("Please enter the radius of the circle:"); DoubleR=sc.nextdouble (); Receives the input value and assigns a value to rDoubleZhouchang = 2*math.pi*0; DoubleMianji = Math.pi*math.pow (R, 2); System.out.println (The circumference of a circle with a radius of +r+ is: "+Zhouchang); System.out.println (The area of the circle with radius "+r+" is: "+Mianji); }}

Second, if statement

2, console input age, according to age output different tips

ImportJava.util.Scanner; Public classZuoye02_nianlingpanduan { Public Static voidMain (string[] args) { for(inti=0;i<1;i++) {Scanner sc=NewScanner (system.in); System.out.print ("Please enter your Age:"); intAge =Sc.nextint (); if(Age>0 && age<135){            if(age<18) {System.out.println ("You are underage!" "); }            Else if(age<60) {System.out.println ("You are an adult!" "); }            Else{System.out.println ("You are old!" "); }        }        Else{System.out.println ("You entered the wrong age!" You can only enter numbers from 1 to 135! "); I--; }        }    }}

Third, the circular statement

1, a piece of paper thickness of about 0.08mm, fold how many times can reach the height of Mount Everest (8848.13 meters)?

 Public class Zuoye03_zhedie {    publicstaticvoid  main (string[] args) {          Double Zhi = 0.08;          for (int i =1;i>0;i++) {            zhi*=2;             if (zhi>=8848130) {                System.out.println ("folded" +i+ "times, exceeding the height of Everest");                  Break ;            }        }    }}

2, calculate the factorial of 5 5! What's the result?

public class Zuoye04_jiecheng {
public static void Main (string[] args) {
int S=1;
for (int i=1;i<=5;i++) {
S*=i;
}
System.out.println (The factorial of "5" is: "+s");
}
}

3. Calculation 1+1/2!+1/3!+1/4!+ ... 1/20!=?

 Public classZuoye05_jiechengqiuhe { Public Static voidMain (string[] args) {DoubleSum=0;  for(inti=1;i<=20;i++){            DoubleS=1;  for(intj=1;j<=i;j++) {s*=J; } Sum+ = (1/s); } System.out.println ("The result is:" +sum); }}

4, print out all the "Narcissus number", so-called "Narcissus number" refers to a three-digit number, the number of its members of the cubic and equal to the number itself. For example: 153 is a "narcissus number", because the 153=1 three times the square +5 of the three +3 Times Square.

 Public classZuoye06_shuixianhuashu { Public Static voidMain (string[] args) { for(inti=100;i<=999;i++){            inta=i/100; intb= (i%100)/10; intC= (i%100)%10; if(Math.pow (a,3) +math.pow (b, 3) +math.pow (c,3) = =i)            {System.out.println (i); }        }    }}

6, the ancient Chinese mathematician Zhang Chujian in the "Calculation of the book" in a "hundred money to buy hundred chickens" problem, test instructions is this: 5 Money can buy a rooster, 3 money can buy a hen, 1 money can buy 3 chicks. Now buy 100 chickens with 100 cents, so how many cocks, hens and chicks are there? Please write a program implementation.

 Public classZuoye07_baiqianbaiji { Public Static voidMain (string[] args) {intA,b,c; intI=0;  for(a=0;a<=20;a++){             for(b=0;b<=33;b++){                 for(c=0;c<=100;c++){                    if(C%3==0 && (5*A+3*B+C/3) ==100 && (a+b+c) ==100) {i++; System.out.println ("+i+" type buy Method: "+a+" only Rooster, "+b+" only hen, "+c+" only chick. "); Continue; }                }            }        }    }}

7, this is the classic "Hundred Horse Hundred Bear" problem, there are 100 horses, carry 100, the big horse carries 3, the horse carries 2 to bear, two small horse carries 1, ask has big, medium, pony each several horses?

 Public classZuoye08_baimabaidan { Public Static voidMain (string[] args) {intA,b,c; intI=0;  for(a=0;a<=33;a++){             for(b=0;b<=50;b++){                 for(c=0;c<=100;c++){                    if((a+b+c) ==100 && c%2==0 && (3*a+2*b+c/2==100) ) {i++; System.out.println ("+i+" Camel Law: "+a+" only big horse, "+b+" only in the horse, "+c+" only pony. "); }                }            }        }    }}

8, console output 99 multiplication table

 Public class Zuoye09_chengfabiao {    publicstaticvoid  main (string[] args) {          for (int i=1;i<=9;i++) {            for (int j=1;j<=i;j++) {                System.out.print (J+ "x" +i+ "=" + (i*j) + "\ T");            }            System.out.println ();     }}}

9. Console output Triangles and diamonds

 Public class zuoye10_sanjiaoxing {    publicstaticvoid  main (string[] args) {          for (int i=1;i<=5;i++) {            for (int j=1;j<=i;j++) {                System.out.print ("★" + "");            }            System.out.println ();     }}}

 Public classzuoye11_lingxing { Public Static voidMain (string[] args) { for(inti=0;i<=5;i++){             for(intk=0;k<=5-i;k++) {System.out.print (" "); }             for(intj=0;j<=2*i;j++) {System.out.print ("*");        } System.out.println (); }         for(inti=1;i<=5;i++){             for(intk=1;k<=i+1;k++) {System.out.print (" "); }             for(intj=1;j<=11-2*i;j++) {System.out.print ("*");        } System.out.println (); }    }}

Learn Java syntax from the topic

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.