简体   繁体   English

如何在每种方法中使用对象数组?

[英]How can I use my object array at every method?

I'm still learning java. 我还在学习Java。 I'm working on object array. 我正在研究对象数组。 Hard topic for me. 对我来说很难。 Because i've never use OOP programming language so this is pushing me harder. 因为我从未使用过OOP编程语言,所以这使我更加努力。 I have a problem now. 我现在有问题。

this is my main code 这是我的主要代码

package deneme;
import java.util.Scanner;
public class Deneme {
static int index=0;
public static void main(String[] args) 
{           
    menu();  
}

public static void menu()
{
    int secim=0;
    Scanner menuSecim = new Scanner(System.in);
    System.out.println("Pazar Listesine Hoşgeliniz.");
    System.out.println("Menu");
    System.out.println("1. Listeyi Görüntüle");
    System.out.println("2. Listeye Ekle");
    System.out.println("3. Listeden Sil");
    System.out.println("4. Çıkış");
    System.out.print("Seçiminiz: ");
    secim = menuSecim.nextInt();
    if(secim==1)
    {

    }
    else if(secim==2)
    {//listeye ekleme
        int eklenilmekIstenen;
        Scanner sayi = new Scanner(System.in);
        System.out.print("Kaç Adet Ürün Eklemek İstiyorsunuz: ");
        eklenilmekIstenen=sayi.nextInt();
        listeyeEkle(eklenilmekIstenen);
    }

}
public static void listeyeEkle(int eklenecekSayisi)
{
    Scanner str = new Scanner(System.in);
    Scanner sayi = new Scanner(System.in);
    PazarListesi[] urun = new PazarListesi[300];
    for(int i=0; i<urun.length;i++)
    {
        urun[i] = new PazarListesi();
    }

    for(int j=index;j<(index+eklenecekSayisi);j++)
    {
        System.out.print((j+1)+". Ürünün Adı: ");
        urun[j].isim = str.nextLine();
        System.out.print((j+1)+". Ürünün Miktarı: ");
        urun[j].miktar = sayi.nextFloat();
        System.out.print((j+1)+". Ürünün Fiyatı(Kg Bazında): ");
        urun[j].fiyat = sayi.nextFloat();
    }
    index=index+eklenecekSayisi;
    System.out.println("Şu an ki İndex: "+index);
    for(int j=0;j<index;j++)
    {
        System.out.println((j+1)+". Ürünün Adı: "+urun[j].isim);
        System.out.println((j+1)+". Ürünün Miktarı: "+urun[j].miktar);
        System.out.println((j+1)+". Ürünün Fiyat: "+urun[j].fiyat);
    }
    menu();
}
public static void hepsiniGor()
{
    PazarListesi[] urun = new PazarListesi[300];
    for(int i=0; i<urun.length;i++)
    {
        urun[i] = new PazarListesi();
    }
    tutarHesapla();

}
public static void listedenSil()
{

}
public static void tutarHesapla()
{
    PazarListesi[] urun = new PazarListesi[300];
    for(int i=0; i<urun.length;i++)
    {
        urun[i] = new PazarListesi();            
    }
    for(int i=0;i<index;i++)
    {
        urun[i].tutar=urun[i].miktar*urun[i].fiyat;
    }
}

} }

this is the next object class 这是下一个对象类

package deneme;
import java.util.Scanner;
public class PazarListesi 
{
Deneme giris = new Deneme();
String isim;
float miktar;
float fiyat;
float tutar;

} }

I have to use this block in every method to access the object I created. 我必须在每种方法中都使用此块来访问我创建的对象。

    PazarListesi[] urun = new PazarListesi[300];
    for(int i=0; i<urun.length;i++)
    {
        urun[i] = new PazarListesi();            
    }

How can I access the object without writing this block? 如何在不编写此块的情况下访问对象?

I couldn't understand your question exactly but following code can help you I think. 我无法完全理解您的问题,但是以下代码可以为您提供帮助。 You can create methods to set values of array objects and get values of array objects. 您可以创建方法来设置数组对象的值并获取数组对象的值。

public class PazarListesi  {
    String isim;
    float miktar;
    float fiyat;
    float tutar;

    public String getPazarListesiIsim() {
        return this.isim;
    }

    public void setPazarListesiIsim(String paramString) {
        this.isim = paramString;
    }
//Add methods for other variables too
}

Based on the code you provided and what I believe I understand from your question, Try this: 根据您提供的代码以及我认为我从您的问题中了解的内容,请尝试以下操作:

public class Deneme {
    private static int index = 0;
    private static PazarListesi[] urun;

    public static void main(String[] args) {

        urun = new PazarListesi[300];

        for (int i = 0; i < urun.length; i++) {
            urun[i] = new PazarListesi();
        }

        menu();
    }

    public static void menu() {
        int secim = 0;
        Scanner menuSecim = new Scanner(System.in);

        System.out.println("Pazar Listesine Hoşgeliniz.");
        System.out.println("Menu");
        System.out.println("1. Listeyi Görüntüle");
        System.out.println("2. Listeye Ekle");
        System.out.println("3. Listeden Sil");
        System.out.println("4. Çıkış");
        System.out.print("Seçiminiz: ");
        secim = menuSecim.nextInt();

        if (secim == 1) {
            //
        } else if (secim == 2) {//listeye ekleme
            int eklenilmekIstenen;
            Scanner sayi = new Scanner(System.in);
            System.out.print("Kaç Adet Ürün Eklemek İstiyorsunuz: ");
            eklenilmekIstenen = sayi.nextInt();
            listeyeEkle(eklenilmekIstenen);
        }
    }

    public static void listeyeEkle(int eklenecekSayisi) {
        Scanner str = new Scanner(System.in);
        Scanner sayi = new Scanner(System.in);

        for (int j = index; j < (index + eklenecekSayisi); j++) {
            System.out.print((j + 1) + ". Ürünün Adı: ");
            urun[j].isim = str.nextLine();
            System.out.print((j + 1) + ". Ürünün Miktarı: ");
            urun[j].miktar = sayi.nextFloat();
            System.out.print((j + 1) + ". Ürünün Fiyatı(Kg Bazında): ");
            urun[j].fiyat = sayi.nextFloat();
        }

        index = index + eklenecekSayisi;
        System.out.println("Şu an ki İndex: " + index);

        for (int j = 0; j < index; j++) {
            System.out.println((j + 1) + ". Ürünün Adı: " + urun[j].isim);
            System.out.println((j + 1) + ". Ürünün Miktarı: " + urun[j].miktar);
            System.out.println((j + 1) + ". Ürünün Fiyat: " + urun[j].fiyat);
        }

        menu();
    }

    public static void hepsiniGor() {
        tutarHesapla();
    }

    public static void listedenSil() {
        //
    }

    public static void tutarHesapla() {
        for (int i = 0; i < index; i++) {
            urun[i].tutar = urun[i].miktar * urun[i].fiyat;
        }
    }
}

It extracts initialization of the model class into main , so you don't need to repeat it in every method. 它将模型类的初始化提取到main ,因此您不需要在每个方法中都重复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在每个对象上使用contains方法吗? - Can I use the contains method on every object? 如何使用片段方法? - How can I use my fragment method? 如果对象由多维数组保存,可以使用该方法吗? - Can I use the method if the object is held by an multidimensional array? 如何使用String数组中的用户输入来调用其他对象的方法? - How can I use user input in a String array to call a different object's method? 如何使用使用JOptionPane的构造函数将对象从我的方法传递到空数组中? - How can i use a constructor using JOptionPane to pass objects from my method into a empty array? 如何在Android上将自己的方法与HttpURLConnection对象一起使用? - How do I use my own method with an HttpURLConnection object on Android? 如何使用一种方法检查要添加到数组中的新元素是否尚未添加? - How can I use a method to check if the new element that I want to add to my array hasn't already been added? 如何在Java中存储在多态数组中的对象上调用方法? - How can I call a method on an object that is stored in a polymorphic array in Java? 如何在我的 getCell 方法中访问元胞数组? (爪哇) - How can I access the Cell Array in my getCell method? (Java) 我如何用 int 数组编写我的获胜方法 - How can i write my winning method with an int array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM