Array encountered an error during Java personal exercises, but I don't know how to fix it.

Asked 1 years ago, Updated 1 years ago, 301 views

The top is the class for the main function (error) and

The classes below are for reference types.

I want to upload the captured image here, but it's not going up well, so if I were to write it,

Drinks = new Drinks [100]; there's a red line on the drinks on this side and on the drinks on the bottom.

When you put the mouse cursor on the drinks,

dirnks cannot be resolved to a varible 5quick fixes available Create local variable 'drinks' Create field 'drinks' Create parameter 'drinks' Remove assignment Change to 'Drink'

That's what it says. It worked well when I was writing the main function.

drinks[userChoice-1]!= null There was an error when writing this part (it may have occurred before this, but I couldn't check it)

Even if you erase this part, the red line remains.

I'd appreciate it if you could tell me the solution.

```package ex02;

import java.util.Scanner;

public class VendingMachineProgram {

// main thread
public static void main(String[] args) {

    boolean flag = true;
    final int END = 0;
    final int BUY = 1;
    final int MANAGE = 2;
    Scanner sc = new Scanner(System.in);
    int userChoice = -1;
    int 

    Drink[] drinks = new Drink[100];
    VendingMachine vendingMachine1 = new VendingMachine(1, drinks, 5_000);
    drinks[0] = new Pet("(Pet) Coca-Cola 500 ml", 1500, 20);
    drinks[1] = new Pet ("Pet) Coca-Cola Zero 500 ml", 1300, 20);
    drinks[2] = new Pet ("Pet) Pepsi Cola 500 ml", 1300, 20);
    drinks[3] = new Pet ("Pet) Pepsi Cola Zero Lime Sugar Fragrance 500 ml", 1300, 20);
    drinks[4] = new Pet("((Can) Coca-Cola 250 ml", 1000, 20);
    drinks[5] = new Pet ("(Can) Coca-Cola Zero 250 ml", 900, 20);
    drinks[6] = new Pet("(Can) Pepsi Cola 250ml",900,20);
    drinks[7] = new Pet("(Can) Pepsi Cola Zero Lime Sugar Fragrance 250 ml", 900, 20);
    drinks[8] = new tea ("milk coffee", 400, 100);
    drinks[9] = new tea ("luxury coffee", 500, 100);
    drinks[10] = new Tea ("Yulmucha", 400, 100);

    while (flag) {
        System.out.println("Welcome. Please select the purpose of use.");
        System.out.println ("1.Beverage Purchase\t2.Vending Machine Management\t0.Out");
        userChoice = sc.nextInt();
        if (userChoice == BUY) {
            showInfo(drinks);
            System.out.println ("Enter the drink number to purchase").
            userChoice = sc.nextInt();
            if(drinks[userChoice - 1] != null) {
                System.out.println("Select quantity to purchase");

            }else {
                System.out.println("You entered the wrong drink number.");
            }
        } } else if (userChoice == MANAGE) {

        } } else if (userChoice == END) {
            System.out.println("End of vending machine usage. Goodbye.");
            flag = false;
        } } else {
            System.out.println() entered incorrectly. Please re-enter ");
        }

    }//end of while

}// }// end of main

static void showInfo(Drink[] drinks) {
    for(int i = 0; i < drinks.length; i++) {
        if(drinks[i] != null) {
        System.out.println("***" + (i + 1) + "Burn Beverage**");
        drinks[i].showInfo();
        System.out.println("=====================");
        }
    }
}

}// end of class


```package ex02;

public abstract class Drink {

    private String name;
    private int price;

    public Drink(String name, int price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    protected abstract void showInfo();

}

package ex02;

public class Pet extends Drink {

    private int amount;

    public Pet(String name, int price, int amount) {
        super(name, price);
        this.amount = amount;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public void takeOut(int amount) {

        this.amount -= amount;
        System.out.println();
    }

    @Override
    public void showInfo() {
        System.out.println("음료명: " + this.getName());
        System.out.println("가격: " + this.getPrice());
        System.out.println("Amount remaining: " + amount + ");
    }

}

package ex02;

public class Can extends Drink {

    private int amount;

    public Can(String name, int price, int amount) {
        super(name, price);
        this.amount = amount;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public void takeOut(int amount) {

        this.amount -= amount;
        System.out.println();
    }

    public void showInfo() {
        System.out.println("음료명: " + this.getName());
        System.out.println("가격: " + this.getPrice());
        System.out.println("Amount remaining: " + amount + ");
    }

}
package ex02;

import java.util.Random;

public class Tea extends Drink {

    private int capacity;

    public Tea(String name, int price, int capacity) {
        super(name, price);
        this.capacity = capacity;
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    static int teaSpoon() {
        Random random = new Random();
        int teaSpoon = random.nextInt(3) + 4;
        return 0;
    }

    public void takeOut(int amount) {

        for (int i = 0; i < amount; i++) {
            this.capacity -= teaSpoon();
            System.out.println();
        }
    }
    @Override
    public void showInfo() {
        System.out.println("음료명: " + this.getName());
        System.out.println("가격: " + this.getPrice());
        System.out.println("Mix remaining: " + capacity + "g");
    }

}// }// end of class

package ex02;

public class VendingMachine {

    private int id;
    Private Drinks; // Drinks in it
    private int water; // Tea용 물
    private int money; // proceeds

    public VendingMachine(int id, Drink[] drinks, int water) {

        this.id = id;
        this.drinks = drinks;
        this.water = water;

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Drink[] getDrinks() {
        return drinks;
    }

    public void setDrinks(Drink[] drinks) {
        this.drinks = drinks;
    }

    public int getWater() {
        return water;
    }

    public void setWater(int water) {
        this.water = water;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public void showInfo() {
        System.out.println("Vending machine number: " + id);
        System.out.println("remaining bottled water:" + water + "ml";
        System.out.println("수익금: " + money);
        System.out.println("");
    }

}


java error

2023-02-12 19:49

1 Answers

If you look closely at the Vending Machine Program class, the grammar is incorrect.

int Drink[] drinks = new Drink[100]; You wrote this.
If you can't find where int is, look at the upper line a little bit. Only int is placed.


2023-02-12 21:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.