六种语言实现输出乘法口诀表

前端之家收集整理的这篇文章主要介绍了六种语言实现输出乘法口诀表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

六种语言实现输出乘法口诀表

  • Objective-c
#import <Foundation/Foundation.h>
int main(int argc,const char * argv[]) {
    @autoreleasepool {
        for (int a=1; a<=9; a++) {
            for (int b=1; b<=a; b++) {
                printf("%dx%d = %d\t",b,a,a*b);
            }
            printf("\n");
        }
    }
    return   1;
}
  • C语言
#include <stdio.h>
int main(void){
    int a,b;
    for (a=1; a<=9; a++) {
        for (b=1; b<=a; b++) {
            printf("%dx%d = %d\t",a*b);
        }
        printf("\n");
    }
return 0;
}
  • java
package hello;

public class main {
    public static void main(String args[]){
         for (int a=1; a<=9; a++) {
                for (int b=1; b<=a; b++) {
                    System.out.printf("%d x %d = %d\t",a*b);
                }
                System.out.printf("\n");
            }
    }
}
  • JavaScript
for (var a = 1; a <10 ;a++){
    for (var b =1;b< a+1;b++){
        document.write(b+"×"+a+"="+(b*a)+"&nbsp;")
    }
    document.write("<br>")
 }
  • Swift
import Cocoa
for a in 1...9{
    for b in 1...a{
        print("\(b)x\(a) = \(b*a)",terminator:"\t")
    }
    print("")
}
  • Python
for a in range(1,10):
    for b in range(1,a+1):
        print(b,"x",a,"=",a*b,end="\t")
    print("")

可以看出 不同语言又不同的写法,从上到下,代码越来越少,越来越简洁,也能够看出这些语言的各自的一些特点。

猜你在找的Swift相关文章