在整个代码中初始化变量和/或更新值的小问题

我之前有一个功能版本,它使用了较少的唯一变量和第一个变量的更多实例,但是在添加了两个与第一个变量相关的变量之后,我的代码出现了一个错误,表明我需要初始化我的变量“numbList”。


理想情况下,我想在每个新行之后简单地更新“numbList”的值,然后更新“numbSquare”和“numbCube”的值。到目前为止,我还没有多少运气。


public class Assignment2BPart2 {

    public static void main(String[] args) {

        int numbList;

        int numbSquare;

        int numbCube;


        numbSquare = numbList * numbList;

        numbCube = numbSquare * numbList;


        System.out.println("The following is a table of values, showing numbers 0 - 10 and their squares and cubes.");

        System.out.printf("%s%s%s%n", "Number   ", "Square  ", "Cube");

        System.out.printf("%d \t %d \t %d%n", numbList = 0, numbSquare, numbCube);

        System.out.printf("%d \t %d \t %d%n", numbList = 1, numbSquare, numbCube);

        System.out.printf("%d \t %d \t %d%n", numbList = 2, numbSquare, numbCube);

        System.out.printf("%d \t %d \t %d%n", numbList = 3, numbSquare, numbCube);

        System.out.printf("%d \t %d \t %d%n", numbList = 4, numbSquare, numbCube);

        System.out.printf("%d \t %d \t %d%n", numbList = 5, numbSquare, numbCube);

        System.out.printf("%d \t %d \t %d%n", numbList = 6, numbSquare, numbCube);

        System.out.printf("%d \t %d \t %d%n", numbList = 7, numbSquare, numbCube);

        System.out.printf("%d \t %d \t %d%n", numbList = 8, numbSquare, numbCube);

        System.out.printf("%d \t %d \t %d%n", numbList = 9, numbSquare, numbCube);

        System.out.printf("%d \t %d \t %d%n", numbList = 10, numbSquare, numbCube);       

    }

}

预期结果将是一个带有标题阅读的 3 列表


数方立方


第 1 列的其余部分是值 0-10,第 2 列是这些数字的平方,第 3 列是这些数字的立方。


到目前为止,我无法在此迭代中运行代码。


至尊宝的传说
浏览 120回答 3
3回答

红颜莎娜

实现这样重复的通常方法是使用for循环:System.out.println("The following is a table of values, showing numbers 0 - 10 and their squares and cubes.");System.out.printf("%s%s%s%n", "Number&nbsp; &nbsp;", "Square&nbsp; ", "Cube");for (int num = 0; num <= 10; num++) {&nbsp; &nbsp; System.out.printf("%d \t %d \t %d%n", num, num * num, num * num * num);}

白衣非少年

您正在寻找的解决方案是如何使代码和变量可重用。一种方法是使用函数,尽管您可能还没有学习到这一点。然后,您将使用 for 循环将数字 1 到 10 输入到您的函数中以获得所需的输出。&nbsp;public static int NumberSquared(int number)&nbsp;{&nbsp; &nbsp; return number*number;&nbsp;}&nbsp;public static int NumberCubed(int number)&nbsp;{&nbsp; &nbsp; return number*number*number;&nbsp;}&nbsp;public static void main(String[] args)&nbsp;&nbsp;{&nbsp; &nbsp; for(int i=0; i <= 10; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Number " + i + " Square: " + NumberSquared(i) + " Cube: " + NumberCubed(i));&nbsp; &nbsp; }&nbsp;}

眼眸繁星

逻辑是错误的,你必须在更新之后计算平方和立方体numbList,所以你必须使用任何循环或递归来做到这一点,但我更喜欢有两种方法返回 int 值,一个用于计算平方,另一个用于立方体public class NewMain {public static void main(String[] args) {&nbsp; &nbsp; int numbList = 0;&nbsp; &nbsp; System.out.println("The following is a table of values, showing numbers 0 - 10 and their squares and cubes.");&nbsp; &nbsp; System.out.printf("%s%s%s%n", "Number&nbsp; &nbsp;", "Square&nbsp; ", "Cube");&nbsp; &nbsp; //&nbsp; &nbsp; System.out.printf("%d \t %d \t %d%n", numbList = 0, squareOfNumber(numbList), cubeOfNumber(numbList));&nbsp; &nbsp; System.out.printf("%d \t %d \t %d%n", numbList = 1, squareOfNumber(numbList), cubeOfNumber(numbList));&nbsp; &nbsp; System.out.printf("%d \t %d \t %d%n", numbList = 2, squareOfNumber(numbList), cubeOfNumber(numbList));&nbsp; &nbsp; System.out.printf("%d \t %d \t %d%n", numbList = 3, squareOfNumber(numbList), cubeOfNumber(numbList));&nbsp; &nbsp; System.out.printf("%d \t %d \t %d%n", numbList = 4, squareOfNumber(numbList), cubeOfNumber(numbList));&nbsp; &nbsp; System.out.printf("%d \t %d \t %d%n", numbList = 5, squareOfNumber(numbList), cubeOfNumber(numbList));&nbsp; &nbsp; System.out.printf("%d \t %d \t %d%n", numbList = 6, squareOfNumber(numbList), cubeOfNumber(numbList));&nbsp; &nbsp; System.out.printf("%d \t %d \t %d%n", numbList = 7, squareOfNumber(numbList), cubeOfNumber(numbList));&nbsp; &nbsp; System.out.printf("%d \t %d \t %d%n", numbList = 8, squareOfNumber(numbList), cubeOfNumber(numbList));&nbsp; &nbsp; System.out.printf("%d \t %d \t %d%n", numbList = 9, squareOfNumber(numbList), cubeOfNumber(numbList));&nbsp; &nbsp; System.out.printf("%d \t %d \t %d%n", numbList = 10, squareOfNumber(numbList), cubeOfNumber(numbList));}public static int squareOfNumber(int num) {&nbsp; &nbsp; return num*num;}public static int cubeOfNumber(int num) {&nbsp; &nbsp; return num*num*num;&nbsp; }}for循环你可以应用上面相同的逻辑for(int i=0; i<=10; i++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("%d \t %d \t %d%n", i, i*i, i*i*i);&nbsp; &nbsp; }你也可以使用Math.pow()nt intResult = (int) Math.pow(2, 3);该方法将 2 提高到 3 的幂并返回结果
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java