Java 编译错误:";不是一句话for循环中的方程
当我尝试这样做时,我得到的是“不是陈述”:Java 编译错误:";不是一句话for循环中的方程,java,for-loop,compiler-errors,Java,For Loop,Compiler Errors,当我尝试这样做时,我得到的是“不是陈述”: quarter = roundedAmount / 25.0; for (int i = 0; i < quarter; i++) { roundedAmount - 25.0; //This line is the error in every loop. } quarter=roundedAmount/25.0; 对于(int i=0;i
quarter = roundedAmount / 25.0;
for (int i = 0; i < quarter; i++) {
roundedAmount - 25.0; //This line is the error in every loop.
}
quarter=roundedAmount/25.0;
对于(int i=0;i<1/4;i++){
roundedAmount-25.0;//这一行是每个循环中的错误。
}
任务是制作一个程序,从用户那里读取1到99之间的数字,将数字舍入到最接近的5,然后根据舍入值的四分之一/一角/五分之和输出一个字符串。我知道我可能无法将这样的方程式推到for循环中,但我不能,以我的生命来说,想出另一种方法来完成我正在尝试的事情。也许如果我提供我班上的其他同学,你们可以发现任何逻辑错误或诸如此类的东西
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.");
}
}
}
import java.util.Scanner;
导入java.util.InputMismatchException;
/*
*从用户处接收int<100和>-1(充当“钱”),并将相应的“更改”返回给用户-
*四舍五入至最接近的5,并尽可能减少“硬币”数量。
*
*姓名:Riley Matchett
*ID:991367312
*日期:2015年1月19日
*/
公共阶级变革{
/*
*
*@param userAmount用户的输入。
*@返回所需的更改。
*/
公共字符串计算(int userAmount){
双圆形底座;
四分之一硬币、一角硬币、五分镍币;
roundedAmount=数学整数(userAmount/5)*5;
if(roundedAmount99){
抛出新的IllegalArgumentException(“整数必须大于0,小于99”);
}
}捕获(输入不匹配异常输入法){
System.err.println(“无效输入”);
}
}
}
我知道还有一些未完成的片段,我现在关心的只是计算,一旦我完成这里,我将处理字符串数组
提前感谢您的帮助 这不是一个声明,因为它什么都不做。java中的语句必须调用方法或为变量赋值
你想说
roundedAmount-=25.0代码>?这将把减去25的结果分配回roundedAmount变量。如果您只想将roundedAmount
减少25
,请使用以下习惯用法:
roundedAmount -= 25.0;
代码中的“非语句”相当于(roundedAmount-25.0)
的值
因此,这就像有一行代码说明42代码>您需要将值分配给某个对象roundedAmount-25.0是不够的。你认为roundedAmount-25.0代码>应该怎么做?你为什么这么认为?对Java语法的根本误解。符号的作用与您认为的不同。你需要仔细阅读变量赋值,我试过了。我试过roundedAmount=roundedAmount-25.0
和roundedAmount-=25.0
但我总是得到相同的错误。编辑:我不确定到底发生了什么,但我被难倒的原因是我确实试过-=就像我说的,但什么也没发生。我重新启动计算机是因为一些不相关的事情,现在可以正常工作了。@RileyMatchett我怀疑如果你在任何地方都不断地更改它,你会不会遇到同样的错误。您有多个不可编译的语句。我忘了提到我在发布之前尝试过,但我得到了相同的错误。如果您修复了所有编译错误,您将不再有编译错误:)