数组帮助 - 如何在1个数组中存储多种数据类型 [英] Array help - how to store multiple data types in 1 array

查看:244
本文介绍了数组帮助 - 如何在1个数组中存储多种数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨所以我一直在尝试;



1.做一个数组,我将String,Double和整数存储在一个数组中。

2.我需要读取用户写入的输入并将它们存储在一个数组中(稍后再做一个获取信息的类)

3.自从一个数组,据我所知,只把一种数据放在数组中,我需要将所有其他数据类型转换为String ...我怎么能这样做...我已经尝试了很多次并且它说错误!!!!



我尝试过:



Hi so I have been trying to;

1. DO an array where I store both String, Double and integers in one array.
2. I need to read the input that user writes and store them in an array (later to do a class for getting the information)
3. Since an array, as far as i know, only put one type of data in array, I need to convert all other data types to String... how can i do it please... i have tried many times and its saying error!!!!

What I have tried:

import java.util.Scanner;

public class productDetails {

public static void enterProductDetails (){
      Scanner in = new Scanner (System.in);
      
       String idNo;
       String description;
       double cost;
       double price;
       int Quantity;
       int n;
       
       //array
       productDetails [] Parray = new productDetails[n];
       
      
       for (int i=0; i<n;i++){
           
        
      System.out.println("___________________");
      
      System.out.print("Enter product's unique identification number: ");
      Parray[i]= in.next();
      System.out.print("Enter product's description: ");
      Parray[i] = in.nextLine();
      System.out.print("Enter product's costing price: ");
      Parray[i] = in.parseDouble();
      System.out.print("Enter product's selling price: ");
      Parray[i] = in.toString();
      System.out.print("Enter quantity of the product: ");
      Parray[i] = in.parseInt();

    }
      //DO METHOD TO ASK WHETHER TO SAVE OR NOT
      
      //clears the screen and goes back to the menu
    
     }
    }

推荐答案

这与 https://www.codeproject.com/Questions/1268262/Trying-to-do-an-array -to-store-product-details-ent [ ^ ]并且您已经获得了建议的解决方案。



但是在上面的代码,您的类不包含任何属性。您的所有数据字段都是 enterProductDetails 方法的本地字段。我建议你去 Java™教程 [ ^ ]并研究关于类和对象的部分。
This is the same issue as https://www.codeproject.com/Questions/1268262/Trying-to-do-an-array-to-store-product-details-ent[^] and you have already been given a suggested solution.

But in your code above, your class does not contain any properties. All your data fields are local to the enterProductDetails method. I suggest you go to The Java™ Tutorials[^] and study the sections on classes and objects.


这篇关于数组帮助 - 如何在1个数组中存储多种数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆