hgg ohh hgg ohh - 4 months ago 6
Java Question

Multiplier with counter, displays answers as zero

I'm trying to make a times table counter. It's supposed to output

1 x 5 is 5
2 x 5 is 10
3 x 5 is 15
up to 10 x 5 is 50

The input is the 5, the counter is taken from the i in the for loop.

It's counting through the numbers but I can't get it to calculate the result, and I can't see what I'm missing. Any help would be appreciated

Code below

import java.util.Scanner;
public class Program {

public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int input = kb.nextInt();
Math math1 = new Math(0,0);
for(int i = 0; i <= 10; i++){
math1.setNum2(i);
math1.multiplier();
System.out.println(input + " times " + i + " is " + math1.getResult());
}
} //main

} // class Program


and

public class Math {

private int num;
private int num2;
private int result;
//constructor//
public Math(int num, int num2){
this.num = num;
this.num2 = num2;
this.result = result;
}

//get//
public int getNum(){
return this.num;
}

public int getNum2(){
return this.num2;
}

public int getResult(){
return this.result;
}
//set//
public void setNum(int value){
this.num = value;
}

public void setNum2(int value){
this.num2 = value;
}
//other//
public void multiplier(){
this.num = num;
result = num * num2;
}
} // class Math

Answer

You seem to be multiplying times zero no matter what you are doing. Math math1 = new Math(0,0); implies something*0. You need to use your input in the code. As arthur mentioned use, Math math1 = new Math(input, 0)

Comments