用于10次表的Java代码

时间:2013-11-27 02:26:26

标签: java

我有这个代码,它制作了一个时间表,但我需要做的只是一个10倍的表,意味着10 x 1是10,10 x 2是20,10 x 3是30 .... ....等10 x 10是100,这是我的代码,但我无法弄清楚如何编辑它。任何人都可以帮助我吗?

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');
    }
  }

}

4 个答案:

答案 0 :(得分:0)

试试这个,它不需要双循环来打印内部输出

    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');

输出

      1     2   3   4   5   6   7   8   9   10
10    10    20  30  40  50  60  70  80  90  100

答案 1 :(得分:0)

这是否符合您的需求

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');
    }
}

答案 2 :(得分:0)

你只需要采取第二个for循环

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');
//}
}

}

学校项目? :)

答案 3 :(得分:-1)

好吧,我们希望保留标题,从1到10打印。我们想要更改的是仅使用10的行。因此,我们将使用1 x 10表格而不是10 x 10表格

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);
      // Set outer to 10.
      for (int outer = 10; outer <= upperValue; outer++) {
        System.out.print("\t"+(inner*outer));
      }
      System.out.print('\n');
    }
  }

}

因此将外部更改为10,您将仅使用10作为内部循环。因此,当外环从1到10时,内环保持在10。

1 x 10,2 x 10等