我对 Java 编程完全陌生。 我想使用我的数据输入(在本例中是我从“ATM”机器提取的钱),然后将其分开。
我试图输入:
System.out.println(witdraw/a);
但它说“对于参数类型 java.lang.String, int,运算符/未定义”
这是我的代码
package atm;
import java.util.Scanner;
public class ATM {
public static void main(String[] args) {
int a, b, c, d;
a = 20;
b = 10;
c = 5;
d = 1;
Scanner user_input = new Scanner(System.in);
String withdraw;
System.out.print("Put an amount of \u0024 to withdraw: ");
withdraw = user_input.next ();
System.out.println(withdraw / a);
System.out.println("Amount is \u0024 " + withdraw);
user_input.close();
}
}
我的目标是显示美元面额,用最少数量的钞票 $1、$5、$10、$20 总计该金额,其中美分可以保留为美分,但现在我只想知道如何使用我的输入在数学任务中,例如除法。
最佳答案
错误消息是一个很好的提示。你不能对字符串进行数学计算。您必须将其转换为数字。
您可以使用 Double.parseDouble(withdraw) 将其作为 double 进行处理,或者使用 Integer.parseInt(withdraw) 将其作为 int 进行处理。
所以你的 println 是:
System.out.println("Withdraw / a == " + Integer.parseInt(withdraw) / a);
关于java - 如何在数学任务中使用我的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54729878/