编译错误:"不是声明" - for循环内的方程式

时间:2015-01-29 17:20:51

标签: 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.
    }

任务是制作一个程序,用户从1到99之间读取一个数字,将数字四舍五入到最接近的五个,然后输出一个字符串,这取决于四舍五入/一角硬币将加到四舍五入值。我知道我可能无法将这样的方程式推入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.");
    }
  }
}

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

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

这不是声明,因为它没有做任何事情。 java中的语句必须调用方法或为变量赋值。

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

答案 1 :(得分:1)

如果您只想将roundedAmount减少25,请使用以下内容:

roundedAmount -= 25.0;

代码中的“非声明”等同于(roundedAmount -25.0)

因此,就像有一行代码陈述42;