รบกวนท่านเทพ java หน่อยครับ

เริ่มโดย don, 13 ตุลาคม 2011, 23:57:05

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

don

ตามรูปครับ อยากรู้วิธีทำ หลักการของมันน่ะครับ หรือ โปรแกรมที่ใกล้เคียงกัน มีเทพท่านไหนพอทราบบ้างครับ


FeeedEx

#1
อ้างถึงจาก: don ใน 13 ตุลาคม 2011, 23:57:05
ตามรูปครับ อยากรู้วิธีทำ หลักการของมันน่ะครับ หรือ โปรแกรมที่ใกล้เคียงกัน มีเทพท่านไหนพอทราบบ้างครับ



ใช้คำสั่ง ที่เป็น method string class ได้ไหมครับ
ข้อ 1  ลองหลายๆวิธี อิอิ
วิธีที่หนึ่ง ผมไม่แน่ใจว่า indexOf(string,start); จะได้ผลแค่ไหน แต่คิดว่าใช้ได้ลองใช้ตามนี้ครับ ใช้หาช่องว่าง ถ้าเจอช่องว่างให้อักขระ ก่อนหน้านี้ทั้งหมด เป็นคำๆนึงครับ ทำแบบนี้เรื่อยๆจนครบ (เก็บในอาเรย์ ) แล้วนำมาเทียบว่าใช่ and , the ไหม (วิธีนี้คือเหมือนกับว่าตัดคำแยกออกจากกันโดยใช้ช่องว่างเป็นหลักมาเก็บใน ตัวแปรแต่ละตัวครับ )
วิธีที่สอง  ใช้ substring ครับ หลักการเดียวกับวิธีที่หนึ่ง แต่อันนี้เราจะไล่ไปทีละตัวครับ ถ้าเจอช่องว่าง เมื่อไหร่ก็ให้ อักษรหน้านี้รวมกันเป็นคำๆนึงครับ

ส่วนข้อ 2 นี่ เรานำสตริงค์พวกนั้นมา ตัดย่อยแล้วเช็คทีละตัว  ด้วยคำสั่ง substring( , );  ประมาณนี้

:wanwan023:

don

อ้างถึงจาก: FeeedEx ใน 14 ตุลาคม 2011, 00:04:07
อ้างถึงจาก: don ใน 13 ตุลาคม 2011, 23:57:05
ตามรูปครับ อยากรู้วิธีทำ หลักการของมันน่ะครับ หรือ โปรแกรมที่ใกล้เคียงกัน มีเทพท่านไหนพอทราบบ้างครับ



ใช้คำสั่ง ที่เป็น method string class ได้ไหมครับ

compareTo()
equal
equalIgnoreCase

ส่วนข้อ 2 นี่ เรานำสตริงค์พวกนั้นมา ตัดย่อยแล้วเช็คทีละตัว  ด้วยคำสั่ง substring( , );  ประมาณนี้

:wanwan023:

ผมไม่มีความรู้เรื่องนี้เลยครับ พอจะมีโค้ดที่คล้ายๆ หรือ ตัวอย่างบ้างไหมครับ อยากศึกษา

gilbert

ใช่พวก stack ช่วยครับ

ใช้หลักการ push()  , pop()

นำมาใช้ได้ทั้ง 2 ข้อครับ

FeeedEx

อ้างถึงจาก: don ใน 14 ตุลาคม 2011, 00:08:07
อ้างถึงจาก: FeeedEx ใน 14 ตุลาคม 2011, 00:04:07
อ้างถึงจาก: don ใน 13 ตุลาคม 2011, 23:57:05
ตามรูปครับ อยากรู้วิธีทำ หลักการของมันน่ะครับ หรือ โปรแกรมที่ใกล้เคียงกัน มีเทพท่านไหนพอทราบบ้างครับ



ใช้คำสั่ง ที่เป็น method string class ได้ไหมครับ

compareTo()
equal
equalIgnoreCase

ส่วนข้อ 2 นี่ เรานำสตริงค์พวกนั้นมา ตัดย่อยแล้วเช็คทีละตัว  ด้วยคำสั่ง substring( , );  ประมาณนี้

:wanwan023:

ผมไม่มีความรู้เรื่องนี้เลยครับ พอจะมีโค้ดที่คล้ายๆ หรือ ตัวอย่างบ้างไหมครับ อยากศึกษา

จริง ๆแล้ว ที่ผมบอกนี่คือการเขียนโปรแกรมจาวาแบบ เบสิกเลยครับ มีในหนังสือทั่วๆไป : )

bonshington

#5
1 ใช้ regex
import java.util.regex.*;

String sample = "To be or not to be....";

String[] patterns = {"and", "the"};

for(int i= 0 ; i < patterns.count; i++){
  Pattern regex = Pattern.compile(patterns);
  int result = regex.split(sample).count();
}

2
string[] patterns = {"a", "e", "\s"...};


แต่ถ้าเป็น C# จะง่ายกว่านี้มาก
1
string sample = "To be or...";
string[] pattern1 = {"and", "the"};
char[] pattern2 = "aeiou ".ToCharArray();
int result1 = sample.Split(' ').Union(pattern1).Count();
int result2 = sample.ToCharArray().Intersect(pattern2).Count();

หรือถ้าอยากจะรู้ว่าแต่ละตัวมีเท่าไหร่ก็
var results = from vowel in pattern2
join alphabet in sample.ToCharArray()
on vowel equals alphabet
into each
select new {vowel, count = each.Count()};

iLhay

[direct=https://bangmod.cloud/wordpress-hosting/]Wordpress Hosting

[/direct]
[direct=https://bangmod.cloud/wordpress-hosting/]Wordpress Hosting[/direct] เริ่มต้นปีละ 790 บาท NVMe SSD เร็ว 9000MB/s เร็วกว่านี้ไม่มีอีกแล้ว
[direct=https://bangmod.cloud/cloud-server]Cloud Server[/direct] เริ่มต้นเพียงเดือนละ 159 บาท พร้อมใช้ภายใน 1 นาที ผ่านระบบอัตโนมัติมีทั้ง Linux / Windows / DirectAdmin
สอบถามข้อมูลและแจ้งปัญหา 02-105-4417 ตลอด 24 ชั่วโมง

oDarkIceo

#7
ผมขอบอก algorithm และหลักการในเบื้องต้นพอนะคับ

ข้อ 1 algorithm ของผมคือ
1.1 แบ่ง String (กลุ่มตัวอักษร) เป็น Word ซึ่งแบ่งโดย space โดยใช้ฟังช์ชั่น split()
1.2 วนลูปเชค
----------------------------------------------------------------------------------------------------
public class MyProject01
{
   public static void main(String args[])
   {
      String myStr = "To be or not to be, that is a question whether ' tis nobler in the mind to suffer the slings and arrow of outrageous fortune or to take arms against a sea of troubles, and by opposing end them.";
      String splitStr[] = myStr.split(" ");
      String myWord[] = {"and", "the"};
      int ctrWord = 0;

      for(int y=0 ; y<splitStr.length ; y++)
      {
         for(int z=0 ; z<myWord.length ; z++)
         {
            if(myWord[z].equals(splitStr[y]) == true)
               ctrWord++;
         }
      }
      System.out.println("Word \"and\", \"the\" = " + ctrWord);
   }
}

----------------------------------------------------------------------------------------------------

ข้อ 2 algorithm ของผมคือ
2.1 วนลูปเชค
----------------------------------------------------------------------------------------------------
public class MyProject02
{
   public static void main(String args[])
   {
      String myStr = "To be or not to be, that is a question whether ' tis nobler in the mind to suffer the slings and arrow of outrageous fortune or to take arms against a sea of troubles, and by opposing end them.";
      char myChar[] = {'a', 'e', 'i', 'o', 'u', ' '};
      int ctrChar = 0;

      for(int x=0 ; x<myStr.length() ; x++)
      {
         for(int y=0 ; y<myChar.length ; y++)
         {
            if(myStr.charAt(x) == myChar[y])
               ctrChar++;
         }
      }
      System.out.println("Char \'a\', \'e\', \'i\', \'o\', \'u\', \' \' = " + ctrChar);
   }
}

----------------------------------------------------------------------------------------------------

ps0. ฟังก์ชั่นไหนมันรับ input อะไรแล้วให้ output อะไร ส่วนนี้แนะนำให้ทดลองเองนะคับ