Results 1 to 8 of 8

Thread: Java Problem

  1. #1

    Java Problem

    Im new to programming and i have a question i cant get my head round.. can anyone help?
    here is the code ive done so far
    public class vend
    {
    public static void main(String[] args)
    {
    char group, selection;
    System.out.println("*****************************\n***Please make your choice***\n*****************************");
    do
    {
    System.out.println("[1] Get Gum ");
    System.out.println("[2] Get Chocolate ");
    System.out.println("[3] Get Popcorn ");
    System.out.println("[4] Get Juice ");
    System.out.println("[5] Display Items Sold ");
    System.out.print("Please Enter your selection : ");
    selection = EasyIn.getChar();
    System.out.println();
    switch(selection)
    {
    case '1' : System.out.println("Here is your Gum!");break;
    case '2' : System.out.println("Here is your Chocolate!");break;
    case '3' : System.out.println("Here is your Popcorn!");break;
    case '4' : System.out.println("Here is your Juice!");break;
    case '5' : System.out.println("Here is your selection : ");break;
    default: System.out.println("Error, options 1-5 only");
    }
    }while (selection != '5');
    System.out.print(selection);
    }
    }

    However i need to make it so when the user enters 5 the program gives them a list of the items they have selected so far and then exit. Please could someone help becouse i have no idea how to do this!!

  2. #2

    Java Problem

    Im new to programming and i have a question i cant get my head round.. can anyone help?
    here is the code ive done so far
    public class vend
    {
    public static void main(String[] args)
    {
    char group, selection;
    System.out.println("*****************************\n***Please make your choice***\n*****************************");
    do
    {
    System.out.println("[1] Get Gum ");
    System.out.println("[2] Get Chocolate ");
    System.out.println("[3] Get Popcorn ");
    System.out.println("[4] Get Juice ");
    System.out.println("[5] Display Items Sold ");
    System.out.print("Please Enter your selection : ");
    selection = EasyIn.getChar();
    System.out.println();
    switch(selection)
    {
    case '1' : System.out.println("Here is your Gum!");break;
    case '2' : System.out.println("Here is your Chocolate!");break;
    case '3' : System.out.println("Here is your Popcorn!");break;
    case '4' : System.out.println("Here is your Juice!");break;
    case '5' : System.out.println("Here is your selection : ");break;
    default: System.out.println("Error, options 1-5 only");
    }
    }while (selection != '5');
    System.out.print(selection);
    }
    }

    However i need to make it so when the user enters 5 the program gives them a list of the items they have selected so far and then exit. Please could someone help becouse i have no idea how to do this!!

  3. #3
    Hi mom!
    Join Date
    Aug 2001
    Posts
    1,103
    Well, you'd need something to hold the items that the customer bought. An array or vector will do. Add something to the vector when they select 1, 2, 3 and 4, and show each element in the vector if 5 is selected.

    oh, and you make a clean exit by adding this line (or something close, check the API):

    System.exit(0);
    I wish to express my gratitude to the people of Italy. Thank you for inventing pizza.

  4. #4
    Hi mom!
    Join Date
    Aug 2001
    Posts
    1,103
    Well, you'd need something to hold the items that the customer bought. An array or vector will do. Add something to the vector when they select 1, 2, 3 and 4, and show each element in the vector if 5 is selected.

    oh, and you make a clean exit by adding this line (or something close, check the API):

    System.exit(0);
    I wish to express my gratitude to the people of Italy. Thank you for inventing pizza.

  5. #5
    Senior Member
    Join Date
    Oct 2001
    Posts
    107
    ok I tried this at school so I don't have acccess to my compiler but you could try something like this. I know it may not be the most efficient and it is certainly not full proof.

    public class vend
    {
    public static void main(String[] args)
    {
    int [] num_array= new int [4]; //declaration of array and num of elements
    String [] food_array = new String [4]; // declaration of array and num of elements

    num_array[0]=0;
    num_array[1]=0;
    num_array[2]=0;
    num_array[3]=0;

    int size=4; //size of num_array for use in loop

    int i=0; // counter for for loop

    food_array[0]=gum;
    food_array[1]=choc;
    food_array[2]=pop;
    food_array[3]=juice;

    gum="gum";
    choc="chocolate";
    pop="popcorn";
    juice="juice";

    char group, selection;
    System.out.println("*****************************\n***Please make your

    choice***\n*****************************");
    do
    {
    System.out.println("[1] Get Gum ");
    System.out.println("[2] Get Chocolate ");
    System.out.println("[3] Get Popcorn ");
    System.out.println("[4] Get Juice ");
    System.out.println("[5] Display Items Sold ");
    System.out.println("[6] Exit");
    System.out.print("Please Enter your selection : ");
    selection = EasyIn.getChar();
    System.out.println();
    switch(selection)
    {
    case '1' : System.out.println("Here is your Gum!");
    num_array[0] + =1;
    break;
    case '2' : System.out.println("Here is your Chocolate!");
    num_array[1] +=1;
    break;
    case '3' : System.out.println("Here is your Popcorn!");
    num_array[2] +=1;
    break;
    case '4' : System.out.println("Here is your Juice!");
    num_array[3] +=1;
    break;
    case '5' : System.out.println("Here are your selections : ");
    for(i=0;i<size;i++)
    {
    System.out.print("\n"+food_array[i]);
    System.out.print("\t"+num_array[i]);
    }
    case '6' : System.exit(0);
    break;
    default: System.out.println("Error, options 1-6 only");
    }
    }while (selection != '6');
    System.out.print(selection);
    }
    }

  6. #6
    Senior Member
    Join Date
    Oct 2001
    Posts
    107
    ok I tried this at school so I don't have acccess to my compiler but you could try something like this. I know it may not be the most efficient and it is certainly not full proof.

    public class vend
    {
    public static void main(String[] args)
    {
    int [] num_array= new int [4]; //declaration of array and num of elements
    String [] food_array = new String [4]; // declaration of array and num of elements

    num_array[0]=0;
    num_array[1]=0;
    num_array[2]=0;
    num_array[3]=0;

    int size=4; //size of num_array for use in loop

    int i=0; // counter for for loop

    food_array[0]=gum;
    food_array[1]=choc;
    food_array[2]=pop;
    food_array[3]=juice;

    gum="gum";
    choc="chocolate";
    pop="popcorn";
    juice="juice";

    char group, selection;
    System.out.println("*****************************\n***Please make your

    choice***\n*****************************");
    do
    {
    System.out.println("[1] Get Gum ");
    System.out.println("[2] Get Chocolate ");
    System.out.println("[3] Get Popcorn ");
    System.out.println("[4] Get Juice ");
    System.out.println("[5] Display Items Sold ");
    System.out.println("[6] Exit");
    System.out.print("Please Enter your selection : ");
    selection = EasyIn.getChar();
    System.out.println();
    switch(selection)
    {
    case '1' : System.out.println("Here is your Gum!");
    num_array[0] + =1;
    break;
    case '2' : System.out.println("Here is your Chocolate!");
    num_array[1] +=1;
    break;
    case '3' : System.out.println("Here is your Popcorn!");
    num_array[2] +=1;
    break;
    case '4' : System.out.println("Here is your Juice!");
    num_array[3] +=1;
    break;
    case '5' : System.out.println("Here are your selections : ");
    for(i=0;i<size;i++)
    {
    System.out.print("\n"+food_array[i]);
    System.out.print("\t"+num_array[i]);
    }
    case '6' : System.exit(0);
    break;
    default: System.out.println("Error, options 1-6 only");
    }
    }while (selection != '6');
    System.out.print(selection);
    }
    }

  7. #7
    thanks owen
    thanks guus

    You were of help

  8. #8
    thanks owen
    thanks guus

    You were of help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •