這個美妙的名稱出自聖經《新約全書》中約翰福音第21章:耶穌對他們說:「把剛纔打的魚拿幾尾來。」西門·彼得就去把網拉到岸上。那網網滿了大魚,共一百五十三尾。魚雖這樣多,網卻沒有破。java
任寫一個3的倍數(正整數),把各位數字的立方相加,得出和,再把和的各位數字立方後相加,如此反覆進行,最後必然出現「聖經數」。git
判斷任一3的倍數的正整數,是不是聖經數,輸出計算過程。測試
1.判斷輸入數據X是不是3 的倍數的數 2.While(X!=153){ (1)將輸入的數轉化多個1位的數字 (2)計算每一個1位數字的三次方,求和,賦值給X } 3.已驗證
package Bible; import java.util.Scanner; /** * @author 15205 * @date 2019/5/2 12:35 */ public class Input { public static void main(String[] args) { int n = 0; while (n % 3 != 0 || n <= 0) { System.out.println("請輸入一個三的倍數(正整數)"); Scanner scanner = new Scanner(System.in); n = scanner.nextInt(); } while(n!=153) { Sanci sanci = new Sanci(); n = sanci.Sanci(n); System.out.println(n); } System.out.println("已驗證"); } } class Len { int Len(int n) { int len; Integer m = n; len = m.toString().length(); return len; } } class Chaifeng { int[] Chaifeng(int n) { int len; Len length=new Len(); len=length.Len(n); int a[]; a = new int[len]; int i; for (i = 0; i < len; i++) { a[i] = n % 10; n = n / 10; } return a; } } class Sanci { int Sanci(int n) { int i; int len; Len length=new Len(); len=length.Len(n); int a[]; Chaifeng chaifeng=new Chaifeng(); a=chaifeng.Chaifeng(n); n=0; for(i=0;i<len;i++) { n=a[i]*a[i]*a[i]+n; } return n; } }
package Bible; import junit.framework.TestCase; import org.junit.Test; /** * @author 15205 * @date 2019/5/2 13:43 */ public class LenTest extends TestCase { Len a = new Len(); Sanci b = new Sanci(); @Test public void testLen() throws Exception { assertEquals(3, a.Len(123)); assertEquals(1, a.Len(3)); assertEquals(4, a.Len(1236)); } @Test public void testSanci() throws Exception { assertEquals(27,b.Sanci(3)); assertEquals(351,b.Sanci(27)); assertEquals(126,b.Sanci(15)); assertEquals(9,b.Sanci(12)); } }