gpt4 book ai didi

java - 带计数器的乘法器,将答案显示为零

转载 作者:行者123 更新时间:2023-12-02 03:26:38 24 4
gpt4 key购买 nike

我正在尝试制作一个乘法表计数器。应该输出

1 x 5 是 52 x 5 是 103 x 5 是 1510 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);
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

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

最佳答案

无论你在做什么,你似乎都在乘以零。 Math math1 = new Math(0,0); 暗示着某些*0。您需要在代码中使用您的输入。正如阿瑟提到的使用,Math math1 = new Math(input, 0)

关于java - 带计数器的乘法器,将答案显示为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38778569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com