思考練習題

2021-09-14 06:42:26 字數 2160 閱讀 3971

1.迴圈求和:利用迴圈語句計算從100加到500的整數的總和。

public static void main(string args)

system.out.println("100到500的和為"+sum);}

public static void main(string args) else if(c>=65 && c<=90) else if(c>=97 && c<=122) else

public static void main(string args) }}

方法一:

public static void main(string args)

system.out.println(d+"的"+c+"次方是:"+result);

}else

把型別變為double型別

system.out.println(d+"的"+c+"次方是:"+(1.0/result));

system.out.println(d+"的"+c+"次方是:1/"+result);

}方法二:

public static void main(string args)

//當初輸入的指數是?

if(c>0) else

system.out.println(d+"的"+c+"次方是:"+result);

}5.統計字元:從鍵盤輸入乙個字串,統計這個字串中包含幾個大寫字母,幾個小寫字母,幾個數字,幾個特殊符號。

public static void main(string args)

else if(c>=65 && c<=90)else if(c>=97 && c<=122) else

}system.out.println("大寫字母個數:"+upsum);

system.out.println("小寫字母個數:"+lowsum);

system.out.println("數字個數:"+numsum);

system.out.println("特殊個數:"+othersum);

}6.組合問題:有紅球5個,黑球7個,白球9個,隨機取出12個,計算可能的顏色組合有多少種?

public static void main(string args)

count++;}}

system.out.println("組合成功的個數:"+count);}}

7.抓球問題:用一元紙幣兌換1分,2分和5分的硬幣,要求兌換總數為50枚,問可以有多少種組合,每種組合對應1分,2分,5分分別是多少

public class test_10

count++;}}

system.out.println("可以有"+count+"種組合");}

public static void main(string args)

system.out.println();//換行

}  }

9.**猜猜猜:隨機產生乙個200以內的**,並且讓使用者猜測該**,如果比實際**高,則輸出「高了」,否則輸出「低了」,直到猜對為止,輸出「對了」,程式結束。(使用math.random()可生成乙個0-1之間的隨機小數)。

public class test06 else if(cnum10。繼續完善上述第9題**,只給使用者猜測5次的機會,如果5次都沒有回答正確,則判定使用者「超時,輸了」。

public static void main(string args) else if(cnum//直接退出程式

system.exit(-1);}}

system.out.println("超時了");

}11.字串判斷:從鍵盤輸入乙個字串,判斷該字串是否可以被轉換為乙個有效的數字

//輸入乙個字串,判斷是否可以轉為有效數字

//1.只能出現乙個+或者-或者.

//2.約束

//3.+ -只能出現在第乙個位置

//4..只能一次,(只能在符號之後)

public class test_08 else if(c=='.') else

}if(!(c>=48 && c<=57))

}string out =isnumber?"能轉化為數字":"不能轉為數字";

system.out.println(str+out);}}

12.將上述**寫在不同的方法中,並通過main()方法進行呼叫。

python書中練習題 python練習題

1 定義乙個空列表,接收從鍵盤輸入的整數,把列表傳給乙個從大到小排序的函式,再輸出排序後的列表的值 listex b 0 a int input 請輸入列表長度 while b a num int input 請輸入字元 b 1 print listex sum 0 for i in range 0...

python的練習題 Python練習題

1 使用while迴圈輸入1 2 3 4 5 6 8 9 10 i 0while i 10 i i 1 if i 7 continue print i 結果 e python python python test.py1 2 求1 100的所有數的和 i 0sum 0 while i 100 i 1...

練習題 基礎練習

第一題 需求 1 計算 5 個月的生活大概開銷 spending 比如 rent 房租 800.00 mealcost 吃飯 900.00 clothingcosts 買衣服 300.00 othercosts 其他費用 300.00 public class dome1 第二題 需求 2 計算 1...