# JavaScript 編程題
頁面上輸入一個年份(需驗證),判斷是否是閏年(能被 4 整除,卻不能被 100 整除的年份;能被 400 整除的是閏年),并且在頁面上顯示相應提示信息。
~~~
<!doctype html>
<html>
<head>
<title>閏年</title>
<meta charset="utf-8">
</head>
<body>
<form>
請輸入年份:<input id="year" type="text" />
<span id="check"></span>
</form>
<script>
var input = document.getElementById("year");
var tip = document.getElementById("check");
//輸入框失去焦點觸發事件
input.onblur = function() {
var year = input.value.trim();
//年份由4位數字組成
if(/^\d{4}$/.test(year)) {
//能被4整除卻不能被100整除的年份;能被400整除的是閏年
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
tip.innerHTML = "閏年";
} else {
tip.innerHTML = "非閏年";
}
} else {
tip.innerHTML = "年份格式不正確請重新輸入";
}
}
</script>
</body>
</html>
~~~
---
# MySQL 問答題
如何通過命令提示符登入 MySQL?如何列出所有數據庫?如何切換到某個數據庫并在上面工作?如何列出某個數據庫內所有表?如何獲取表內所有 Field 對象的名稱和類型?
~~~
1. mysql -u -p
2. show databases;
3. use dbname;
4. show tables;
5. describe table_name ;
~~~
---
# Java 編程題
一個數如果恰好等于它的因子之和,這個數就稱為「完數」。例如 `6=1+2+3`.編程找出 1000 以內的所有完數。
~~~
package test;
/**
* 完數判斷
* @author CUI
*/
public class Tl5 {
/**
* 判斷是否是完數
* @param a 需判斷的數字
* @return boolean
*/
public static boolean test(int a) {
int cup = 0;
// 循環遍歷,找到所有因子,并計算因子之和
for (int i = 1; i < a; i++) {
if (a % i == 0)
cup = cup + i;
}
return (cup == a);
}
public static void main(String[] args) {
String str = "";
for (int i = 1; i < 1000; i++) {
if (test(i)) {
str += i + ",";
}
}
System.out.print(str.substring(0, str.length() - 1));
}
}
~~~