我正在编写“老虎机”代码,并且不能在if语句中使用列表中多个项,因此可以为不同的项组合设置单独的语句。
尝试使用 如果rand_items中的“ item1”和“ item2”: 要么 如果rand_items中的“ item1”和“ item1”: 但它实际上只能拾取其中一个
public class PolarLineDemo extends ApplicationFrame {
public PolarLineDemo(String title) {
super(title);
final XYDataset dataset = getXYDataset();
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 500));
setContentPane(chartPanel);
}
private XYDataset getXYDataset() {
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series1 = new XYSeries("Series 1");
series1.add(20, 45);
series1.add(145, 120);
series1.add(90, 150);
dataset.addSeries(series1);
return dataset;
}
private JFreeChart createChart(final XYDataset dataset) {
JFreeChart chart = ChartFactory.createPolarChart("Polar Line Chart",dataset,true,true,false);
PolarPlot plot = (PolarPlot)chart.getPlot();
DefaultPolarItemRenderer renderer = (DefaultPolarItemRenderer) plot.getRenderer();
NumberAxis rangeAxis = (NumberAxis) plot.getAxis();
//set gridlines for category
plot.setAngleGridlinesVisible(true);
plot.setAngleLabelsVisible(true);
//set gridlines for series
plot.setRadiusGridlinesVisible(true);
plot.setRadiusMinorGridlinesVisible(false);
//series range setter
rangeAxis.setRange(0, 240.0);
//series label visible
rangeAxis.setTickLabelsVisible(true);
rangeAxis.setTickUnit(new NumberTickUnit(40.0));
//Marker shape visible
renderer.setShapesVisible(true);
//set marker shape, line color and width
Shape triangle = ShapeUtilities.createDownTriangle(5);
renderer.setSeriesShape(0, triangle, true);
renderer.setSeriesStroke(0, new BasicStroke(2));
//show series item labels -- This is not working.
renderer.setSeriesItemLabelsVisible(0, true);
return chart;
}
public static void main(String[] args) {
PolarLineDemo polarLineDemo = new PolarLineDemo("Polar Line Chart");
polarLineDemo.setSize(500, 500);
RefineryUtilities.centerFrameOnScreen(polarLineDemo);
polarLineDemo.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
polarLineDemo.setVisible(true);
}
}
应该带两个物品,如果是钻石和炸弹,请继续,或者如果有两个(超过1个)钻石“给”钱。
答案 0 :(得分:1)
您需要先检查"bomb"
和"diamond"
是否都在选项中,然后才能检查炸弹。我还建议使用set.issubset instead of multiple conditions in the if. Also making
项a
set`在这里也更有意义。
from random import choices
items = ["diamond", "bomb", "apple"]
rand_items = set(choices(items, k=3))
if {"bomb", "diamond"} <= rand_items:
print("You may spin again.")
elif "bomb" in rand_items:
print ("Game over.")
elif "diamond" in rand_items:
print("You won £25.")
您将需要一个else
,因为如果随机选择全部为"apple"
答案 1 :(得分:0)
请注意,此语法不正确:
elif "bomb" and "diamond" in rand_items:
正确的编写方式是:
elif "bomb" in rand_items and "diamond" in rand_items:
...其他条件相同。现在,要检查某项是否在列表中两次,可以将其计数:
elif rand_items.count("diamond") == 2: