high school java user

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PvtBillPilgrim
    New Member
    • May 2007
    • 19

    #1

    high school java user

    Hi. I'm completely new to Java, but I'm taking a course at a local university. I was wondering if someone could help me with this.

    I need to create a program that counts change (pennies, nickels, dimes, quarters).

    I've only been taking the class for about a week and am still very inexperienced when it comes to programming (and more importantly spotting errors). I modeled this program after a very similar one in my textbook. However, I cannot compile it using Dr. Java. Would someone mind helping me with some of the syntax errors I have made? Any help would be appreciated.


    import java.util.Scann er;

    public class CoinCounter {

    public static void main(String[] args) {

    // we define the variables that we need for our calculations
    int quarters = 0, dimes = 0.0;
    int nickels = 0; pennies = 0;
    int total, num;

    // we read in the values
    Scanner scan = new Scanner(System. in);

    System.out.prin tln("How many quarters do you have:");
    int quarters = scan.nextInt();
    System.out.prin tln("How many dimes do you have:");
    dimes = scan.nextInt();
    System.out.prin tln("How many nickels do you have:");
    nickels = scan.nextInt();
    System.out.prin tln("How many pennies do you have:");
    pennies = scan.nextDouble ();

    // we perform the calculations
    quarters + dimes + nickels + pennies = num;

    total = 25 * quarters;
    total = total + 10 * dimes;
    total = nickels * 5 + total;
    total = total + pennies;

    // print the result
    System.out.prin tln("The number of coins you have is "
    + num);
    System.out.prin tln("And they are worth in cents: " total);
    }
    }
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Three errors I see:

    1) In declaring your variables, the first line you say int quarters = 0, dimes = 0.0; This 0.0 is technically a double value (because of the decimal) and your compiler may complain about that.

    2) The next line of declaring variables, you write int nickels = 0; pennies = 0; - should the first ; be a , instead? As it is, you are trying to execute the statement pennies = 0, which doesn't work, since pennies has not been defined.

    3) When reading into pennies, you use scan.nextDouble () - but pennies should be an int. Use .nextInt() like the rest of the variables.

    Comment

    • PvtBillPilgrim
      New Member
      • May 2007
      • 19

      #3
      Thank you very much. I'll try and compile it again.

      Comment

      • PvtBillPilgrim
        New Member
        • May 2007
        • 19

        #4
        OK. I made 4 changes.
        I changed the dimes to just 0
        I changed the ; after nickels = 0 to ,
        I changed the scan.nextDouble for pennies to scan.nextInt
        I changed quarters + dimes + nickels + pennies = num; to num = quarters + dimes + nickels + pennies;

        I still get the following compiler error however:
        Line 35 "System.out.pri ntln("And they are worth in cents: " total);
        Error: ')' expected
        Could someone explain?


        import java.util.Scann er;

        public class CoinCounter {

        public static void main(String[] args) {

        // we define the variables that we need for our calculations
        int quarters = 0, dimes = 0;
        int nickels = 0, pennies = 0;
        int total, num;

        // we read in the values
        Scanner scan = new Scanner(System. in);

        System.out.prin tln("How many quarters do you have:");
        int quarters = scan.nextInt();
        System.out.prin tln("How many dimes do you have:");
        dimes = scan.nextInt();
        System.out.prin tln("How many nickels do you have:");
        nickels = scan.nextInt();
        System.out.prin tln("How many pennies do you have:");
        pennies = scan.nextInt();

        // we perform the calculations
        num = quarters + dimes + nickels + pennies;

        total = 25 * quarters;
        total = total + 10 * dimes;
        total = nickels * 5 + total;
        total = total + pennies;

        // print the result
        System.out.prin tln("The number of coins you have is "
        + num);
        System.out.prin tln("And they are worth in cents: " total);
        }
        }

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          In order to give two different types of information to the System.out.prin tln() function, you need to use the '+' operator e.g.

          Code:
          System.out.println("You have " + myIntVariable + " puppies.");

          Comment

          Working...