Java(流程控制作業)完全數、水仙花數、溫度轉換

import java.text.DecimalFormat;
import java.util.Scanner;




public class processControl {


/**
* 第三章 流程控制作業 顏碧霞
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
processControl pc = new processControl();
// pc.q1();
// pc.q2();
// pc.q3();
// pc.q4();
// pc.q5();
// pc.q6();
// pc.q7();
// pc.q8();
pc.q9();
}


//1、有限5位數,個位數爲6且能被3整除的五位數共有多少?
public void q1(){
for(int i = 10000 ; i < 100000 ; i++ ){
if( i % 10 == 6 && i % 3 == 0)
System.out.println(i);
}
}

//2、反序數就是將整數的數字倒過來的形成的整數。
public void q2(){
Scanner input = new Scanner(System.in);
System.out.print("請輸入一個四位數字:");
int number = input.nextInt();
int num1 = number % 10 ;
int num10 = number / 10 % 10 ;
int num100 = number / 100 % 10 ;
int num1000 = number / 1000 % 10 ;
int num = num1000 + num100 * 10 + num10 * 100 + num1 * 1000 ;
if(number == num) System.out.println(number+"是反序數");
else System.out.println(number+"不是反序數");
}

//3、完全數:如果一個數恰好等於他的因子之和,則成爲“完全數”。如:6的因子是1、2、3,而6=1+2+3,則6是個“完全數”。試求出1000以內的全部“完全數”。
public void q3(){
int sum = 0 ;
for(int i = 1 ; i <= 1000 ; i++){
for(int j = 1 ; j < i  ; j++){
if(i % j == 0)
sum += j ;
}
if(sum == i)
System.out.println(i + "是個完全數");
sum = 0 ;
}
}

//4、和的平方數3025這個數具有一種獨特的性質:將它評分爲兩段,即30和25,使之相加後求方,即(30+25)的平方等於3025本身。
//求具有這樣性質的全部四位數。這些數字滿足 abcd=(ab+cd)*(ab+cd)的關係
public void q4(){
for(int i = 1000 ; i <= 9999 ; i++){
int ab = i / 100 ;
int cd = i % 100 ;
if(Math.pow(ab + cd, 2) == i)
System.out.println(i);
}
}

//5、打印出100-999之間的所有“水仙花數”。所謂“水仙花數”,是指一個3位數,其各位數字立方和等於該數本身。例如:153是一個水仙花數。
public void q5(){
for(int i = 100 ; i <= 999 ; i++){
int i1 = i / 100 ;
int i2 = i % 100 / 10 ;
int i3 = i % 10 ;
if(i == Math.pow(i1, 3) + Math.pow(i2, 3) + Math.pow(i3, 3) )
System.out.println(i + "是一個水仙花數");
}
}

//6、學校2009年培養學生900人,每年增長25%,請問按此速度增長,到哪一年培訓學生人數將達到1萬人。
public void q6(){
int students = 900 ;
int count = 2009 ;
do{
students *= 1.25 ;
count++;
}while(students < 10000);
System.out.println("到" + count + "年培訓學生達到" + students + ",首次超過一萬人");
}

//7、使用do-while實現:輸出攝氏溫度與華氏溫度的對照表,要求它從攝氏溫度0度到250度,每隔20度爲一項,對照表中的條目不超過10條。
//轉換關係:華氏溫度 = 攝氏溫度 * 9 / 5.0 + 32
public void q7(){
DecimalFormat df = new DecimalFormat("0.0");
int temperature = 0 ;
System.out.println("攝氏溫度\t華氏溫度");
do{
System.out.println(temperature + "\t" + df.format(temperature * 9 / 5.0 + 32));
temperature += 20 ;
}while(temperature <= 250);
}

//8、求1~100之間不能被3整除的數之和
public void q8(){
int sum = 0 ;
for(int i = 1 ; i <= 100 ; i++){
if(i % 3 != 0)
sum += i ;
}
System.out.println(sum);
}

//9、循環錄入某學生5門課的成績並計算平均分,如果某分數錄入爲負,停止錄入並提示錄入錯誤
public void q9(){
DecimalFormat df = new DecimalFormat("0.00");
int sum = 0 ;
Scanner input = new Scanner(System.in);
for(int i = 5 ; i > 0 ; i--){
System.out.print("請輸入分數");
int score = input.nextInt();
if(score < 0){
System.out.println("錄入的成績爲負數,錯誤");
System.exit(0);
}
sum += score ;
}
System.out.println("你的平均分是:" + df.format(sum / 5.0));
}


}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章