2016-03-04 68 views
-3

我的任务需要我读取用户输入的号码并输出号码所生成的硬币。例如,如果用户输入“37”,程序应该回应(1季度,1毛钱和2便士)。我最有可能的代码根本没有任何意义,我不知道我需要做什么来解决它。需要帮助,使我的第一个Java代码。 (Coin Change)

import java.util.Scanner; 

public class Change 
{ 
    public static void main(String[] args) 
    { 
     Scanner sc = new Scanner (System.in); 
     Int n = sc.nextInt("Enter a positive integer"); 
     int number1, number2; // Division operands 
     int quotient;   // Result of division 


      if (QtrCnt > 0) 
       if (QtrCnt > 1) 
        System.out.println(QtrCnt + " quarters"); 
       else 
        System.out.println(QtrCnt + " quarter"); 
     } 

     if (DimeCnt > 0) 
     { 
      if (DimeCnt > 1) 
       System.out.println(DimeCnt + " dimes"); 
      else 
       System.out.println(DimeCnt + " dime"); 
     } 

     if (NicklCnt > 0) 
     { 
      if (NicklCnt > 1) 
       System.out.println(NicklCnt + " nickles"); 
      else 
       System.out.println(NicklCnt + " nickle"); 
     } 

     if (PennyCnt > 0); 
     { 
      if (PennyCnt > 1); 
       System.out.println(PennyCnt + " pennies"); 
      System.out.println(PennyCnt + " penny"); 
     } 

     int q = 25; 
     int d = 10; 
     int n = 5; 
     int p = 1; 

     if (a < 0); 
      System.out.println("ERROR"); 


      String (money >=25); { int numQuarters = money/ 25; } 
      money -= numQuarters * 25; 
      QtrCnt = (num1 - num1 % 25)/25; 
      num1 = num1 - QtrCnt * 25; 

      String(money >=10); { int numDimes = money/ 10; } 
      money -= numDimes * 10; 
      DimeCnt = (num1 - num1 % 10)/10; 
      num1 = num1 - DimeCnt * 10; 

      String (money >=5); { int numNickles = money/ 5; } 
      money -= numNickles * 5; 
      NicklCnt = (num1 - num1 % 5)/5; 
      num1 = num1 - NicklCnt * 5; 

      String (money >=1); { int numPennies = money/ 1; } 
      money -= numPennies * 1; 
      PennyCnt = (num1 - num1 % 1)/1; 
      num1 = num1 - PennyCnt * 1; 
     } 
    } 
} 

回答

0

鸿沟和地板(你或许应该重新开始)

public static int getQuarters(int cents) { 
    return Math.floor(cents/25.0); 
} 

这里是你如何做到这一点:

public static void main(String[] args) { 
    int cents = 46; // 46 just for an example 
    int left = cents; // a variable that represents how many cents are left 
    int quarters = getQuarters(cents); // how many quarters fit into 46 (1) 
    int left -= quarters * 25; // now we have 21 cents left we need to work out 
    int dimes = getDimes(left); // you can implement getDimes yourself (look at getQuarters). This will now return 2, since 2 dimes can go into 21 cents. 
    left -= dimes * 10; // we now have only 1 cent to account for 
    int nickels = getNickels(left) // Returns 0 (no nickels can fit into 1 cent) 
    left -= nickels * 5; // we still have 1 cent left 
    int pennies = left; // how many pennies are left over (always < 5) 
    System.out.println(cents + " cents = " + quarters + " Quarters, " + dimes + " Dimes, " + nickels + " Nickels, and " + pennies + " Pennies."); // print the output 
} 

请记住,包括getQuarters方法在Java类

示例:

  • getQuarters(25) -> 1
  • getQuarters(24) -> 0
  • getQuarters(49) -> 1
  • getQuarters(51) -> 2
+0

如何实现这一点。 (我是新的,认真不明白这一点) –

+0

我很惊讶任务会要求你在没有太多语言理解的情况下做到这一点 - 每当你使用getQuarters(美分)时,它会返回有多少宿舍可以“适合“这些美分。例如,'System.out.println(getQuarters(100))'输出“4”。我会更新答案,只需等待一分钟 – MCMastery

+0

因此,按照您刚才的方式进行操作,那就是代码的数学方面?剩下的只是输出? –