Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Java Programming > Java Forum > ช่วยดู Code หน่อยครับ พอดีจะใช้ If..else if กับค่า String ครับ



 

ช่วยดู Code หน่อยครับ พอดีจะใช้ If..else if กับค่า String ครับ

 



Topic : 114100



โพสกระทู้ ( 15 )
บทความ ( 0 )



สถานะออฟไลน์




คือ จากตัวโค้ด ผมจะประกาศ ตัวแปร String เอาไว้ 2ค่า คือ Year กับ Month เพื่อที่จะนำมาสร้าง ไฟล์ Excel (ผมจะกำหนดที่เซฟไฟล์ครับ)

แต่ เนื่องจากไม่เคยใช้ if มาก่อน เลยไม่ทราบว่า ผมจะดึงค่า String ที่อยู่ใน loop if มาใช้ยังไง
(ก็คือตั้งเงื่อนไขไว้ พอมันตรงกับเงื่อนไข ก็ให้มันประกาศค่า String มาใหม่อีกตัว ซึ่งเป็นชื่อที่เก็บไฟล์ Excel แต่จะดึง ค่า String ที่ประกาศใหม่มาใช้ยังไง)

หรือว่ามันทำไม่ได้ ทำได้แค่ System.out.println หรือว่ามีวิธีอื่นที่สามารถนำมาเช็คในลักษณะ if ได้ และ สามารถนำค่า String ที่ประกาศนั้นมาใช้ได้ด้วย ขอบคุณครับ

Code (Java)
String year = "2015";
String month = "January"

if(year == "+year+")
{
   if(month == "January"){
             String fileName = "c:\\สอบถาม\\Report\\"+year+"\\January\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }else if(month == "February"){
             String fileName = "c:\\สอบถาม\\Report\\"+year+"\\February\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }else if(month == "March"){
             String fileName = "c:\\สอบถาม\\Report\\"+year+"\\March\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }else if(month == "April"){
             String fileName = "c:\\สอบถาม\\Report\\"+year+"\\April\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
    }else if(month == "May"){
             String fileName = "c:\\สอบถาม\\Report\\"+year+"\\May\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }else if(month == "June"){
             String fileName = "c:\\สอบถาม\\Report\\"+year+"\\June\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }else if(month == "July"){
             String fileName = "c:\\สอบถาม\\Report\\"+year+"\\July\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }else if(month == "August"){
             String fileName = "c:\\สอบถาม\\Report\\"+year+"\\August\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }else if(month == "September"){
             String fileName = "c:\\สอบถาม\\Report\\"+year+"\\September\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }else if(month == "October"){
             String fileName = "c:\\สอบถาม\\Report\\"+year+"\\October\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }else if(month == "November"){
             String fileName = "c:\\สอบถาม\\Report\\"+year+"\\November\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }else if(month == "December"){
             String fileName = "c:\\สอบถาม\\Report\\"+year+"\\December\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }
          }
              else
                      {
                          System.out.println("Please,Input Year.");
                      }


คือจะเอามาใช้กับ บรรทัดนี้ แต่ไม่รู้ว่า จะไปเรียกยังไง หรือดึงค่าที่อยู่ใน if มายังไงครับ
Code (Java)
WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName));



ขอบคุณครับ



Tag : Java









ประวัติการแก้ไข
2015-01-30 10:57:24
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2015-01-30 10:54:55 By : joezaok123 View : 1264 Reply : 2
 

 

No. 1



โพสกระทู้ ( 9,559 )
บทความ ( 2 )



สถานะออฟไลน์


Code (Java)
String year = "2015";
String month = "January"

String fileName = "c:\\สอบถาม\\Report\\"+year+"\\"+month+"\\Report Inquire "+n+"_ID."+s+" "+month+" "+year+".xls";



แบบนี้น่าจะสั้นกว่านะครับ รูปแบบทุกอย่างเหมือนกัน ต่างกันแค่ชื่อ ปี เดือน






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-01-30 11:11:59 By : Chaidhanan
 


 

No. 2

Guest


ไม่ยากครับ เพียงแค่ประกาศตัวแปลไว้ด้านบน เพื่อให้มันเห็นได้ครอบคลุมใน Function จากนั้นก็เรียกใช้ได้เลครับ

Code (C#)
String year = "2015";
String month = "January"
String fileName="";
if(year == "+year+")
{
   if(month == "January"){
             fileName = "c:\\สอบถาม\\Report\\"+year+"\\January\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }else if(month == "February"){
             fileName = "c:\\สอบถาม\\Report\\"+year+"\\February\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }else if(month == "March"){
             fileName = "c:\\สอบถาม\\Report\\"+year+"\\March\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }else if(month == "April"){
             fileName = "c:\\สอบถาม\\Report\\"+year+"\\April\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
    }else if(month == "May"){
             fileName = "c:\\สอบถาม\\Report\\"+year+"\\May\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }else if(month == "June"){
             fileName = "c:\\สอบถาม\\Report\\"+year+"\\June\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }else if(month == "July"){
             fileName = "c:\\สอบถาม\\Report\\"+year+"\\July\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }else if(month == "August"){
             fileName = "c:\\สอบถาม\\Report\\"+year+"\\August\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }else if(month == "September"){
             fileName = "c:\\สอบถาม\\Report\\"+year+"\\September\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }else if(month == "October"){
             fileName = "c:\\สอบถาม\\Report\\"+year+"\\October\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }else if(month == "November"){
             fileName = "c:\\สอบถาม\\Report\\"+year+"\\November\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }else if(month == "December"){
             fileName = "c:\\สอบถาม\\Report\\"+year+"\\December\\Report Inquire '"+n+"'_ID.'"+s+"' "+month+" "+year+".xls";
   }
          }
              else
                      {
                          System.out.println("Please,Input Year.");
                      }


// เรียกใช้บรรทัดนี้ได้เลย เพราะมันเป็นการประกาศให้ใช้ได้ทั้ง Function 
WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName));

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-02-04 17:23:38 By : thaidevelopment.net
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ช่วยดู Code หน่อยครับ พอดีจะใช้ If..else if กับค่า String ครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 00
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่