我试图以递归方式调用方法,直到获得所需的输出。但是,我想从另一个类调用一个方法并从该方法获取信息,以便在我的递归方法中使用它。例如,假设我有一个名为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.
}
}
答案 0 :(得分: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