【问题标题】:How to print a multiplication table in java如何在java中打印乘法表
【发布时间】:2016-11-02 04:37:07
【问题描述】:

我必须创建一个打印时间表的程序 1)有多种方法。 2)读取两个数字,其中一个是上限,第二个是表格的深度(本质上是行和列)。它比方桌的限制更上一层楼,例如,方桌只能是 10x10 或 12x12。
3)有一个循环,以便用户可以使用不同的输入再次运行它。 4) 使用 Scanner 读取 I/P。 当您为第一个数字输入 3 并为第二个数字输入 8 时,输出应该如下所示:

但结果是这样的:Output

import java.util.Scanner;
public class TimesTableRewrite
{
    public static void main (String[] args) 
    {   
        Scanner in;
        in = new Scanner (System.in);
        boolean runAgain = false;
        header();
        String response;
        do 
        {   

            printTable();
            System.out.println("\nDo you want to go again? Y or N?");
            response = in.next();
            if ((response.charAt(0)=='Y'||response.charAt(0)=='y'))
                {
               runAgain = true;
                }
            else
                {
               runAgain = false;    
                }
         }
       while (runAgain);
        footer();       
    }

    public static void printTable()
    {
        Scanner in;
        in = new Scanner (System.in);

        int height;
        int length;
        System.out.println("Enter the first number to set up how far down you want the table to go.");
        height= Integer.parseInt(in.next());
        System.out.println("Enter the second number to extend the table horizontally");
        length = Integer.parseInt(in.next());

        for (int i = 1; i <= height; i++ )
        {
            for (int j = 1; j <= length; j++ ) //j is number of columns
            {
                if (i<height)
                    System.out.format("%4d", + i*j);
                else
                    System.out.format("%4d", + i*j);
                if (i==height)
                {
                    System.out.println();
                }
            }   
        }
        for (int i = 1; i <= height; i++ )
        {
            for (int j = 1; j <= length; j++ ) 
            {
                if (j<length)
                    System.out.format("%4d", + i*j);
                else
                    System.out.format("%4d", + i*j);
                if (j==length)
                {
                    System.out.println();
                }   
            } 
        }
    }
    public static void header()
    {
        System.out.println("This program will help you practice your times tables!");
        System.out.print("This newer version will allow you to go beyond the 12 times tables!");
        System.out.println("It will let you choose your upper limit \nand how deep the times table will go.");
        System.out.println("Let's go!");
        System.out.println("\nPlease enter two numbers to generate a multiplication table.");
    }   
    public static void footer ()
    {
        System.out.println("That's all folks! See you next week!");
    }           
}

【问题讨论】:

  • 我不是要你们帮我做作业。正如你所看到的,我已经完成了大部分工作,我只是需要帮助来弄清楚为什么会这样。我已经花了好几个小时来处理它,但我无法让它工作,所以如果我寻求一些指导的话,上帝保佑。

标签: java eclipse loops methods


【解决方案1】:

替换这个:

for (int i = 1; i <= height; i++ )
{
    for (int j = 1; j <= length; j++ ) //j is number of columns
    {
        if (i<height)
            System.out.format("%4d", + i*j);
        else
            System.out.format("%4d", + i*j);
        if (i==height)
        {
            System.out.println();
        }
    }   
}
for (int i = 1; i <= height; i++ )
{
    for (int j = 1; j <= length; j++ ) 
    {
        if (j<length)
            System.out.format("%4d", + i*j);
        else
            System.out.format("%4d", + i*j);
        if (j==length)
        {
            System.out.println();
        }   
    } 
}

有了这个:

for (int i = 1; i <= height; i++ )
{
    for (int j = 1; j <= length; j++ ) //j is number of columns
    {
        System.out.print(i * j);
        System.out.print("\t"); //We add a tab after each number to form every column.
    }   

    System.out.println(); //We add a berakline for each row.
}

【讨论】:

  • 您可以在“runAgain”逻辑中使用三元运算符。像这样:runAgain = (response.charAt(0)=='Y' || response.charAt(0)=='y');
猜你喜欢
  • 2018-08-30
  • 2014-12-11
  • 1970-01-01
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多