找到 270 篇文章 關於 Java8
32K+ 次瀏覽
因數是我們相乘得到另一個數字的數字。14 的因數是 2 和 7,因為 2 × 7 = 14。有些數字可以用不止一種方式分解因數。16 可以分解為 1 × 16、2 × 8 或 4 × 4。只能分解為 1 乘以自身的數字稱為質數。前幾個質數是 2、3、5、7、11 和 13。給定數字的所有質數因數的列表是該數字的質因數。將數字分解為其質因數的分解以及… 閱讀更多
5K+ 次瀏覽
如果一個數字的數字之和能被 3 整除,那麼這個數字就能被 3 整除。一些能被 3 整除的數字示例如下。數字 85203 能被 3 整除,因為它的數字之和 (8 + 5 + 2 + 0 + 3 = 18) 能被 3 整除。數字 79154 不能被 3 整除,因為它的數字之和 (7 + 9 + 1 + 5 + 4 = 26) 不能被 3 整除。程式 import java.util.Scanner; public class DivisibleBy3 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("請輸入一個數字:"); String num = sc.nextLine(); int digitSum = 0; for(int i = 0; i
1K+ 次瀏覽
如果一個數字的交替數字之差能被 11 整除,那麼這個數字就能被 11 整除。即,如果 (奇數之和) – (偶數之和) 為 0 或能被 11 整除,則給定數字能被 11 整除。程式 import java.util.Scanner; public class DivisibleBy11 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("請輸入一個數字:"); String num = sc.nextLine(); int digitSumEve = 0; int digitSumOdd = 0; for(int i = 0; i
911 次瀏覽
如果一個數字的數字之和能被 9 整除,那麼這個數字就能被 9 整除。一些能被 9 整除的數字示例如下。數字 51984 能被 9 整除,因為它的數字之和 (5 + 1 + 9 + 8 + 4 = 27) 能被 9 整除。數字 91403 不能被 9 整除,因為它的數字之和 (9 + 1 + 4 + 0 + 3 = 17) 不能被 9 整除。程式 import java.util.Scanner; public class DivisibleBy9 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("請輸入一個數字:"); String num = sc.nextLine(); int digitSum = 0; for(int i = 0; i
2K+ 次瀏覽
程式以下是計算陣列數字的 GCD 的示例。線上演示 public class GCDOfArrayofNumbers{ public static int gcd(int a,int b){ int res = 0; while (b > 0){ int temp = b; b = a % b; a = temp; res = a; } return res; } public static void main(String arg[]){ int[] myArray = {3, 6, 8}; int result = gcd(myArray[0],myArray[1]); for(int i = 2; i < myArray.length; i++){ result = gcd(result, myArray[i]); } System.out.println("n 個數字的 GCD 是:"+result); } }輸出GCD n 個數字的 GCD 是:1
4K+ 次瀏覽
兩個值的最小公倍數 (LCM) 是這兩個值的最小正倍數。例如,3 和 4 的倍數是:3 → 3、6、9、12、15 ...4 → 4、8、12、16、20 ...這兩個數字的最小倍數是 12,因此 3 和 4 的 LCM 是 12。程式以下示例計算陣列數字的 LCM。線上演示 public class LCMofArrayOfNumbers { public static void main(String args[]) { int[] myArray = {25, 50, 125, 625}; int min, max, x, lcm = 0; for(int i = 0; i
807 次瀏覽
以下是一個計算兩個給定數字的 LCM 和 GCD 的示例。程式 import java.util.Scanner; public class LCM_GCD { public static void lcm(int a, int b){ int max, step, lcm = 0; if(a > b){ max = step = a; } else{ max = step = b; } while(a!= 0) { if(max%a == 0 && max%b == 0) { lcm = max; break; } max += step; } System.out.println("給定數字的 LCM 是 :: "+lcm); } public static void gcd(int a,int b){ int i, hcf = 0; for(i = 1; i
164 次瀏覽
Java.io.StringWriter 類是一個字元流,它將輸出收集到一個字串緩衝區中,然後可以使用該緩衝區構造一個字串。關閉 StringWriter 沒有任何效果。Java.io.PrintWriter 類將物件的格式化表示形式列印到文字輸出流。使用這兩個類,您可以將堆疊跟蹤轉換為字串。示例線上演示import java.io.PrintWriter; import java.io.StringWriter; public class StackTraceToString { public static void main(String args[]) { try { int a[] = new int[2]; System.out.println("Access element three :" + a[3]); } catch (ArrayIndexOutOfBoundsException e) ... 閱讀更多
瀏覽量:254
示例您可以按照以下程式檢查給定字串是否為數字。線上演示import java.util.Scanner; public class StringNumeric { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("輸入一個字串:"); String str = sc.next(); boolean number = str.matches("-?\d+(\.\d+)?"); if(number) { System.out.println("給定字串是一個數字"); } else { System.out.println("給定字串不是一個數字"); } } }輸出輸入一個字串: 4245 給定字串是一個數字
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP