2015-11-28 81 views
-3

我試圖遞歸調用一個方法,直到我獲得所需的輸出。但是,我想調用另一個類的方法並從該方法獲取信息,以便在遞歸方法中使用它。例如,假設我有稱爲Cake包含有關蛋糕的信息的父類,如它的麪糊(即量麪糊),與特定類型的含有面糊的唯一實例蛋糕的擴展類,我有另一個類叫做Bakery,我想在那裏製作正在訂購的蛋糕。我在Bakery稱爲createCake的方法,我想遞歸調用此方法,直到足夠的麪糊在鍋裏創建一個蛋糕。如果麪糊的量擴展的類是隨機產生的,我如何調用從類getBatter方法和捕獲有關連擊量的信息,以便使用它在我的遞歸方法創建的蛋糕嗎?任何人都可以幫我解決這個問題嗎?我正在做類似這樣的事情,但我不太瞭解如何實際獲取信息以便使遞歸工作。我有一個下面代碼的例子,這樣你就可以知道我想要做什麼(我知道它不是很準確)。任何幫助將不勝感激。如何遞歸調用方法?

import java.util.Random; 

public abstract class Cake 
{ 
    static Random gen = new Random(System.currentTimeMillis()); 

    public int type; //type of cake 
    public static int batter = gen.nextInt() * 3; //random amount of batter 

    public int getType() 
    { 
    return type; 
    } 

    public int getBatter() 
    { 
     return batter; 
    } 
} 


public class RedVelvet extends Cake 
{ 
    public int type; 
    public int batter = gen.nextInt(3)+6; //generates between 6-8 cups of batter inclusive 

    public int getType() 
    { 
    return 1; 
    } 
    public int getBatter() 
    { 
    return batter; 
    } 
} 

public class Chocolate extends Cake 
{ 
    public int type; 
    public int batter = gen.nextInt(3)+6; //generates between 6-8 cups of batter inclusive 

    public int getType() 
    { 
    return 2; 
    } 
    public int getBatter() 
    { 
    return batter; 
    } 
} 


public class Pound extends Cake 
{ 
    public int type; 
    public int batter = gen.nextInt(3)+6; 

    public int getType() 
    { 
    return 3; 
    } 
    public int getBatter() 
    { 
    return batter; 
    } 
} 

public class Bakery 
{ 
    import java.util.Scanner; 
    System.out.print("Enter desired size of cake to be baked (Must be at least 12):"); 
    desiredSize=scan.nextInt(); 

    public static void createCake(int desiredSize, int currentSize) //currentSize is the current amount of batter in the pan 
    { 
    if (currentSize == desiredSize) 
     return; 
    else if (currentSize < desiredSize) 
    { 
    //Recursively call createCake method so that batter continues to be added to the pan until there is enough to make the desired cake size. I want to get the batter information from one of the extended classes in order to add it to the cake. 
    } 
} 
+1

你的問題不是很清楚,請張貼一些代碼,即已創建 –

回答

0

這是學校或各種各樣的課程,因爲我個人不會走這條路線,但話又說回來這是我的意見。就像我知道烘焙過程中知道些什麼,我可以安全地告訴你......絕對沒有。有些人甚至可以說我的編程/編程技能,但是再次,我不是程序員,幾乎在所有編程環境中我都自學了教程,其中包括我現在已經忘記的大部分老程序。 :/

我認爲,當涉及到烤蛋糕(或大部分事情就此而言)必須建立某種形式的準確性,以避免浪費,我們都知道,成本浪費錢。我並不是非常相信,生成隨機數量的蛋糕麪糊對於你最有可能完成的嘗試來說足夠準確,但是再一次,你已經知道這一點。我注意到,在你的不同的蛋糕班裏,他們基本上都會產生一個從6到8的隨機數。如果他們都做同樣的事情,那他們爲什麼要這樣做?

我不相信你需要在所有的遞歸,而是一個從一個循環中調用簡單的方法,例如:

while (currentSize < desiredSize) { 
    currentSize+= getBatter(cake, desiredSize); 
} 

這裏是我會怎麼做這個,現在我道歉,如果你覺得這都是完全沒有意義的:

import java.util.Random; 
import java.util.Scanner; 

public class Bakery { 


    public static void main(String[] args) { 
     getBaking(); // :) 

    } 


    private static void getBaking(){ 
     Scanner scan = new Scanner(System.in); 

     String cakeType = ""; 
     while (cakeType.equals("")) { 
      System.out.print("Enter desired cake to be baked (Pound, Chocolate, " 
        + "RedVelvet) or\nenter 'exit' to quit: -> "); 
      cakeType = scan.nextLine(); 
      if (cakeType.isEmpty()) { 
       System.out.println("\nYou must supply the name of cake to bake or " 
         + "enter 'exit' to quit!\n"); 
      } 
      else if (cakeType.toLowerCase().equals("exit")) { System.exit(0); } 
      else if (!cakeType.toLowerCase().matches("(pound|chocolate|redvelvet)")) { 
       System.out.println("\nYou must supply the name of cake as shown above!\n"); 
       cakeType = ""; 
      } 

     } 

     int desiredSize = 0; 
     String size = ""; 
     while (size.equals("")) { 
      System.out.print("\nEnter desired size of cake to be baked (must be at " 
        + "least 12\"): -> "); 
      size = scan.nextLine(); 
      if (size.isEmpty()) { 
       System.out.println("\nYou must supply the size of cake to bake or " 
         + "enter 'exit' to quit!\n"); 
      } 
      else if (size.toLowerCase().equals("exit")) { System.exit(0); } 
      else if (Integer.valueOf(size.replace("\"","")) < 12) { 
       System.out.println("\nYou must supply a cake size of 12\" or greater!\n"); 
       size = ""; 
      } 
     } 
     desiredSize = Integer.valueOf(size.replace("\"","")); 

     createCake(cakeType, desiredSize, 0); 
    } 

    public static void createCake(String cake, int desiredSize, int currentSize) { 
     //currentSize is the current amount of batter in the pan 
     while (currentSize < desiredSize) { 
      currentSize+= getBatter(cake, desiredSize); 
      System.out.println(currentSize); 
     } 

     System.exit(0); // Done! - Quit! 
    } 

    public static int getBatter(String cake, int desiredSize) { 
     Random gen = new Random(System.currentTimeMillis()); 
     // Since the random generation for the batter amount is the 
     // same for all cake types according to your code we just 
     // need this: 
     int batterAmount = gen.nextInt(3)+6; 

     // But if we want to be more specific for each Cake Type 
     // then we can do it this way but first create the required 
     // batter equations for each cake type and remove the /* and 
     // */ from the code but first comment out (//) the batterAmount 
     // variable declaration above. 
     // NOTE: Both cake diameter AND 'Height' should play into the factor 
     // of how much batter is required unless of course all cakes made are 
     // of the same height. 
     /* 
     int batterAmount = 0; 
     switch (cake.toLowerCase()) { 
      case "pound": 
       batterAmount = //batter amount to make 12 inch cake + (this much batter for a 1 inch cake * (desiredSize - 12)); 
       break; 
      case "chocolate": 
       batterAmount = //batter amount to make 12 inch cake + (this much batter for a 1 inch cake * (desiredSize - 12)); 
       break; 
      case "redvelvet": 
       batterAmount = //batter amount to make 12 inch cake + (this much batter for a 1 inch cake * (desiredSize - 12)); 
       break; 
     } */ 

     return batterAmount; 
    } 
} 

哼,我也希望這有所幫助你或至少泛起了一點想法放入烤箱:P

+0

@DevilHnd嗨班,我認爲一個while循環將是這一個更好的解決辦法,但我卻不得不使用遞歸。我只是不確定如何去做,所以我只是想出了一個類似的問題,我正在做的。感謝您的幫助,雖然:)! – salmon1407