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 > Java - Debugging การบ้าน ให้แก้โค้ดครับ รบกวนพี่ๆใจดี ช่วยเหลือหน่อยครับ



 

Java - Debugging การบ้าน ให้แก้โค้ดครับ รบกวนพี่ๆใจดี ช่วยเหลือหน่อยครับ

 



Topic : 102097



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



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




คือ ผม ต้องทำการบ้านส่งอะคับ ให้ Debug ตามโจทย์ข้างล่างอะครับ
ผมไม่มีความร้เรื่องนี้เลยจิงๆคับ รบกวนผู้ใจดีหน่อยได้ป่าวคับ

The program below reads the integers between 1 and 100 and counts the occurrences of each. The input ends with 0. Here is a sample run of the program:

Enter the integers between 1 and 100: <2, 5, 6, 5, 4, 3, 23, 43, 2, 0 is entered>

2 occurs 2 times
3 occurs 1 time
4 occurs 1 time
5 occurs 2 times
6 occurs 1 time
23 occurs 1 time
43 occurs 1 time
Issue:
However, if the integer entered is <1, 0 is entered>, then the output is:
1 occurs 0 time.
which is wrong, the correct output should be:
1 occurs 1 time.
Requirement:
Debug the program and fix this error. Show this to the trainer.
Program:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
Code (Java)
package occurence;
 
import java.util.Arrays;
import javax.swing.JOptionPane;
 
public class Occurence {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String sNumbers = "";
        int iNum = 0            
           ,iElementCount = 0   // number of elements
           ,iOccurence = 0;  // How many times the number occurs
               
        String sNum = JOptionPane.showInputDialog("Enter an Integer: ");
        iNum = Integer.parseInt(sNum); //Converts string to integer
       
        //Valid numbers
        while(!(iNum == 0))   // iNum not equal to zero
        {
            if ((iNum>=1)&&(iNum<=100))
            {
                sNumbers = sNumbers + iNum + ",";
                iElementCount++;
               
                sNum = JOptionPane.showInputDialog("Enter another Integer: ");
                iNum = Integer.parseInt(sNum); //Converts string to integer
                               
            }
            else if ((iNum<0)||(iNum>100))
            {
                sNum = JOptionPane.showInputDialog("Invalid Number. Enter another Integer: ");
                iNum = Integer.parseInt(sNum); //Converts string to integer           
            }
        }
       
        //zero
        if(iNum==0)
        {
            // if integer is between 1 to 100
            String[] sArray = new String[iElementCount];
           
            sArray = sNumbers.split(",");
           
            Arrays.sort(sArray);
           
            for(int index = 0; index<sArray.length; index++)
            {
                if(index == sArray.length-1)
                {
                    //last index
                    if(iOccurence > 1)
                    {
                        System.out.println(sArray[index]+" occurs "+iOccurence+" times.");
                    }
                    else
                    {
                        System.out.println(sArray[index]+" occurs "+iOccurence+" time.");
                    }                   
 
                    iOccurence = 1;
                }
                else
                {
                    //not the last index
                    if(sArray[index].equals(sArray[index+1]))
                    {
                        if(iOccurence==0)
                        {
                            iOccurence = iOccurence + 2;
                        }
                        else
                        {
                            iOccurence++;
                        }
                    }
                    else
                    {  
                        if(iOccurence > 1)
                        {
                            System.out.println(sArray[index]+" occurs "+iOccurence+" times.");
                        }
                        else
                        {
                            System.out.println(sArray[index]+" occurs "+iOccurence+" time.");
                        }
 
 
                        iOccurence = 1;
                    }                   
                }                               
            }  //for loop        
        }
    }
}




Tag : Java, JavaScript, JAVA







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2013-10-27 18:58:34 By : oceanprince View : 1448 Reply : 3
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

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

การบ้าน






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-10-28 06:23:59 By : mr.win
 


 

No. 2



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



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


T_T ทำไม่เป็นอะ คับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-10-29 03:50:28 By : oceanprince
 

 

No. 3



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



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


ตอบความคิดเห็นที่ : 1 เขียนโดย : mr.win เมื่อวันที่ 2013-10-28 06:23:59
รายละเอียดของการตอบ ::
.. พี่คับ อันนี้แก้ให้ผมละเหรอคับ

พี่พอจะอธิบาย คราวๆได้ป่าวคับ ว่า โค้ดนี้มันทำงานยังไงอะคับ พี่ดีอาจารย์จะถามด้วย T_T

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-10-29 04:17:08 By : oceanprince
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : Java - Debugging การบ้าน ให้แก้โค้ดครับ รบกวนพี่ๆใจดี ช่วยเหลือหน่อยครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ 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 04
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 อัตราราคา คลิกที่นี่