空心金字塔和菱形的打印
一.C语言实现
- 打印9*9乘法表
int main()
{
int i, j;
for (i = 1; i <= 9; i++)
{
for (j = 1; j <= i; j++)
{
int mul = i * j;
printf("%d*%d=%d\t", j, i, mul);
}
printf("\n");
}
getchar();
return 0;
}
- 打印空心金字塔
int main()
{
int i, j, k;//i控制行数,j控制每行的个数,k控制空格
unsigned int totalLevle=0;
printf("请输入空心金子塔的层数:");
scanf("%d", &totalLevle);
getchar();//过滤掉scanf的回车
for (i = 1; i <= totalLevle; i++)
{
for (k = 1; k <= totalLevle - i; k++)
{
printf(" ");
}
for (j = 1; j <= 2 * i - 1; j++)
{
//打印*号的条件
if (j == 1 || j == 2 * i - 1 || i == totalLevle)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
getchar();//暂停控制台
return 0;
}
- 打印空心菱形
int main()
{
printf("请输入一个奇数(>=3):");
unsigned int num = 0;
scanf("%d", &num);
getchar();
unsigned int halfNum = num / 2 + 1;
for (unsigned int i = 1; i <= num; i++)
{
if (i <= halfNum)
{
for (unsigned int k = 1; k <= halfNum - i; k++)
{
printf(" ");
}
for (unsigned int j = 1; j <= 2 * i - 1; j++)
{
if (j == 1 || j == 2 * i - 1)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
else
{
for (unsigned int k = 1; k <= i - halfNum; k++)
{
printf(" ");
}
for (unsigned int j = 1; j <= 2 * ((num + 1) - i) - 1; j++)
{
if (j == 1 || j == 2 * ((num + 1) - i) - 1)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
getchar();
return 0;
}
二.Java语言实现
- 打印9*9乘法表
public class TestDemo02 {
public static void main(String[] args) {
//9 * 9 乘法表
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + (i * j) + "\t");
}
System.out.println();
}
}
}
- 打印空心金字塔
public class TestDemo03 {
public static void main(String[] args) {
//打印空心金字塔
/*
先死后活即先给定5层的空心金字塔,然后扩展到任意层
1.先打印5*5矩形
2.打印半个三角形.(和9*9乘法表一样)
3.修改每层打印*号的个数(1,3,5,7,9...)得出:个数 = 2 * 层数 - 1
4.打印出每层的空格(实心金字塔完成)。(可以看到如果是五层:第一层空格为4,第二层3,依次类推)
5.根据条件判断哪里打印*,哪里打印空格,完成空心金字塔的打印。
*/
//打印任意层的空心金字塔
Scanner scanner = new Scanner(System.in);
System.out.println("请输入三角形的层数:");
int number = scanner.nextInt();
for (int i = 1; i <= number; i++) {
for (int k = 1; k <= number - i; k++) {
System.out.print(" ");//打印空格
}
for (int j = 1; j <= (2 * i - 1); j++) {
if (j == 1 || j == (2 * i) - 1 || i == number) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.print("\n");
}
}
}
- 打印空心菱形
public class TestDemo04 {
public static void main(String[] args) {
/*
打印空心菱形
先死后活,先规定打印一个9层的空心菱形
1.先打印实心菱形的上半部分
2.再打印实心菱形的下半部分
关于下半部分每层个数的表示公式:
下半层从6开始,即,第六层为7个,第七层为5个,第八层为3个,第九层为1个
公式:2 * (number +1 -i )-1; //i代表层数,number代表总层数
比如:i = 6,则就有2 * (9 + 1 - 6) - 1 = 7;
3.打印空心菱形中的空格
4.优化。
*/
Scanner scanner = new Scanner(System.in);
System.out.println("请输入空心菱形的层数(>=3):");
int number = scanner.nextInt();
int half_number = number / 2 + 1;
int i, j, k;
for (i = 1; i <= number; i++) {
if (i <= half_number) {
for (k = 1; k <= half_number - i; k++) {
System.out.print(" ");
}
for (j = 1; j <= 2 * i - 1; j++) {
if (j == 1 || j == 2 * i - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.print("\n");
} else {
for (k = 1; k <= i - half_number; k++) {
System.out.print(" ");
}
for (j = 1; j <= 2 * (number + 1 - i) - 1; j++) {
if (j == 1 || j == 2 * (number + 1 - i) - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
}
三.Java实现四则运算
public class CalculateImpl{
public static void main(String[] args) {
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个数字:");
double num1 = scanner.nextDouble();
Scanner scanner2 = new Scanner(System.in);
System.out.print("请输入运算符(+ - * /):");
String c1 = scanner2.next();
Scanner scanner3 = new Scanner(System.in);
System.out.print("请再输入一个数字:");
double num2 = scanner3.nextDouble();
if (c1.equals("exit")) {
break;
}
double result;
switch (c1) {
case "+":
result = add(num1, num2);
System.out.println(num1 + " + " + num2 + " = " + result);
break;
case "-":
result = sub(num1, num2);
System.out.println(num1 + " - " + num2 + " = " + result);
break;
case "*":
result = mul(num1, num2);
System.out.println(num1 + " * " + num2 + " = " + result);
break;
case "/":
result = div(num1, num2);
System.out.println(num1 + " / " + num2 + " = " + result);
break;
default:
System.out.println("输入有误!~");
}
}
}
public static double add(double num1, double num2) {
return num1 + num2;
}
public static double sub(double num1, double num2) {
return num1 - num2;
}
public static double mul(double num1, double num2) {
return num1 * num2;
}
public static double div(double num1, double num2) {
return num1 / num2;
}
}
四.C实现四则运算
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable:4996) //忽略非安全函数告警
#pragma warning(disable:6031) //使得getchar()函数不写返回值也不会报警告
double add(double num1, double num2);
double sub(double num1, double num2);
double mul(double num1, double num2);
double divi(double num1, double num2);
int main()
{
double num1, num2;
double result;
char c1;
while (1) {
printf("请输入一个数字:");
scanf("%lf", &num1);
getchar();
printf("请输入运算符号(+ - * /):");
scanf("%c", &c1);
getchar();
printf("请再输入一个数字:");
scanf("%lf", &num2);
getchar();
if (c1 == 'n')
{
break;
}
switch (c1)
{
case '+':
result = add(num1, num2);
printf("%.2f + %.2f = %.2f\n", num1, num2, result);
break;
case '-':
result = sub(num1, num2);
printf("%.2f - %.2f = %.2f\n", num1, num2, result);
break;
case '*':
result = mul(num1, num2);
printf("%.2f * %.2f = %.2f\n", num1, num2, result);
break;
case '/':
result = divi(num1, num2);
printf("%.2f / %.2f = %.2f\n", num1, num2, result);
break;
default:
printf("输入有误!~");
}
}
system("pause");//暂停控制台
return 0;
}
double add(double num1, double num2) {
return num1 + num2;
}
double sub(double num1, double num2) {
return num1 - num2;
}
double mul(double num1, double num2) {
return num1 * num2;
}
double divi(double num1, double num2) {
return num1 / num2;
}
五.完结
说明:小白一枚,代码过于基础,忘大佬指点!~~~谢谢!