为 blackjack 程序返回 bufferdimage 的方法
Method returning bufferdimage for blackjack program
我一直在尝试为学校作业制作一个 21 点程序,但我一直坚持使用我的方法,该方法采用随机数并用它提取图片并将一个添加到数组中,我用它来确保不能有两张同一张卡。但是我的问题在于我想 return if 语句中的图像但它不允许我这样做,我也必须把它放在外面,如果语句不正确它 returns null 我想避免,而是让它重新运行该方法,直到选择了未拍摄的照片。感谢您的帮助,
public static BufferedImage ImportCards() {
String FileLocation;
Double Number;
Number = GetRandom(53);
int i = Number.intValue();
String num = String.valueOf(i);
String main = "DeckOfCards2//Card";
String end = ".png";
BufferedImage image = null;
if (myArray[i] == 0) {
myArray[i] = 1;
playerValue = i;
System.out.println("PLayerValue" + playerValue);
FileLocation = main + num + end;
File file = new File(FileLocation);
try {
image = ImageIO.read(file);
} catch (IOException e) {
System.out.println(e);
}
return image;
} else {
ImportCards();
}
return image;
}
为什么要递归调用同一个方法?这是任务吗?如果不是,这是一个非常糟糕的做法。为什么你需要精确的图像?您可以将该值作为 10♥ 或类似的值打印到控制台中。会更容易实施。
另外,为什么要读取卡值字符串?最好的方法是让 Card class 定义花色和价值,而不是阅读
字符串 num = String.valueOf(i);
没有递归的更好做法:
while (image == null ){
image = importCards();
}
但是如果你坚持使用递归,将方法的结果赋值给一个变量:
}else {
image = ImportCards();
}
return image;
我一直在尝试为学校作业制作一个 21 点程序,但我一直坚持使用我的方法,该方法采用随机数并用它提取图片并将一个添加到数组中,我用它来确保不能有两张同一张卡。但是我的问题在于我想 return if 语句中的图像但它不允许我这样做,我也必须把它放在外面,如果语句不正确它 returns null 我想避免,而是让它重新运行该方法,直到选择了未拍摄的照片。感谢您的帮助,
public static BufferedImage ImportCards() {
String FileLocation;
Double Number;
Number = GetRandom(53);
int i = Number.intValue();
String num = String.valueOf(i);
String main = "DeckOfCards2//Card";
String end = ".png";
BufferedImage image = null;
if (myArray[i] == 0) {
myArray[i] = 1;
playerValue = i;
System.out.println("PLayerValue" + playerValue);
FileLocation = main + num + end;
File file = new File(FileLocation);
try {
image = ImageIO.read(file);
} catch (IOException e) {
System.out.println(e);
}
return image;
} else {
ImportCards();
}
return image;
}
为什么要递归调用同一个方法?这是任务吗?如果不是,这是一个非常糟糕的做法。为什么你需要精确的图像?您可以将该值作为 10♥ 或类似的值打印到控制台中。会更容易实施。 另外,为什么要读取卡值字符串?最好的方法是让 Card class 定义花色和价值,而不是阅读 字符串 num = String.valueOf(i);
没有递归的更好做法:
while (image == null ){
image = importCards();
}
但是如果你坚持使用递归,将方法的结果赋值给一个变量:
}else {
image = ImportCards();
}
return image;