Java 带计数器的乘法器,将答案显示为零
我想做一个泰晤士报计数器。它应该是输出的 1乘5等于5 2乘5等于10 3乘5等于15 10 x 5等于50 输入为5,计数器取自for循环中的i 它在计算数字,但我不能让它计算结果,我也看不出我遗漏了什么。任何帮助都将不胜感激 代码如下Java 带计数器的乘法器,将答案显示为零,java,Java,我想做一个泰晤士报计数器。它应该是输出的 1乘5等于5 2乘5等于10 3乘5等于15 10 x 5等于50 输入为5,计数器取自for循环中的i 它在计算数字,但我不能让它计算结果,我也看不出我遗漏了什么。任何帮助都将不胜感激 代码如下 import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner kb = new Scanner(System.in)
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
无论你在做什么,你似乎都在乘以零<代码>数学数学1=新数学(0,0)代码>意味着某些东西*0。您需要在代码中使用您的输入。正如arthur提到的使用,
Math math1=new Math(input,0)
无论你在做什么,你似乎都在乘以零<代码>数学数学1=新数学(0,0)代码>意味着某些东西*0。您需要在代码中使用您的输入。正如arthur提到的那样,use,Math math1=new Math(输入,0)
您总是在乘以0。这就是你的结果
在main方法中更改代码,如下所示:
// use the input that you took
//let's take 5
Math math1 = new Math(0,0);
math1.setNum(input);
之后,确保在数学
课程中使用它
更新构造函数:
public Math(int num, int num2){
this.num = num;
this.num2 = num2;
}
result
与此无关
但是接下来的问题是,如何获得结果
为此,请将乘数方法更改为以下内容:
public void multiplier(){
this.result = num * num2;
}
你总是在乘以0。这就是你的结果 在main方法中更改代码,如下所示:
// use the input that you took
//let's take 5
Math math1 = new Math(0,0);
math1.setNum(input);
之后,确保在数学
课程中使用它
更新构造函数:
public Math(int num, int num2){
this.num = num;
this.num2 = num2;
}
result
与此无关
但是接下来的问题是,如何获得结果
为此,请将乘数方法更改为以下内容:
public void multiplier(){
this.result = num * num2;
}
你正在用0乘以一切。您可以看到传递到Math类对象中的参数 尝试以下几行:
Scanner in = new Scanner(System.in);
int number = in.nextInt(); // I suppose this is where the user enters the numbers say 5.
Math math = new Math(number, 0);
for(int i=1; i<=10; i++){
math.setNum2(i);
math.multiplier();
System.out.println(input + " times " + i + " is " + : math.getResult());
}
Scanner-in=新的扫描仪(System.in);
int number=in.nextInt();//我想这就是用户输入数字的地方,比如说5。
数学=新数学(数字,0);
对于(int i=1;i您将所有参数乘以0 mate。您将看到传递到Math类对象中的参数
尝试以下几行:
Scanner in = new Scanner(System.in);
int number = in.nextInt(); // I suppose this is where the user enters the numbers say 5.
Math math = new Math(number, 0);
for(int i=1; i<=10; i++){
math.setNum2(i);
math.multiplier();
System.out.println(input + " times " + i + " is " + : math.getResult());
}
Scanner-in=新的扫描仪(System.in);
int number=in.nextInt();//我想这就是用户输入数字的地方,比如说5。
数学=新数学(数字,0);
对于(int i=1;i您似乎将所有内容都乘以零。再次检查数学类的参数。永远不要使用input
。尝试math math1=new math(input,0)
this.result=result
(在构造函数中)而不使用result参数…似乎有点粗略…您需要调用setNum(input)在for循环之前,您似乎将所有内容都乘以零。再次检查math类的参数。永远不要使用input
。尝试math math1=new math(input,0)
(在构造函数中)而不使用result参数…似乎有点粗略…您需要调用setNum(input)在for循环之前