public static void main(String[] args) {
創(chuàng)新互聯(lián)公司主營龍山網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,APP應(yīng)用開發(fā),龍山h5重慶小程序開發(fā)搭建,龍山網(wǎng)站營銷推廣歡迎龍山等地區(qū)企業(yè)咨詢
int array[] = new int[20];
int result = 0;
ListInteger list = new ArrayListInteger();
System.out.println("請輸入20個整數(shù):");
Scanner sc = new Scanner(System.in);
for (int i = 0; i 20; i++) {
array[i] = sc.nextInt();
if(array[i] % 2 == 0) {
list.add(array[i]);
result += array[i];
}
}
System.out.println("偶數(shù)為:");
for (int i = 0; i list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println("偶數(shù)和為:" + result);
}
for(int i=0;i;i+){
if(i%2=0){
System.out.println(i);}
}
一、按鈕“求2到100之間的偶數(shù)和”代碼如下:
Private Sub Command1_Click()Dim i As IntegerDim s As Integers = 0For i = 2 To 100 Step 2 s = s + iNext iPrint "2到100的所有偶數(shù)和是:"; sEnd Sub
二、按鈕“方法2”代碼如下:
Private Sub Command2_Click()Dim i As IntegerDim s As Integers = 0For i = 2 To 100If i Mod 2 = 0 Then s = s + iEnd IfNext iPrint "2到100的所有偶數(shù)和是:"; sEnd Sub
三、按鈕“方法3”代碼如下:
Private Sub Command3_Click()Dim i As IntegerDim s As Integers = 0i = 2DoIf i Mod 2 = 0 Then s = s + iEnd Ifi = i + 1Loop Until i 100Print "2到100的所有偶數(shù)和是:"; sEnd Sub
四、按鈕“方法4”代碼如下:
Private Sub Command4_Click()Dim i As IntegerDim s As Integers = 0i = 2Dos = s + ii = i + 2Loop Until i 100Print "2到100的所有偶數(shù)和是:"; sEnd SubPrivate Sub Command2_Click()Dim i, sum1, sum2 As Integersum1 = 0sum2 = 0For i = 1 To 100If i Mod 2 = 1 Thensum1 = sum1 + iElsesum2 = sum2 + iEnd IfNext iPrint "奇數(shù)和為" sum1Print "偶數(shù)和為" sum2End Sub
你所說的正方形輸出是指矩形輸出吧,下面是完整代碼:
public??class??Output
{
public?static?void?main(String?[]args)//主方法
{
int?i;
int?count=0;
for(i=0;i=40;i+=2)??
{
count++;
System.out.printf("%-4d",i);?
if(count%7==0)???//每行顯示7個偶數(shù)
{System.out.println();}
}
}
}
運行結(jié)果圖:
public?class?Test?{
public?static?void?main(String[]?args)?{
int?count?=?0;
for?(int?i?=?100;?i?=?1000;?i++)?{
if?(i?%?2?==?0)?{
System.out.print(i?+?"?");
if?(++count?%?10?==?0)?{
System.out.print("\n");
}
}
}
}
}