첫째 : 일반적인 관행은 switch 문으로 목록을 사용할 수 있나요? 이것은 1 (number one)
이 아니어야합니까? 또한 0 (zero)
이라면 어레이의 색인으로 사용할 수 있으므로 더 좋을 것입니다.
두 번째 것은 무엇입니까? int y = 0'?
y
은 프로그램의 어느 곳에서나 사용하지 않습니다. 이에
int y = 0;
for(int x = l; x <= 100; x++)
변경 :
for(int x = 0; x < 100; x++)
당신은 3 개 별도의 배열을 갖고 싶어하기 때문에
, 당신은 3 개 별도의 인덱스를 가지고 있어야 그들을 추적 할 수 있습니다.
이제 데이터를 저장할 어레이를 초기화하는 방법을 살펴 보겠습니다. 크기를 클래스 상단 (또는 main()
)의 변수로 선언해야 나중에 프로그램에서 쉽게 변경할 수 있습니다. 배열에서도 동일한 작업을 수행 할 수 있습니다. 수업 시간 내에 또는 로컬로 main()
안에 신고하십시오. 수업 외 두 가지 방법을 모두 선포하는 방법을 보여 드리지만 연습을 위해 다른 방법으로 시도해야합니다.
배열을 사용할 때 배열을 만들 자마자 미리 배열의 크기를 알아야하며 그 이후에는 크기를 변경할 수 없습니다. 이것은 많은 요소가 배열 안에 들어갈 수 있음을 의미합니다. 가변 크기 구조가 필요하다면 ArrayList
으로 응답을 확인하십시오. 당신이 이것을 달성 할 수
public class DataInsideArrays
{
public static DataInputStream a = new DataInputStream(System.in)
public static int size = 100; // now you only change this number and everything works
public static int[] productNumbers = new int[size];
public static String[] productNames = new String[size];
public static int[] productPrices = new int[size];
public static void main(String args[]) throws Exception
{
int prodnumIndex = 0; // index for tracking product numbers
int prodnameIndex = 0; // index for tracking product names
int prodpriceIndex = 0; // index for tracking product prices
for(int x = 0; x < size; x++)
{
// ... your code...
// ...
int prodNum = Integer.parseInt(a.readLine());
// check if we didn't reach our maximum size
if(prodnumIndex < productNumbers.length) {
productNumbers [prodnumIndex] = prodnum;
prodnumIndex++; // increment
} else {
System.out.println("Cannot add product number. Reached maximum amount.");
}
// ...
String prodName = a.readLine();
// check if we didn't reach our maximum size
if(prodnameIndex < productNames.length) {
productNames [prodnameIndex] = prodName ;
prodnameIndex++; // increment
} else {
System.out.println("Cannot add product name. Reached maximum amount.");
}
// ...
int prodPrice = Integer.parseInt(a.readLine());
// check if we didn't reach our maximum size
if(prodpriceIndex < productPrices.length) {
productPrices [prodpriceIndex] = prodPrice ;
prodpriceIndex++; // increment
} else {
System.out.println("Cannot add product number. Reached maximum amount.");
}
// ...
}
또 다른 방법은 당신이 필요로하는 속성을 포함하고 해당 객체의 배열을해야합니다 Product
라는 개체를 만드는 것입니다.
class Product {
int num;
String name;
int price;
Product(int num, String name, int price) {
this.num = num;
this.name = name;
this.price = price;
}
}
// ...
// declare your array of `Product` objects...
Product[] products = new Product[size];
// ...
// then you can do something like
System.out.println("Enter product number:");
int prodNum = Integer.parseInt(a.readLine());
System.out.println("Enter product name:");
String prodName = a.readLine();
System.out.println("Enter product price:");
int prodPrice = Integer.parseInt(a.readLine());
products[INDEX] = new Product(prodNum, prodName, prodPrice);
// ...
는 또한, 실제 제품의보다 현실적인 표현이기 때문에 제품 가격에 대한
double
float
(A)의 사용을 고려하십시오.
적절한/관련있는 질문에 태그를 답니다. 자바 질문처럼 보이지만 자바 스크립트에 태그를 추가했습니다. –
@ManjunathDR 죄송합니다. 바로 바 꾸었습니다. 감사. –
코드를 OCR하셨습니까? 구문 오류가 많습니다. – Narmer