Search code examples
javabluej

Basic Java program wont run in Blue-J


So I am supposed to make a vending machine for my intro to programming class Lab. So far this is what my Java code looks like.

import java.util.Scanner;
public class VendingMachine
{
       public static void main (String[] args)
    {
 Scanner keyboard = new Scanner(System.in);

final int quarter = 25;
final int dime = 10;
final int nickel = 15;
int cost = keyboard.nextInt ();

int totalChange = 100 - cost;
int totalQuarters= totalChange/quarter;
totalChange = totalChange % quarter;
int totalDimes = totalChange/dime;
totalChange = totalChange % dime;
int totalNickels = totalChange/nickel;
totalChange = totalChange % nickel;

System.out.print("Enter Price for your item" + "30" );

 }
}

What I need it to do is this.

Enter a price for item (from 25 cents to a dollar, in 5-cent increments): 45


You bought and item for 45 cents and gave me a dollar, so your change is

 2 quarters,

 0 dimes, and

 1 nickels.

With the values 30 , 65, and 100. For some reason the program will not start up in Blue J..so I know Eclipse is recommended but I think I want to finish this lab with Blue J, anyone have any tips?


Solution

  • My guess is that the program is starting, but you don't realize it because it is waiting for input.

    Move the line containing System.out.print("Enter Price for your item" + "30" ); above your keyboard.nextInt scanning for input. Like so:

    System.out.print("Enter Price for your item" + "30" );
    int cost = keyboard.nextInt ();