กำลังหัดใช้ array อยู่ครับ คือจะค้นหาข้อมูลที่มีอยู่แล้วใน array เทียบกับข้อมูลที่ป้อนเข้าไปใหม่จาก keyboard ว่ามันตรงกันครับ

ทีนี้ตัว subscript (พอดีไม่ทราบว่าภาษาไทยเรียกว่าอะไรอะครับ) ที่จะใช้ในการกำหนดว่าเป็นค่าไหนใน array เนี่ย มันเป็น int แล้วเราก็ต้องให้ค่ามันก่อนใช่มั้ยครับ พอผมให้ค่าเริ่มต้นไปเป็นศูนย์ มันก็เลยหาเจอแต่ตัวแรก หาตัวอื่นๆที่อยู่ใน array ไม่เจอครับ ไม่ทราบว่าต้องทำยังไงครับ โปรดชี้แนะด้วยครับ

Hiring! บริษัทที่น่าสนใจ

Carmen Software company cover
Carmen Software
Hotel Financial Solutions
Next Innovation (Thailand) Co., Ltd. company cover
Next Innovation (Thailand) Co., Ltd.
We are web design with consulting & engineering services driven the future stronger and flexibility.
KKP Dime company cover
KKP Dime
KKP Dime บริษัทในเครือเกียรตินาคินภัทร
Kiatnakin Phatra Financial Group company cover
Kiatnakin Phatra Financial Group
Financial Service
Fastwork Technologies company cover
Fastwork Technologies
Fastwork.co เว็บไซต์ที่รวบรวม ฟรีแลนซ์ มืออาชีพจากหลากหลายสายงานไว้ในที่เดียวกัน
Thoughtworks Thailand company cover
Thoughtworks Thailand
Thoughtworks เป็นบริษัทที่ปรึกษาด้านเทคโนโยลีระดับโลกที่คว้า Great Place to Work 3 ปีซ้อน
Iron Software company cover
Iron Software
Iron Software is an American company providing a suite of .NET libraries by engineer for engineers.
CLEVERSE company cover
CLEVERSE
Cleverse is a Venture Builder. Our team builds several tech companies.
Nipa Cloud company cover
Nipa Cloud
#1 OpenStack cloud provider in Thailand with our own data center and software platform.
Bangmod Enterprise company cover
Bangmod Enterprise
The leader in Cloud Server and Hosting in Thailand.
CIMB THAI Bank company cover
CIMB THAI Bank
MOVING FORWARD WITH YOU - CIMB is the leading ASEAN Bank
Bangkok Bank company cover
Bangkok Bank
Bangkok Bank is one of Southeast Asia's largest regional banks, a market leader in business banking
MuvMi (Urban Mobility Tech Co.,Ltd.) company cover
MuvMi (Urban Mobility Tech Co.,Ltd.)
Shape the future of urban mobility towards affordable, clean, and safe solutions
T.N. Digital Solution Co., Ltd. company cover
T.N. Digital Solution Co., Ltd.
TNDS has been involving in every first move of banking’s major digital transformation.
KBTG - KASIKORN Business-Technology Group company cover
KBTG - KASIKORN Business-Technology Group
KBTG - "The Technology Company for Digital Business Innovation"
Siam Commercial Bank Public Company Limited company cover
Siam Commercial Bank Public Company Limited
"Let's start a brighter career future together"
Icon Framework co.,Ltd. company cover
Icon Framework co.,Ltd.
Global Standard Platform for Real Estate แพลตฟอร์มสำหรับธุรกิจอสังหาริมทรัพย์ครบวงจร มาตรฐานระดับโลก
REFINITIV company cover
REFINITIV
The Financial and Risk business of Thomson Reuters is now Refinitiv
H LAB company cover
H LAB
Re-engineering healthcare systems through intelligent platforms and system design.
The Gang Technology Co., Ltd. company cover
The Gang Technology Co., Ltd.
We're a Digital Agency that helps our customers transform their business into digital with ease.
LTMH company cover
LTMH
LTMH มุ่งเน้นการพัฒนาผลิตภัณฑ์ที่สามารถช่วยพันธมิตรของเราให้บรรลุเป้าหมาย
Seven Peaks company cover
Seven Peaks
We Drive Digital Transformation
Wisesight (Thailand) Co., Ltd. company cover
Wisesight (Thailand) Co., Ltd.
The Best Choice For Handling Social Media · High Expertise in Social Data · Most Advanced and Secure
MOLOG Tech company cover
MOLOG Tech
We are Modern Logistic Platform, Specialize in WMS, OMS and TMS.
Data Wow Co.,Ltd company cover
Data Wow Co.,Ltd
We enable our clients to realize increased productivity by solving their most complex issues by Data
LINE Company Thailand company cover
LINE Company Thailand
LINE, the world's hottest mobile messaging platform, offers free text and voice messaging + Call
LINE MAN Wongnai company cover
LINE MAN Wongnai
Join our journey to becoming No.1 food platform in Thailand

ไม่ได้เขียนแบบจริงจังมาปีกว่าละ

เข้าใจถูกแล้วครับ ตำแหน่งในอาเรย์เป็น int ครับ สำหรับการเปรียบเทียบค่าจากคีย์บอร์ดให้ใช้ .equal เพราะใช้ == ในการเปรียบเทียบค่าไม่ได้ครับ (ไม่ทราบรายละเอียดว่าเพราะอะไร)

if(ตัวแปรรับจากคีย์บอร์ด.equal(อาเรย์[ตำแหน่ง]))

ที่เหลือก็ลูปตามปกติครับ อยากให้คิดอัลกิริทึมเองเพื่อให้เกิดการเรียนรู้+เสริมความจำด้วยครับ (จริงๆผมใช้ list นะรับค่าได้เรื่อยๆจนกว่าหน่วยความจำจะเต็ม)

billion Sat, 14/05/2011 - 15:34

ขอบคุณครับ แต่ว่าพอผมลองแล้วขึ้น error ว่า int cannot be dereferenced

ไม่ทราบว่าเกี่ยวด้วยรึป่าวว่า array ตัวที่ประกาศไปเป็น int อะครับ

ถ้าค่าใน array เป็น int และรับค่าจากคีย์บอร์ดแบบ nextInt มาก็ใช้ == เช็คได้เลยครับไม่ต้องผ่าน equal ( แต่ถ้ารับมาแบบอื่นก็ต้อง .equal เอาหน่ะครับ )

ใช้ JOptionPane ครับ ไม่ทราบว่าเหมือนกันกับ next int รึป่าว เข้าใจว่าคือ Scanner อะครับ ไม่แน่ใจว่าจะส่งผลด้วยรึป่าว

คือตอนนี้ผมเข้าใจว่าปัญหามันอยู่ตรงที่ว่า เพราะผมกำหนดค่าเริ่มต้นของ array index ให้เท่ากับศูนย์ มันก็เลยไม่ไปอ่านค่าอื่นๆใน array ไม่ทราบว่าเกี่ยวมั้ยครับ

หรือว่าจริงๆแล้ว เรากำหนดค่าเริ่มต้นของ array index ไปเป็นศูนย์แล้วก็จริง แต่ว่าโปรแกรมก็จะอ่านค่าต่อๆไปอยู่ดีครับ

billion Sat, 14/05/2011 - 16:11

import javax.swing.*;

public class areaCode

{

public static void main(String args[])
{

	int aCode, x;

	String areaString;

	int areaCodes[] = {219, 309, 425, 512, 630, 717, 870, 952, 815, 321}; 
	
	areaString = JOptionPane.showInputDialog("Enter area code: ");
	aCode = Integer.parseInt(areaString); 	
	
	while(aCode != 0)										
	{
		x = 0;
		if (aCode == areaCodes[x])
		{
			System.out.println("Area code found");
		}
		else
		{					
			System.out.println("Area code not found");
		}
		
		areaString = JOptionPane.showInputDialog("Enter area code or 0 to exit: ");
		aCode = Integer.parseInt(areaString); 
	}
}

}

x เท่ากับ 0 โดดๆเวลาเปรียบเทียบข้อมูลมันก็ชี้ index ตัวแรกอยู่อย่างนั้นล่ะ

for(int x=0;x<areaCodes.length;x++)
แล้วค่อยมา if คงเข้าใจนะ

จากโค้ดที่คุณให้มา ผมเอามาเขียนใหม่ได้แบบนี้ครับ ทำงานเหมือนกันทุกประการ

import javax.swing.*;

public class areaCode {
	public static void main(String args[]) {
		int aCode;

		String areaString;

		int areaCodes[] = { 219 };

		areaString = JOptionPane.showInputDialog("Enter area code: ");
		aCode = Integer.parseInt(areaString);

		while (aCode != 0) {
			if (aCode == areaCodes[0]) {
				System.out.println("Area code found");
			} else {
				System.out.println("Area code not found");
			}

			areaString = JOptionPane.showInputDialog("Enter area code or 0 to exit: ");
			aCode = Integer.parseInt(areaString);
		}
	}
}

ย่นลงไปอีกซักรอบก็จะได้แบบนี้

import javax.swing.*;

public class areaCode {
	public static void main(String args[]) {
		int aCode;

		String areaString;

		int areaCodes[] = { 219 };

		areaString = JOptionPane.showInputDialog("Enter area code: ");
		aCode = Integer.parseInt(areaString);

		while (aCode != 0) {
			if (aCode == 219) {
				System.out.println("Area code found");
			} else {
				System.out.println("Area code not found");
			}

			areaString = JOptionPane.showInputDialog("Enter area code or 0 to exit: ");
			aCode = Integer.parseInt(areaString);
		}
	}
}

ผมลองเอา code ไป compile ดูแล้วครับ ทำงานเหมือนกันทุกประการจริงครับ

คือผมติดตรงที่ว่ามี value มากกว่าหนึ่งจำนวน อยู่ใน array หน่ะครับ
ใน areaCodes[] มีอยู่สิบจำนวน

ทีนี้ตอนแรกผมทำให้โปรแกรมไปเทียบ array index ตัวอื่นๆไม่ได้หน่ะครับ

แต่ก็ขอบคุณนะครับ พอเห็น code ผมถึงเพิ่งรู้ว่ามาเอาใส่เลยตรงๆได้เลยด้วย

จุดประสงค์ที่ผมเขียนโค้ดสองอันนี้ให้คุณดู เพราะผมกำลังพยายามชี้โดยอ้อมๆ 2 สเต็ป ให้คุณเห็นว่าโค้ดคุณมีปัญหาที่ตรงไหน

การบอก solution ไปตรงๆ สิ่งที่คุณจะได้รับไปเต็มๆ คือ "คำตอบ" ครับ
แต่คุณอาจจะไม่ได้ "หัด" มากพอ ตามเจตนารมณ์ที่คุณตั้งกระทู้นี้ขึ้นมา

ดีใจด้วยครับ ที่ตอนนี้มันใช้ได้แล้ว

อ๋อ อย่างนี้นี่เอง เข้าใจแล้วครับ ขอบคุณครับ เหมือนกับว่าผมไม่ได้ใช้ array เลย แต่ใช้ค่าของตัวแรกอย่างเดียว ก็เลยเหมือนเอามาเทียบตรงๆก็เหมือนกันใช่มั้ยครับ
ตอนนี้จำได้ขึ้นใจเลยครับ เพราะก่อนจะมาตั้งกระทู้นั่งงงอยู่หลายชั่วโมงมาก

ไม่รู้ว่าผมเข้าใจถูกหรือเปล่านะครับ

แต่ Code นี้ มันไม่ได้เอาค่า Input มาเทียบกับทุกค่าใน Array นิครับ

เพราะทุกรอบของ while loop มันก็ไปรับค่า Input มาใหม่ ทั้งๆ ที่พึ่งเทียบกับค่าใน Array ไปค่าเดียว

ถ้าเรารู้ว่าโค้ดแต่ละส่วนทำงานยังไงแล้ว เราก็จับมายำรวมกันได้ครับ เอาโค้ดเดิม โค้ดใหม่ที่มีคนช่วย มารวมๆ กันก็ได้แล้ว (แต่ต้องเข้าใจด้วยว่าแต่ละส่วนมันทำอะไร)

คราวหน้าใช้ syntax hilighter ด้วยก็ดีครับ ^__^

{syntaxhighlighter brush:YourCodeLanguage;}
import tool

if(code):
  print(hilight(code))
else
  print(code)

def hilight(text):
  return tool.syntaxhilighter(text)
{/syntaxhighlighter}

จะได้ผลแบบนี้ออกมาครับ

(อย่าลืมเปลี่ยนค่าตรง YourCodeLanguage ให้เป็นภาษาที่ใช้นะครับ)
{syntaxhighlighter brush:python;}
import tool

if(code):
print(hilight(code))
else
print(code)

def hilight(text):
return tool.syntaxhilighter(text)
{/syntaxhighlighter}

เพิ่งทราบว่าทำงี้ได้ด้วย เจ๋งจริงๆ ขอทดสอบบ้างครับ

{syntaxhighlighter brush:java;}

while(aCode != 0)
{
found = false;
for(int x=0; x<areaCodes.length; x++)
{
if (aCode == areaCodes[x])
{
found = true;
}
}
}

{/syntaxhighlighter}

UltimaWeapon Sun, 15/05/2011 - 17:59

ทดสอบ
{syntaxhighlighter brush:cpp;}
int CWindowEx::MessageBox(const wchar_t *pszFormat, const wchar_t *pszCaption, UINT nType, ...)
{
va_list args;
int nRet;

// Make Argument
va_start(args, nType);
nRet = MessageBox(pszFormat, pszCaption, nType, args);
va_end(args);

return nRet;

}
{/syntaxhighlighter}

โค้ดงูเหลือมเหรอครับ ผมไม่เข้าใจ

ฮ่า ฮ่า แซวเล่นครับ พอดีตื่นสายอาเกรียนเลยกำเริบนิดหน่อย

ขอบคุณครับ ไม่ทันอ่าน คห คุณ neizod