如何解决使用for循环打印表的Java问题? [英] How do I solve this Java question of printing a table using a for loop?

查看:106
本文介绍了如何解决使用for循环打印表的Java问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(打印表格)编写一个显示下表的程序.将浮点数转换为整数.

(Print a table) Write a program that displays the following table. Cast floating point numbers into integers.

a b pow(a, b)
1 2 1
2 3 8
3 4 81
4 5 1024
5 6 15625

在构思如何使用循环使这段代码更简单时,我遇到了麻烦.

I am having trouble conceptualizing how I can make this code simpler using loops.

public class Exercise_02_eighteen {
    public static void main(String[] args) {
        float a, b;
        System.out.println("a        b        pow(a, b)");
        a = 1;
        b = 2;
        System.out.println((int)a + "        " + (int)b + 
            "        " + (int)Math.pow(a, b));
        a++;
        b++;
        System.out.println((int)a + "        " + (int)b + 
            "        " + (int)Math.pow(a, b));
        a++;
        b++;
        System.out.println((int)a + "        " + (int)b + 
            "        " + (int)Math.pow(a, b));
        a++;
        b++;
        System.out.println((int)a + "        " + (int)b + 
            "        " + (int)Math.pow(a, b));
        a++;
        b++;
        System.out.println((int)a + "        " + (int)b + 
            "        " + (int)Math.pow(a, b));
    }
}

推荐答案

只需将打印内容移至for循环下即可:

Just move your print under for loop:

public class Exercise_02_eighteen {

    public static void main(String[] args) {
        System.out.println("a\tb\tpow(a, b)");

        for (int a = 1, b = 2; a <= 5; a++, b++)
            System.out.format("%d\t%d\t%d\n", a, b, (long)Math.pow(a, b));
    }

}

这篇关于如何解决使用for循环打印表的Java问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆