gpt4 book ai didi

java - 编译错误: “Not a statement” - Equation inside a for loop

转载 作者:行者123 更新时间:2023-12-02 11:07:40 25 4
gpt4 key购买 nike

尝试执行此操作时,我收到“不是声明”:

quarter = roundedAmount / 25.0;
for (int i = 0; i < quarter; i++) {
roundedAmount - 25.0; //This line is the error in every loop.
}

该任务是制作一个程序,该程序从用户读取一个介于1到99之间的数字,将数字四舍五入到最接近的5,然后输出一个字符串,该字符串取决于将四舍五入的四分之一/角钱/镍加起来。我知道我可能无法将这样的方程式推到for循环中,但是我无法为自己的一生而想出另一种方法来完成我要尝试做的事情。也许如果我提供其余的类(class),你们会发现任何逻辑错误或其他错误。
import java.util.Scanner;
import java.util.InputMismatchException;
/*
* Recieves an int < 100 and > -1 from the user (to act as "money") and returns the appropriate "change" to the user -
* rounded to the nearest 5 and in the least amount of "coins" possible.
* <br><br>
* <b>Name: Riley Matchett<br>
* ID: 991367312<br>
* Date: 01/19/2015</b>
*/
public class MakeChange {
/*
*
* @param userAmount The user's input.
* @return The change required.
*/
public String calculate(int userAmount) {
double roundedAmount;
int quarter, dime, nickel;

roundedAmount = Math.round(userAmount/5)*5;
if (roundedAmount < userAmount) {
return "No change needed.";
}

quarter = roundedAmount / 25.0;
for (int i = 0; i < quarter; i++) {
roundedAmount - 25.0; //Error
}

dime = roundedAmount / 10.0;
for (int i = 0; i < dime; i++) {
roundedAmount - 10.0; //Error
}

nickel = roundedAmount / 5.0;
for (int i = 0; i < nickel; i++) {
roundedAmount - 5.0; //Error
}

String coins = userAmount + " cents requires: " + roundedAmount;
return coins;
}
/*
*
* @param args Unused
* @throws IlegalArgumentException If userValue is less than 0, or greater than 99.
*/
public static void main(String[] args) throws IllegalArgumentException {
Scanner s = new Scanner(System.in);
System.out.println("Please enter a positive integer from 0 - 99.");
try {
int userAmount = s.nextInt();
if (userAmount < 0 || userAmount > 99) {
throw new IllegalArgumentException("Integer must be greater than 0, and less than 99.");
}
} catch (InputMismatchException ime) {
System.err.println("Invalid input.");
}
}
}

我知道还有一些未完成的段,我现在关心的只是计算,一旦通过这里,我将处理String数组。

在此先感谢您的帮助!

最佳答案

这不是声明,因为它什么都不做。 Java中的语句必须调用方法或将值分配给变量。

您想说roundedAmount -= 25.0;吗?这会将减去25的结果分配给roundedAmount变量。

关于java - 编译错误: “Not a statement” - Equation inside a for loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28220768/

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