小練習_num1,練習_num1

來源:互聯網
上載者:User

小練習_num1,練習_num1

題目:將一個正整數分解質因數。例如:輸入90,列印輸出90=2*3*3*5。

 1 /* 2 分解質因數 3 */ 4 import java.util.*; 5 class num1 6 { 7     public static void main(String[ ] args)throws Exception 8     { 9         boolean b=true;10         while(b)11         {12                 try13                 {14                     Scanner sc = new Scanner(System.in);15                     System.out.println("********************************************************");16                     System.out.println("開始___請輸入一個大於一的正整數:");17                     int n = sc.nextInt();                        18                     int k=2;19                     System.out.print(n+"=");20                     while(k<=n)21                     {                        22                         if(k==n)    //判斷n是否等於k,如果等說明本身就是質數,尋找完畢,break!23                         {24                             System.out.println(n);break;25                         }26                         else if(n%k==0)    //此時n肯定不是質數,與k求餘,如果能被整除,此k為其一個質因數27                         {28                             System.out.print(k+"*");n=n/k;29                         }30                         else    //此時的n本身不是質數,此時k也不是其質因數,從小到大,開始尋找下一個質因數31                             k++;32                     }33                     while(k>n)34                     {35                         System.out.println("<ERROR-請輸入大於1的正整數>");break;36                     }        37                     }38                     catch(Exception e)39                     {40                         System.out.println("<!!您輸入的不是大於一的正整數 或者 超出計算範圍!>");41                     }42                     Scanner scgoon = new Scanner(System.in);43                     System.out.println("_______________________________________________");44                     System.out.println("斷行符號繼續,輸入exit退出");45                     String sg = scgoon.nextLine();46                     if(sg.equals("exit"))47                         {b=false;}48             }49     }50 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.