Java 判斷 迴圈

來源:互聯網
上載者:User

標籤:3.1   大小   style   出錯   表示   str   最小   nbsp   eth   

一、優先順序

1.1 先判斷5>3,true 6>4 true;然後true==true ,最後是true;

1.2 6>5,true;而true和4無法比較。所以該判斷出錯;

1.3  和1.2比較,可以知道該比較可以正常進行

1.4 錯誤,

1.5 >號優先順序要高於=。所以先判斷b>false ,這個無法判斷。所以錯誤

1.6 先判斷a和b true 或者false,但是true和false 沒法比較大小,所以也錯誤。

 

2、浮點數判斷大小

package hello;import java.util.Scanner;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner in=new Scanner(System.in);double 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);//浮點數直接判斷相等很容易出錯System.out.println(Math.abs(a-c)<1e-6);//差值和很小的一個數比較}}

明顯看到c逼近1,但是不是1,所以直接判斷a==c,false ;

浮點數比較是要用差值比較。

3、

4、

switch 遇到break才退出,否則會越過case 繼續做下一個case的任務。

package hello;import java.util.Scanner;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner in=new Scanner(System.in);int a=1;switch(a){case 1:case 2:System.out.println("您好");case 3:System.out.println("晚上好");break;default:System.out.println("再見");break;}}}

package hello;import java.util.Scanner;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner in=new Scanner(System.in);int a=4;switch(a){default:System.out.println("再見");case 1:case 2:System.out.println("您好");case 3:System.out.println("晚上好");break;}}}

 二、迴圈

2.1

package hello;import java.util.Scanner;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner in=new Scanner(System.in);int num=in.nextInt();int n=0;while(num!=0){n++;num=num/10;}System.out.println(n);}}

輸入為0時是有問題的。

package hello;import java.util.Scanner;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner in=new Scanner(System.in);int num;int n=0;num=in.nextInt();do{    n++;num=num/10;}while(num!=0);//分號別忘了System.out.println(n);}}

2.2 

package hello;import java.util.Scanner;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner in=new Scanner(System.in);int num=(int)(Math.random()*100);//產生隨機數int i=1;//次數int n;n=in.nextInt();while(n!=num){if(n>num){System.out.println("大了");}else{System.out.println("小了");}n=in.nextInt();i++;}System.out.println("隨機數為:"+num+",猜了"+i+"次。");}}

 

三、迴圈控制

int 4個位元組,4*8=32位。表示的最大數2^31-1,最小數-2^31。

3.1

package hello;import java.util.Scanner;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner in=new Scanner(System.in);System.out.println("100以內素數有:");int num=0;for(int i=2;i<=100;i++){int flag=0;//素數標誌for(int j=2;j<=(int)Math.sqrt(i);j++){if(i%j==0)//不是素數{flag=1;//不是素數break;}}if(flag==0)//素數{num++;System.out.println(i);}}System.out.println("100以內素數個數為:"+num);}}

 3.2

枚舉方法求多種情況。

package hello;import java.util.Scanner;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner in = new Scanner(System.in);int amount = in.nextInt();//總錢數for ( int one = 0; one <=amount; ++one )//1塊錢從1到33張{for ( int five = 0; five <= amount/5; ++five )//5塊錢 從1到6張{for ( int ten = 0; ten <= amount/10; ++ten )//10塊錢 從1到3張{for ( int twenty = 0; twenty <= amount/20; ++twenty )//20塊錢 1張{if ( one+five*5+ten*10+twenty*20 == amount ){System.out.println(one+"張1元,"+five+"張5元,"+ten+"張10元,"+twenty+"張20元-->"+amount);}}}}        }    }}

多個迴圈跳出

 

 

 

  

  

 

  

  

  

  

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

  

 

Java 判斷 迴圈

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.