我上周刚开始学习计算机科学,我们得到了一个名为 Coins 的工作表,我必须在其中找出一组硬币中有多少 25 美分硬币、5 美分硬币和几美分硬币。我遇到了很多麻烦,并且遇到了那个错误。这是我的代码
package Coins;
public class Coins
{
private int change;
// two contructors
Change() //default constructor
{
change = 94;
}
Change( int c )
{
change = c;
}
// accessor method - change
public int getChange()
{
return Change;
}
// mutator method - change
public void setChange( int anotherChange)
{
change = anotherChange;
}
public void askUserForChange()
{
Scanner keyIn;
keyIn = new Scanner(System.in);
System.out.print("Please enter the amount of change: ");
String input = keyIn.nextLine();
int nChange = Integer.parseInt (input);
setChange(nChange);
// change = nChange
printChangex();
}
// action method - take accessor figure out coins -> output
// calculating the coins needed for the change
public void printChangeRange(int start, int end)
{
for(int c = start; c <= end; c++
{
setChange(c);
printChangex();
}
}
public void printChangex()
{
int c = change;
int quarter = c / 25;
System.out.println("quarter = " + quarter);
int a = c%25;
int dime = a / 10;
System.out.println("dime = " + dime);
int b = a%10;
int nickel = b / 5;
System.out.println("nickel = " + nickel);
int c = b%5;
int penny = c / 1;
System.out.println("penny = " + penny);
}
// instance variables - replace the example below with your own
private int x;
public Coins()
{
// initialise instance variables
x = 0;
}
public int sampleMethod(int y)
{
// put your code here
return x + y;
}
}