首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >10倍表的Java代码

10倍表的Java代码
EN

Stack Overflow用户
提问于 2013-11-27 10:26:26
回答 4查看 2.3K关注 0票数 0

我有这个代码,它做了一个乘法表,但我需要它做的只是一个10乘法表,意思是10 x 1是10,10 x 2是20,10 x 3是30........etc to 10 x 10是100,这是我的代码,但我不知道如何编辑它。有谁可以帮我?

代码语言:javascript
复制
public class TimesTable {

  public static void main(String[] args) {

    int lowerValue = 1;
    int upperValue = 10;

    for (int headingIndex = lowerValue; headingIndex <= upperValue; headingIndex++) {
      System.out.print("\t" + headingIndex);
    }
    System.out.print('\n');

    for (int inner = lowerValue; inner <= upperValue; inner++) {
      System.out.print(inner);
      for (int outer = lowerValue; outer <= upperValue; outer++) {
        System.out.print("\t"+(inner*outer));
      }
      System.out.print('\n');
    }
  }

}
EN

回答 4

Stack Overflow用户

发布于 2013-11-27 10:32:37

尝试一下,它不需要双循环来打印内部输出

代码语言:javascript
复制
    int lowerValue = 1;
    int upperValue = 10;

    for (int headingIndex = lowerValue; headingIndex <= upperValue; 
             headingIndex++) {
      System.out.print("\t" + headingIndex);
    }
    System.out.print('\n');

    int inner = 10;
    System.out.print(inner);
      for (int outer = lowerValue; outer <= upperValue; outer++) {
        System.out.print("\t"+(inner*outer));
      }
      System.out.print('\n');

输出

代码语言:javascript
复制
      1     2   3   4   5   6   7   8   9   10
10    10    20  30  40  50  60  70  80  90  100
票数 0
EN

Stack Overflow用户

发布于 2013-11-27 10:34:29

这能满足你的需要吗?

代码语言:javascript
复制
public static void main(String[] args) {
    int lowerValueHorizontal = 1;
    int lowerValue = 10;
    int upperValue = 10;

    for (int headingIndex = lowerValueHorizontal; headingIndex <= upperValue; headingIndex++) {
        System.out.print("\t" + headingIndex);
    }
    System.out.print('\n');

    for (int inner = lowerValue; inner <= upperValue; inner++) {
        System.out.print(inner);
        for (int outer = lowerValueHorizontal; outer <= upperValue; outer++) {
            System.out.print("\t"+(inner*outer));
        }
        System.out.print('\n');
    }
}
票数 0
EN

Stack Overflow用户

发布于 2013-11-27 10:38:13

您只需执行第二个for循环即可

代码语言:javascript
复制
public class TimesTable {

public static void main(String[] args) {

int lowerValue = 1;
int upperValue = 10;

for (int headingIndex = lowerValue; headingIndex <= upperValue; headingIndex++) {
  System.out.print("\t" + headingIndex);
}
System.out.print('\n');

//for (int inner = lowerValue; inner <= upperValue; inner++) {
  int inner = 10;
  System.out.print(inner);
  for (int outer = lowerValue; outer <= upperValue; outer++) {
    System.out.print("\t"+(inner*outer));
  }
  System.out.print('\n');
//}
}

}

学校项目?:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20232780

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档