Java Judgment Loop

Source: Internet
Author: User

First, priority

1.1 First Judge 5>3,true 6>4 true; then True==true, and finally true;

1.2 6>5,true; while True and 4 cannot be compared. So the judgment is wrong;

Compare 1.3 and 1.2 to know that the comparison can be performed normally

1.4 Errors,

1.5 > Priority is higher than =. So first Judge B>false, this cannot judge. So the error

1.6 A and B are judged to be true or false, but true and false cannot compare size, so it is also wrong.

2, the size of floating-point number judgment

Package Hello;import Java.util.scanner;public class Main {public static void main (string[] args) {//TODO auto-generated m Ethod stubscanner in=new Scanner (system.in);d ouble a=1.0;double b=0.2+0.2+0.2+0.2+0.2;double c=0.1+0.1+0.1+0.1+0.1+ 0.1+0.1+0.1+0.1+0.1; System.out.println ("a=" +a); System.out.println ("b=" +b); System.out.println ("c=" +c); System.out.println (a==c);//The floating-point number directly determines the equality is easy error System.out.println (Math.Abs (a-c) <1e-6);//Difference and a small comparison}}

Obviously see C approximation 1, but not 1, so directly judge A==c,false;

The comparison of floating-point numbers is to be compared with the difference value.

3.

4.

Switch exits if it encounters a break, otherwise it will continue to do the next case task over the case.

Package Hello;import Java.util.scanner;public class Main {public static void main (string[] args) {//TODO auto-generated m Ethod stubscanner in=new Scanner (system.in); int A=1;switch (a) {case 1:case 2:system.out.println ("Hello"); Case 3: System.out.println ("Good Evening"); Break;default:system.out.println ("Goodbye");

Package Hello;import Java.util.scanner;public class Main {public static void main (string[] args) {//TODO auto-generated m Ethod stubscanner in=new Scanner (system.in); int A=4;switch (a) {default:System.out.println ("good-bye"); case 1:case 2: System.out.println ("Hello"), Case 3:system.out.println ("Good Evening");

Second, the circulation

2.1

Package Hello;import Java.util.scanner;public class Main {public static void main (string[] args) {//TODO auto-generated m Ethod stubscanner in=new Scanner (system.in); int num=in.nextint (); int N=0;while (num!=0) {N++;NUM=NUM/10;} SYSTEM.OUT.PRINTLN (n);}}

There is a problem with the input as 0 o'clock.

Package Hello;import Java.util.scanner;public class Main {public static void main (string[] args) {//TODO auto-generated m Ethod stubscanner in=new Scanner (system.in); int num;int n=0;num=in.nextint ();d o{    N++;NUM=NUM/10;} while (num!=0);//Semicolon Don't forget System.out.println (n);}}

2.2

Package Hello;import Java.util.scanner;public class Main {public static void main (string[] args) {//TODO auto-generated m Ethod stubscanner in=new Scanner (system.in); int num= (int) (Math.random () *100);//generate random number int i=1;//number int n;n=in.nextint () while (N!=num) {if (n>num) {System.out.println ("big");} Else{system.out.println ("small");} N=in.nextint (); i++;} System.out.println ("Random number:" +num+ ", guess" +i+ "times.) ");}}

Third, Cycle control

An int 4 bytes, 4*8=32 bits. Represents the maximum number of 2^31-1, the minimum number of -2^31.

3.1

Package Hello;import Java.util.scanner;public class Main {public static void main (string[] args) {//TODO auto-generated m Ethod stubscanner in=new Scanner (system.in); SYSTEM.OUT.PRINTLN ("100 primes:"); int num=0;for (int i=2;i<=100;i++) {int flag=0;//prime number flag for (int j=2;j<= (int) MATH.SQRT (i); j + +) {if (i%j==0)//Not prime number {flag=1;//is not a prime break;}} if (flag==0)//prime number {num++; System.out.println (i);}} System.out.println ("The number of prime numbers within 100 is:" +num);}}

3.2

Enumeration methods to find multiple cases.

Package Hello;import Java.util.scanner;public class Main {public static void main (string[] args) {//TODO auto-generated m Ethod Stubscanner in = new Scanner (system.in); int amount = In.nextint ();//Total money for (int one = 0; one <=amount; ++one)// 1 dollars from 1 to 33 sheets {for (int five = 0; five <= amount/5; ++five)//5 block money from 1 to 6 sheets {for (int ten = 0; ten <= amount/10; ++ten)//10 Block money from 1 to 3 {for (int twenty = 0; twenty <= amount/20; ++twenty)//20 block Money 1 Sheets {if (one+five*5+ten*10+twenty*20 = = amount) {Sy Stem.out.println (one+ "Zhang 1 Yuan," +five+ "Zhang 5 Yuan," +ten+ "Zhang 10 Yuan," +twenty+ "Zhang 20--" +amount);}}}}}}    

Multiple loops jump out

  

  

  

  

  

  

 

  

  

Java Judgment Loop

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.