มีปัญหากับปัญหาการเรียกซ้ำที่แปลงสตริงเป็น Int

ดังนั้นปัญหาจึงเป็นดังนี้ คุณต้องสร้างเมธอดที่ใช้สตริงเป็นพารามิเตอร์ที่ประกอบด้วยตัวเลขทั้งหมด และคุณต้องนำสตริงนั้นมาแปลงเป็นจำนวนเต็ม คุณต้องใช้วิธีแก้ปัญหาแบบเรียกซ้ำเพื่อแก้ไขปัญหา

ฉันเขียนโค้ดต่อไปนี้แล้วและใช้งานได้ดีเมื่อฉันป้อน "1234" หรือ "138775" แต่ทันทีที่ฉันป้อนตัวเลขที่มี 0 ผลลัพธ์ที่ได้กลับแปลกๆ

100, 1001, 101 และ 12045 ส่งคืน 10, 11, 110 และ 1245 ตามลำดับ ตามที่กล่าวไว้ข้างต้น โค้ดใช้งานได้ดีเมื่อฉันส่งไปอย่างเช่น "1234" หรือ "14384" แต่ทันทีที่มีเลขศูนย์ ก็มีแนวโน้มที่จะลบเลขศูนย์นั้นออกไป

ฉันได้ลอง int ที่แตกต่างกันในการแปลงสตริงจากคลาส Integer เช่น parse(int) แต่นั่นก็ให้ผลลัพธ์เดียวกัน

/**
 * converts a string of numbers to an int
 * 
 * @param String str: original string of numbers
 * @return int recursiveStringInt: returns the int value of the string
 */
public static int recursiveStringInt(String str)
{
    if(str.length() == 1)
        return Integer.valueOf(str);
    else
    {
        return Integer.valueOf(str.substring(0,1) + recursiveStringInt(str.substring(1)));
    }
}

ขอบคุณสำหรับความช่วยเหลือของคุณ!

โปรดแจ้งให้เราทราบหากต้องการคำชี้แจงใด ๆ


person Joe Smith    schedule 30.10.2018    source แหล่งที่มา
comment
พิจารณา System.out.println(Integer.valueOf("0123"));   -  person Scary Wombat    schedule 30.10.2018
comment
ฉันคิดว่ารหัสของคุณควรจะแบ่งและใช้พลังของบางสิ่งบางอย่าง 10   -  person Scary Wombat    schedule 30.10.2018
comment
คุณควรถามคำถามประเภทนี้ในการทบทวนโค้ด ไม่ใช่ Stackoverflow ต   -  person Juniar    schedule 30.10.2018
comment
ผลลัพธ์แปลกๆคืออะไร? การรู้ว่าสามารถช่วยเราแก้ไขปัญหาได้   -  person Darien Springer    schedule 30.10.2018


คำตอบ (3)


วงเล็บของคุณค่อนข้างจะผิดกับสิ่งที่คุณกำลังพยายามทำ และตรรกะต้องได้รับการแก้ไข.. สิ่งที่คุณต้องการคือนำอักขระ ตัวสุดท้าย ของสตริงปัจจุบัน แปลงเป็นจำนวนเต็ม และเพิ่มลงในการแปลงด้านหน้าของสตริงคูณ 10

int recursiveStringInt(String str) {
    int length = str.length()
    if(length == 1)
        return Integer.valueOf(str);
    else
    {
        int temp = Integer.valueOf(str.substring(length-1)) + ( 10 * recursiveStringInt(str.substring(0,length-1)));
        return temp;
    }
}

กรณีเล็กๆ น้อยๆ ของ "8" ส่งผลให้มีเพียงบล็อกแรกที่ถูกดำเนินการ

กรณีถัดไปของ "83" ให้ผลลัพธ์เป็น temp = 3 + (10 * 8) = 83

กรณีถัดไปของ "103" ให้ผลลัพธ์เป็น temp = 3 + (10 * (0 + (10 * 1))) = 103

person billjamesdev    schedule 30.10.2018

ลองใช้สารละลายหารและยกกำลัง

public static void main(String[] args) {

    System.out.println(recursiveStringInt("12034", 0));

}

public static int recursiveStringInt(String str, int pow)
{
    if(str.length() < 1)
        return 0;
    else
    {
        int temp = Integer.valueOf(str.substring(str.length() -1)) 
                                                       * (int) Math.pow(10.0, 1.0 * pow);
        temp += recursiveStringInt(str.substring(0, str.length() -1), pow + 1);
        return temp;
    }
}
person Scary Wombat    schedule 30.10.2018
comment
ฉันพยายามแยกมันออกเพื่อให้ OP เข้าใจได้ง่ายขึ้นถึงแม้ว่าของคุณจะดีกว่าก็ตาม - person Scary Wombat; 30.10.2018
comment
ไม่มีปัญหาอะไรเลย - ต้องการกาแฟเพิ่ม ;-) - person Scary Wombat; 30.10.2018

เนื่องจากเมื่อคุณแยกวิเคราะห์สตริงย่อยเช่น 054 ถึง int มันจะกลายเป็น 54

ลองใช้รหัสนี้:-

public static int recursiveStringInt(String str) {
    return str.length() == 1
            ? Integer.valueOf(str)
            : (int) (Integer.parseInt(str.substring(0, 1)) * Math.pow(10, str.length() - 1)) + recursiveStringInt(str.substring(1));
}

ฉันใช้ตรรกะนี้: -

105 = 1*100 + 0*10 + 5*1

แก้ไข: หากคุณไม่เข้าใจตัวดำเนินการที่ประกอบด้วยสาม นี่คือเวอร์ชัน if-else:-

public static int recursiveStringInt(String str) {
    if (str.length() == 1) {
        return Integer.valueOf(str);
    } else {
        return (int) (Integer.parseInt(str.substring(0, 1)) * Math.pow(10, str.length() - 1)) + recursiveStringInt(str.substring(1));
    }
}
person Kartik    schedule 30.10.2018