數(shù)組:
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 73203。
馬克-to-win:一組數(shù),內(nèi)存中連在一起。例如:a[]。
題目:4個(gè)數(shù),求這4個(gè)數(shù)的和。
例1:
public class Test1 {
public static void main(String[] args) {
int i = 0;
int b = 0;
int a[] = { 1, 2, 3, 4 };
while (i < 4) {
b = b + a[i];
System.out.println(b);
i = i + 1;
}
}
}
結(jié)果:
1
3
6
10
題目:4個(gè)數(shù),求a[0]和a[2]的和。
public class Test41 {
public static void main(String[] args) {
int i = 0;
int b = 0;
int a[] = { 1, 2, 3, 4 };
while (i < 4) {
if (i == 0 || i == 2) {
b = b + a[i];
// System.out.println(b);
}
i = i + 1;
}
System.out.println("和is" + b);
System.out.println("循環(huán)完了 ");
}
}
結(jié)果:
和is4
循環(huán)完了
馬克-to-win:循環(huán),賦初值i=0,有判斷while (i < 5) {} 有自加的過(guò)程。i=i+2,或i++,i=0,1,2,
例2:
public class Test2 {
public static void main(String[] args) {
int i = 0;
int a[] = { 1, 2, 3, 4, 5 };
while (i < 5) {
System.out.println(a[i]);
i++;
}
}
}
結(jié)果:
1
2
3
4
5
例3:
求a和b之間的大數(shù)。
public class Test3 {
public static void main(String[] args) {
int a = 7;
int b = 6;
int c = 0;
if (a > b) {
c = a;
}
if (a < b) {
c = b;
}
{
System.out.println(c);
}
}
}
結(jié)果:
7
例4:
馬克-to-win:找數(shù)組里最大的數(shù),讓c=a[i],假如a[i]大于c,c等于a[i],a.length數(shù)組長(zhǎng)度。
public class Test4 {
public static void main(String[] args) {
int i = 0;
int a[] = { 2, 4, 6, 8 };
int c = a[0];
while (i < a.length) {
if (a[i] > c) {
c = a[i];
}
i++;
}
System.out.println(c);
}
}
結(jié)果:
8
作業(yè)1:輸出數(shù)組前三個(gè)。
public class Test5 {
public static void main(String[] args) {
int a[] = { 7, 6, 8, 5, 4 };
int i = 1;
while (i < 4) {
System.out.println(a[i]);
i++;
}
}
}
結(jié)果:
6
8
5
馬克-to-win:作業(yè):數(shù)組String s[6]={"唱歌","跳舞","畫(huà)畫(huà)","大字","太極","手工"},
打印出從四年級(jí)到五年級(jí)的課外活動(dòng),例如一年級(jí)唱歌。
public class Test6 {
public static void main(String[] args) {
String s[] = { "唱歌", "跳舞", "畫(huà)畫(huà)", "大字", "太極", "手工" };
int i = 0;
while (i < 5) {
if (2 < i && i < 5) {
System.out.println(s[i]);
}
i++;
}
}
}
結(jié)果:
大字
太極
作業(yè)2:馬克-to-win:打印數(shù)組其中幾個(gè)數(shù)。
public class Test7 {
public static void main(String[] args) {
int a[] = { 7, 6, 8, 5, 4 };
int i = 1;
while (i < 4) {
System.out.println(a[i]);
i++;
}
}
}
結(jié)果:
6
8
5
作業(yè)3:打印數(shù)組每一個(gè)數(shù)
public class Test11 {
public static void main(String[] args) {
int a[] = { 3, 5, 6, 4 };
int i = 0;
int b = 0;
while (i < 4) {
b = a[i];
System.out.println(b);
i++;
}
}
}
結(jié)果:
3
5
6
4
作業(yè)4:馬克-to-win:找數(shù)組中的某數(shù)有與沒(méi)有
public class Test12 {
public static void main(String[] args) {
int a[] = { 3, 7, 8, 9 };
int i = 0;
int c = 7;
while (i < a.length) {
if (c == a[i])
{System.out.println("有");return;}
i++;
}
System.out.println("沒(méi)有");
}
}
結(jié)果:
有
馬克-to-win:找數(shù)組中的數(shù)有與沒(méi)有
public class Test12 {
public static void main(String[] args) {
int a[] = { 3, 7, 8, 9 };
int i = 0;
int c = 2;
while (i < a.length) {
if (c == a[i])
System.out.println("有");
i++;
}
System.out.println("沒(méi)有");
}
}
結(jié)果:
沒(méi)有
作業(yè)5:馬克-to-win:找數(shù)組中的某數(shù)有與沒(méi)有
public class Test13 {
public static void main(String[] args) {
int a[] = { 3, 7, 8, 9 };
int i = 0;
int c = 5;
int q = 100;
while (i < a.length) {
if (c == a[i]) {
q = 200;
}
i++;
}
if (q == 200) {
System.out.println("有");
}
if (q == 100)
{
System.out.println("沒(méi)有");
}
}
}
結(jié)果:
沒(méi)有