解答:
import java.util.Scanner; //沒用到
public class JPA303
{
public static void main(String[] args)
{
int i, num, sum = 0;//題給
System.out.printf("1~1000中的完美數有: ");//題給
for(num=1;num<=1000;num++) //設num為1~1000
{
sum=0; //每跑一個num清為0(將總和清為0)
for(i=1;i<num;i++) //判斷因數的公式
{ //尋找num的因數
if(num%i==0)
{
sum+=i; //題目要求條件(完美數-因數相加)
}
}
if(sum==num) //題目要求條件(完美數相等)
{
System.out.printf("%d ",num); //題給
}
}
System.out.printf("\n"); //題給
}
}
神奇小手手 ヾ(*´∇`)ノ
解答:
import java.util.Scanner;
public class JPA303_2
{
public static void main(String[] args)
{
int i, num,count=0;
System.out.printf("2~1000中的質數有: ");
for(num=2;num<=1000;num++)
{
count=0;
for(i=1;i<=num;i++)
{ if(num%i==0)
{
count++; //質數判斷(加總因數)
}
}
if(count==2) //質數判斷(1和本身)
{
System.out.printf("%d ",num);
}
}
System.out.printf("\n");
}
}
當圈數未知時,使用while(前測式迴圈)
while(true) //條件永遠成立
{
break; //因為沒有條件,所以會有無窮迴圈 //因此要使用break強制跳出
}
解答:
import java.util.Scanner;
public class JPA304
{
static Scanner keyboard=new Scanner(System.in);
public static void main(String[] args)
{
int total = 0; //總金額
int s = 0; //輸入的金額 (餐點費)
int count = 0; //用餐人數 (題設五位朋友)
double average; //計算平均
while(true) //無窮迴圈
{
System.out.print("Please enter meal dollars or enter -1 to stop: ");
s=keyboard.nextInt();
if(s!=-1) //無窮迴圈 //(=-1)代表輸入完畢
{
total+=s; //加總金額
count++; //算 幾道菜,為了求平均值
}
else //s=-1
{
break; //while強制跳出,不然他會一直跑
}
}
average=(double)total/count; //(double)強制浮點數
System.out.println("餐點總費用:" + total);
System.out.printf(" %d 道餐點平均費用為: %.2f \n",count,average);
}
}
import java.util.Scanner;
public class JPA304_2
{
static Scanner keyboard=new Scanner(System.in);
public static void main(String[] args)
{
int total = 0;
int s = 0;
int count = 0;
double average;
do
{
System.out.print("Please enter meal dollars or enter -1 to stop: ");
s=keyboard.nextInt();
total+=s; //加總金額
count++; //算 幾道菜,為了求平均值
}while(count<5); //限定5筆以內(已給條件,自動跳出)
average=(double)total/count;
System.out.println("餐點總費用:" + total);
System.out.printf(" %d 道餐點平均費用為: %.2f \n",count,average);
}
}
解答:
import java.util.Scanner;
public class JPA305
{
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
test();
test();
test();
}
public static void test()
{
System.out.print("Please enter one value: ");
int n=keyboard.nextInt();
int result=1; //用乘的,所以不能乘0
if((n>0)&&(n<=10)) //1~10
{
for(int i=1;i<=n;i++)
{
result*=i; //用乘的
}
System.out.printf("%d!: %d\n",n,result);
}
else
{
System.out.println("Error,the value is out of range.");
}
}
}
最後編輯時間:2021/4/17 8:57am.
JAVA課堂學習
複習用
高科大
◈1-1
Apr 1, 2025:::spoiler 文章目錄 ::: 前情提要 報告時間(下週分組)、報告方式(中文報告,講英文則加分) 2025/5/5(一)早上或下午 建工 聽演講 R最重要的套件之一 ==ggplot2== 安裝路徑
Mar 24, 2025➤ 在校上課紀錄筆記 <i class="fa fa-file-code-o" aria-hidden="true"></i><img alt="GitHub repo file count (file extension)" src="https://img.shields.io/badge/%F0%9F%94%A5-%E7%81%AB%E7%86%B1%E8%A3%BD%E4%BD%9C%E4%B8%AD-red"> <img alt="GitHub repo file count (file extension)" src="https://img.shields.io/badge/%E6%B4%BB%E5%8B%95%E7%B5%90%E6%9D%9F%20--%202021%2F12%2F25-%20-lightgrey">學校複習用
Mar 23, 2025◈1-1
Mar 19, 2025or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up