Java Powers显示表

时间:2014-03-16 19:12:16

标签: java

我需要显示如下输出:

输入一个整数:3

Number Squared Cubed
====== ======= =====

1 1 1
2 4 8
3 9 27

但是,当我运行代码时,我得到了这个输出:

数字平方立方
====== ======= =====

3 9 27

换句话说,我需要显示一个整数的幂,包括小于或等于整数的数字的幂。需要列出较小整数的数字,但不会与输入的整数一起显示。如何修复代码以确保它输出的所有整数小于或等于输入的整数?没有错误(即红色感叹号圆圈),但我需要弄清楚正确的计算。

以下是代码:

====================

import java.util.Scanner;

public class Powers 
{ 

public static void main(String[] args) 
{

    System.out.println("Welcome to the Squares and Cubes Table");
    System.out.println();

    Scanner sc = new Scanner(System.in);
    String choice = "y";

    while(choice.equalsIgnoreCase("y"))
    {
            // get the input from the user
            System.out.println("Enter an Integer: ");
            int integerNext = sc.nextInt();     

            System.out.println("Number" + "  " + "Squared" + "  " + "Cubed");       
            System.out.println("======" + "  " + "======" + "  " + "======");

            for(int i = 1; i <= integerNext; i++) 
                  {      
                  i = integerNext;
                  int numberSquared = (int) Math.pow(i, 2);
                  int numberCubed = (int) Math.pow (i, 3);

             String message = "\n" + i + "       " + numberSquared + "       " + numberCubed;

             System.out.println(message);
             System.out.println();

             // see if the user wants to continue
              System.out.print("Continue? (y/n): ");
              choice = sc.next();
              System.out.println(); 
                  }
             }
        }
   }

总是感谢帮助。感谢。

5 个答案:

答案 0 :(得分:1)

首先,正如Nikhil所说:&#34;删除行i = integerNext;它正在重置I的值,因此只打印最后一行&#34;。

其次,在获得用户输入之前移动第一个结束大括号 - 你想要运行循环,并且如果我理解正确的话,只询问是否已经完成,

答案 1 :(得分:0)

删除行i = integerNext;它正在重置I的值,因此只打印最后一行

答案 2 :(得分:0)

你几乎就在那里。由于您从1循环到integerNext(文本中为3),循环变量i将在每次迭代时获得值[1,2,3],因此您不需要必须手动设置i。当你这样做时:

i = integerNext;

您将i设置为3,因此循环将在达到循环条件时完成。

您可能还想将"Continue?"检查放在循环之外:

for (int i = 1; i <= integerNext; i++) {
    int numberSquared = (int) Math.pow(i, 2);
    int numberCubed = (int) Math.pow(i, 3);

    String message = "\n" + i + "       " + numberSquared + "       " + numberCubed;

    System.out.print(message);
}

// see if the user wants to continue
System.out.print("\nContinue? (y/n): ");
choice = sc.next();
System.out.println();

答案 3 :(得分:0)

import java.util.Scanner;

public class SquaresAndCubes {

    public static void main(String[] args)
    {
        // Welcome the user
        System.out.println("Welcome to the Squares and Cubes table");
        System.out.println();

        Scanner sc = new Scanner(System.in);
        String choice = "y";

        do
        {
            // Get input from the user
            System.out.print("Enter an integer: ");
            int integer = sc.nextInt();

            // Create a header
            String header = "Number  " + "Squared " + "Cubed   " + "\n"
                        +   "======  " + "======= " + "=====   ";
            System.out.println(header);

            int square = 0;
            int cube = 0;

            String row = "";
            for (int i = 1; i <= integer; i++)
            {

                square = i * i;
                cube = i * i * i;

                row = i + "       " + square + "       " + cube;
                System.out.println(row);
            }

            // See if the user wants to continue
            System.out.print("Continue? (y/n): ");
            choice = sc.next();
            System.out.println();

        }
        while (!choice.equalsIgnoreCase("n"));  
    }
}

答案 4 :(得分:0)

使用 foo 循环和一些打印行的基本方法

Scanner sc = new Scanner(System.in);
    System.out.print("What number would you like to go up to? ");
    int userInt = sc.nextInt();
    System.out.println("");
    System.out.println("Here is your table!");
    System.out.println("");
    System.out.println("number | squared | cubed");
    System.out.println("------ | ------- | -----");

    for (int i = 1; i <= userInt; i++){
        System.out.println(i + "      | " + (i * i) + "       |" + "  " +(i * i * i));
    }