循环打印两次java

循环打印两次java,java,for-loop,Java,For Loop,我不知道为什么我的循环打印了两次。我创建了两个独立的类,无论出于什么原因,我都无法理解为什么它会在控制台上打印两次。我不确定这是否是因为我在TableTest中称之为错误?或者我在别的地方做错了什么。我已经在IDE和命令行中运行了这个程序,但仍然遇到同样的问题 public class TableTest { public static void main(String[] args){ int getBegin, getEnd; System.out.print

我不知道为什么我的循环打印了两次。我创建了两个独立的类,无论出于什么原因,我都无法理解为什么它会在控制台上打印两次。我不确定这是否是因为我在TableTest中称之为错误?或者我在别的地方做错了什么。我已经在IDE和命令行中运行了这个程序,但仍然遇到同样的问题

public class TableTest {
public static void main(String[] args){


    int getBegin, getEnd;


        System.out.println("Enter a number to start with: ");
        Scanner input = new Scanner(System.in);
        getBegin = input.nextInt();
            while (getBegin < 0){
                System.out.println("Please enter a number greater than 0.");
                getBegin = input.nextInt();
            }
        System.out.println("Enter a number to end with: ");
        getEnd = input.nextInt();
            while (getEnd < 0 || getEnd < getBegin){
                System.out.println("Please enter a number greater than 0. Or greater than your first input.");
                getEnd = input.nextInt();
    }

        MultiplicationTable loop = new MultiplicationTable(getBegin, getEnd);

        loop.printTable(getBegin, getEnd);

    }
}





public class MultiplicationTable {

public MultiplicationTable(int begin, int end){
    printTable(begin,end);
}

void printTable(int begin, int end){

    System.out.println(String.format("%-10s %-10s %-10s", "Number", "Squared" , "Cubed"));

    for (int i = begin; i <= end; ++i){
        System.out.println(String.format("%-10s %-10s %-10s", i, i* i, i*i*i));
    }

}

}
公共类TableTest{
公共静态void main(字符串[]args){
int getBegin,getEnd;
System.out.println(“输入以“.”开头的数字);
扫描仪输入=新扫描仪(System.in);
getBegin=input.nextInt();
while(getBegin<0){
System.out.println(“请输入一个大于0的数字”);
getBegin=input.nextInt();
}
System.out.println(“输入一个以“.”结尾的数字);
getEnd=input.nextInt();
而(getEnd<0 | | getEnd对于(int i=begin;i乘法表中的构造函数调用
printTable
方法,然后在主方法中再次调用它乘法表中的构造函数调用
printTable
方法,然后在主方法中再次调用它
printTable
方法两次:

  • 乘法表的构造函数中

    公共乘法表(整数开始,整数结束){ 打印表(开始、结束); }

  • main
    方法中,在获取
    乘法表的实例后
    类:

    乘法表循环=新的乘法表(getBegin,getEnd); 打印表(getBegin,getEnd)


  • 因此,出于这些原因,它会执行两次
    printTable
    方法。

    您要调用两次
    printTable
    方法:

  • 乘法表的构造函数中

    公共乘法表(整数开始,整数结束){ 打印表(开始、结束); }

  • main
    方法中,在获取
    乘法表的实例后
    类:

    乘法表循环=新的乘法表(getBegin,getEnd); 打印表(getBegin,getEnd)


  • 因此,出于这些原因,它执行了两次
    printTable
    方法。

    ah!谢谢,我很困惑为什么会发生这种情况。啊!谢谢,我很困惑为什么会发生这种情况。