第一次考核
1.變量名,類名,方法名命名規(guī)范,并舉例。
變量名:駝峰式 —— String superWoman;
由字母 數(shù)字 _ $ 組成
由字母 _ $ 開始
見名知意
不要用中文 拼音
類 名:首字母大寫 —— class Flower{}
方法名:駝峰式 —— getAge();
2.枚舉型如何定義,舉例說明。
class Flower{
enum FlowerColor{
RED,
BLUE,
YELLOW
}FlowerColor color;
}
public class Neum{
public static void main(String[] args){
Flower rose = new Flower();
rose.color = Flower.FlowerColor.RED;
if (rose.color == Flower.FlowerColor.RED) {
System.out.println("rose color is :" + rose.color);
}
}
}
3.x=1,y=2,z=3 ,則y+=z--/++x 的結(jié)果是多少。
結(jié)果是:3
4.java源文件中的有幾個(gè)public類,程序入口是什么。
一個(gè)public類,程序入口是主方法main
5.分別用for, for each, while,do{}while 來計(jì)算1-100的和,附上源碼
public class Hundred{
public static void main(String[] args){
int b = 0;
for (int i = 1; i <=100 ; i++ ) {
b += i;
}
System.out.println(b);
int c = 1;
int d = 0;
while ( c <=100 ){
d += c++;
}
System.out.println(d);
int e = 1;
int h = 0;
do{
h += e++;
}while(e <=100);
System.out.println(h);
}
}
6.舉例說明continue與break的含義。
break:程序運(yùn)行到指定字符跳出循環(huán)
public class Break{
public static void main(String[] args){
int[] number = {1,43,6,100};
for (int i : number ) {
if ( i == 100 ) {
break;
}
System.out.println(i);
}
}
} 結(jié)果:1 43 6
continue:程序運(yùn)行到指定字符跳過繼續(xù)循環(huán)
public class Neum{
public static void main(String[] args){
int[] number = {1,43,6,100};
for (int i : number ) {
if ( i == 6 ) {
continue;
}
System.out.println(i);
}
}
} 結(jié)果:1 43 100
7.使用三目運(yùn)算符來求三個(gè)數(shù)種的最大數(shù)。
public class Max{
public static void main(String[] args){
int result = max(2,4);
System.out.println(result);
}
public static int max(int num1,int num2){
int result = num2 > num1 ? num2 : num1;
return result;
}
}
9.把咱們班同學(xué)的姓(拼音),按照A-Z的順序輸出。
import java.util.Arrays;
public class Neum{
public static void main(String[] args){
String[] name = {"fengjing","liushaohua","cuiyingxin","zhangzhi"};
for (int a = 1; a < name.length ; a++) {
for (int b = name.length - 1; b>=a ; b-- ) {
if (name[b-1].compareTo(name[b]) > 0) {
String fs = name[b-1];
name[b-1]=name[b];
name[b] = fs;
}
}
}
System.out.println(Arrays.toString(name));
把一個(gè)數(shù)組進(jìn)行從小到大的排序 [參考資料]
(http://wiki.jikexueyuan.com/project/data-structure-sorting/bubble-sort.html)定義一個(gè)數(shù)組,使最小數(shù)與第一個(gè)位置交換,最大與最后一個(gè)位置交換。
import java.util.Arrays;
public class UserString{
public static void main(String[] args) {
int[] arr = {45,23,65,76,19,90,14};
int minIndex = 0;
int maxIndex = 0;
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
maxIndex = i;
}else {
minIndex = i;
}
}
int temp;
// 交換
temp = arr[0];
arr[0] = arr[minIndex];
arr[minIndex] = temp;
temp = arr[arr.length-1];
arr[arr.length-1] = arr[maxIndex];
arr[maxIndex] = temp;
System.out.print(Arrays.toString(arr));
}
}
結(jié)果
[14, 23, 65, 76, 19, 45, 90]
- 打印出所有的“水仙花數(shù)”,所謂“水仙花數(shù)”是指一個(gè)三位數(shù),其各位數(shù)字立方和等于該數(shù)本身。例如:153是一個(gè)水仙花數(shù),因?yàn)?53 = 1的三次方+5的三次方+3的三次方。請打印100-999之間的水仙花數(shù)。
public class UserString{
public static void main(String[] args) {
for (int i = 100;i < 1000 ; i++) {
int j = i%10;
int k = i/100;
int m = i/10%10;
if (i == j*j*j +k*k*k + m*m*m ) {
System.out.print(i+" ");
}
}
}
}