public static void main(String[] args) {
创新互联公司主营龙山网站建设的网络公司,主营网站建设方案,APP应用开发,龙山h5重庆小程序开发搭建,龙山网站营销推广欢迎龙山等地区企业咨询
int array[] = new int[20];
int result = 0;
ListInteger list = new ArrayListInteger();
System.out.println("请输入20个整数:");
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("偶数为:");
for (int i = 0; i list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println("偶数和为:" + result);
}
for(int i=0;i;i+){
if(i%2=0){
System.out.println(i);}
}
一、按钮“求2到100之间的偶数和”代码如下:
Private Sub Command1_Click()Dim i As IntegerDim s As Integers = 0For i = 2 To 100 Step 2 s = s + iNext iPrint "2到100的所有偶数和是:"; 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的所有偶数和是:"; 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的所有偶数和是:"; 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的所有偶数和是:"; 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 "奇数和为" sum1Print "偶数和为" 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个偶数
{System.out.println();}
}
}
}
运行结果图:
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");
}
}
}
}
}