求和
求偶数和
求奇数和
打印水仙花数
统计水仙花数
九九乘法表
import java.util.Scanner;
class HomeWork
{
public static void main(String[] agrs)
{
//for循环 求和
int a = getSum(10);
System.out.println("和为"+a);
//for求偶数和
int b = getOuSum(100);
System.out.println("偶数和为"+b);
//for求奇数和
int c = getJiSum(100);
System.out.println("奇数和为"+c);
//for打印水仙花数,统计水仙花个数
// printFlower();
//for打印乘法表
print99(9);
}
//for循环 求和
static int getSum(int num)
{
int sum = 0;
for (int i = 1; i <= num; i++)
{
sum += i;
}
return sum;
}
//for求偶数和
static int getOuSum(int num)
{
int sum = 0;
for (int i = 1; i <= num; i++)
{
if (i % 2 == 0)
{
sum += i;
}
}
return sum;
}
//for求奇数和
static int getJiSum(int num)
{
int sum = 0;
for (int i = 1; i <= num; i++)
{
if (i % 2 == 1)
{
sum += i;
}
}
return sum;
}
//for打印水仙花数,并统计个数。
static void printFlower()
{
int count = 0;
System.out.print("100-999中水仙花数有:");
for (int i = 100; i <= 999; i++)
{
int x = i % 10;
int y = i / 10 % 10;
int z = i / 100;
if (x*x*x + y*y*y + z*z*z == i)
{
System.out.print(i+"");
count++;
}
System.out.println(count++);
}
}
//for打印乘法表
static void print99(int num)
{
for (int i = 1; i <= num; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+"*"+i+"="+j*i+"\t");
}
System.out.println();
}
}
}
|