練習6:編程計算
package com.lovoinfo;
/**
* 用while循環實現1-100求和
* @author jackfrued
*
*/
public class Test03 {
public static void main(String[] args) {
int sum = 0; // 累加變量
int i = 1; // 循環變量
while(i <= 100) {
sum += i++;
}
System.out.println("Sum = " + sum);
}
}
package com.lovoinfo;
/**
* 用do...while循環實現1-100求和
* @author jackfrued
*
*/
public class Test04 {
public static void main(String[] args) {
int sum = 0;
int i = 1;
do {
sum = sum + i;
i = i + 1;
} while(i <= 100);
System.out.println("Sum = " + sum);
}
}
package com.lovoinfo;
/**
* 使用for循環實現1-100求和
* @author jackfrued
*
*/
public class Test05 {
public static void main(String[] args) {
int sum = 0;
for(int i = 1; i <= 100; ++i) {
sum += i;
}
System.out.println("Sum = " + sum);
}
}
作業:計算1-100之間的偶數的和。
/**
* 0~100偶數相加
* @author 谷振東
*
*/
public class Tset05 {
public static void main(String[] args) {
double num = 0;
for (int i = 0; i <= 100;i=i+2 )
{
num += i;
}System.out.println(num);
}
}
練習7:編程計算
package com.lovoinfo;
public class Test06 {
public static void main(String[] args) {
double sum = 0;
for(int i = 1; i <= 10; i++) {
sum = sum + 1.0 / i;
}
System.out.println(sum);
}
}
練習8:編程輸入
package com.lovoinfo;
import java.util.Scanner;
public class Test07 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("n = ");
int n = sc.nextInt();
double result = 1;
for(int i = 1; i <= n; ++i) {
result = result * i;
}
System.out.println(n + "! = " + result);
sc.close();
}
}
作業:輸入兩個正整數m和n,計算m^n^。
import java.util.Scanner;
/**
* 輸入兩個正整數M和N,計算M的N平方。
* @author 谷振東
*
*/
public class Tset08 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("m=");
double m = sc.nextDouble();
System.out.println("n=");
double n = sc.nextDouble();
for (int i = 1; i <= m; i++) {
}for (int i = 1; i <= n; i++) {
}System.out.print(Math.pow(m, n));
sc.close();
}
}
練習9:找出1-100之間的素數。
package com.lovoinfo;
/**
* 找出1-100之間的素數(質數, 只能被1和自身整除的數)
* @author jackfrued
*
*/
public class Test12 {
public static void main(String[] args) {
for(int i = 1; i <= 100; i++) {
// 假定當前的i是素數將isPrime賦值爲true
boolean isPrime = true;
// 在2-(i-1)之間找尋i的因子
for(int j = 2; isPrime && j <= i - 1; j++) {
// i能被j整除說明它不是素數
if(i % j == 0) {
// 將isPrime修改成false表示i不是素數
isPrime = false;
}
}
// 如果isPrime的值是true說明i是素數就打印它
if(isPrime) {
System.out.println(i);
}
}
}
}
練習10:找出1-100之間的完美數。
提示:完美數是該數除自身外的所有因子之和等於這個數本身的數。如28=1+2+4+7+14。
/**
* 計算1-100之間的完美數。
*
* @author 谷振東
*
*/
public class Tset15 {
public static void main(String[] args) {
int c = 0;
for (int i = 1; i <= 100; i++) {
c = 0;
for (int j = 1; j <= i / 2; j++) {
if (i % j == 0) {
c += j;
}
}
if (c == i) {
System.out.println(i);
}
}
}
}
練習11:找出100-999之間的水仙花數。
提示:水仙花數是各位立方和等於這個數本身的數,如153=1^3^+5^3^+3^3^。
package com.lovoinfo;
public class Test11 {
public static void main(String[] args) {
for(int i = 100; i <= 999; i++) {
int gw = i % 10;
int sw = i / 10 % 10;
int bw = i / 100;
if(gw * gw * gw + sw * sw * sw + bw * bw * bw == i) {
System.out.println(i);
}
}
// for(int bw = 1; bw <= 9; bw++) {
// for(int sw = 0; sw <= 9; sw++) {
// for(int gw = 0; gw <= 9; gw++) {
// int num = bw * 100 + sw * 10 + gw;
// if(bw * bw * bw + sw * sw * sw + gw * gw * gw == num) {
// System.out.println(num);
// }
// }
// }
// }
}
}