我对编码很新,而且我遇到了一些问题。
我需要创建一个程序,允许用户创建自己的变形虫群。其中一项要求是包括给予他们维生素和疾病。
维生素 - 维生素可以帮助我们的变形虫保持健康。询问您的用户是否想要给他们的菌落维生素。
疾病 - 不幸的是,所有宠物有时都会生病。当发生这种情况时,10%的变形虫死亡。对于每个会话:
如果不给予菌落维生素,它有25%的可能性生病。
如果殖民地给予维生素,它有20%的可能性生病。
我使用随机数生成器来获得疾病的百分比,但由于某种原因,我的if / then语句有一些wrog因为它永远不会使疾病等于真,它总是使它等于假而我拥有它设置为boolean并声明我的一些if / then语句使疾病等于false。
编辑:我列出了下面的代码而不是链接,我添加了luisluix提供的代码,现在似乎正在运行。谢谢!
我的代码如下:
import java.lang.Math;
import java.util.Random;
import javax.swing.JOptionPane;
public class AmoebaColony {
private String colonyName;
private String caretakerName;;
private String isItSick;
private int daysFed;
private int breedRequest;
private int breedSuccess;
private int rNumber;
private int requestVitamins;
private double startingSize;
private double colonySize;
private double finalSize;
private double died;
private boolean sickness;
private boolean vitamin;
public void setColonyProperties()
{
JOptionPane.showMessageDialog(null, "Welcome new caretaker, this program allows you to create, manage, and develope your own amoeba colony.",
"Welcome!", JOptionPane.INFORMATION_MESSAGE);
colonyName = JOptionPane.showInputDialog(null, "Please enter the name of your amoeba colony.",
"Creating Your Amoeba Colony", JOptionPane.INFORMATION_MESSAGE);
caretakerName = JOptionPane.showInputDialog(null, "Please enter the name of your amoeba colony's caretaker.",
"Creating Your Amoeba Colony", JOptionPane.INFORMATION_MESSAGE);
startingSize = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter the starting size of your amoeba colony.",
"Creating Your Amoeba Colony", JOptionPane.INFORMATION_MESSAGE));
}
public void setUserActions()
{
JOptionPane.showMessageDialog(null, "Congratulations, " + colonyName + " is starting out great with a totoal of " + startingSize + " amoebas.",
"Your New Amoeba Colony", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "Now that you've got your colony set up, you can do several things to it, including feeding, breeding, and giving it vitamins.",
"Developing Your Colony", JOptionPane.INFORMATION_MESSAGE);
daysFed = Integer.parseInt(JOptionPane.showInputDialog(null, "Feeding your colony allows it to breed, for each day fed, it can breed one time. How many days would you like to feed your colony?",
"Feeding Your Colony", JOptionPane.INFORMATION_MESSAGE));
breedRequest = Integer.parseInt(JOptionPane.showInputDialog(null, "Breeding allows you to increase the size of your colony. A colony must be fed in order to breed. \nYour colony size will double for each successful breed. How many time would you like your colony to breed?",
"Breeding Your Amoeba Colony", JOptionPane.INFORMATION_MESSAGE));
}
public void setVitamin()
{
requestVitamins = JOptionPane.showConfirmDialog(null, "Giving your colony vitamins decreases the chance of your amoebas getting sick and dying off. Would you like to give your colony vitamins?",
"Keeping Your Colony Healthy",JOptionPane.INFORMATION_MESSAGE, JOptionPane.YES_NO_OPTION);
}
public void vitaminResult()
{
if (requestVitamins == JOptionPane.YES_OPTION)
vitamin = true;
else
vitamin = false;
}
public void setBreeding()
{
if (daysFed >= breedRequest)
breedSuccess = breedRequest;
else
breedSuccess = daysFed;
colonySize = startingSize;
colonySize *= Math.pow(2, breedSuccess);
}
public void getSick()
{
Random number = new Random();
rNumber = number.nextInt(100);
if (vitamin == true)
{
if (rNumber <= 20)
sickness = true;
else
sickness = false;
}
else
{
if (rNumber <= 25)
sickness = true;
else
sickness = false;
}
}
public void sickEffect()
{
if (sickness == true)
finalSize = colonySize*.10;
else
finalSize = colonySize;
}
public void deadCount()
{
died = finalSize - colonySize;
}
public void setSickText()
{
if (sickness == true)
isItSick = "Yes";
else
isItSick = "No";
}
public void getFinalDialogOutput()
{
JOptionPane.showMessageDialog(null, rNumber + " All actions have been completed. \n\n Colony Name: " + colonyName + "\n Caretaker Name: " + caretakerName + "\n Starting Size: " + startingSize + "\n Number of of times fed in days: " + daysFed + "\n Number of times requested to breed: " + breedRequest + "\n Number of successful breeds: " + breedSuccess + "\n Sickness and death: " + isItSick + ", " + died + " died" + "\n Final size of colony: " + finalSize, "Amoeba Colony Summary", JOptionPane.INFORMATION_MESSAGE);
}
}
并在这里测试课程
public class AmoebaColonyTest {
public static void main(String[] args) {
AmoebaColony newColony = new AmoebaColony();
newColony.setColonyProperties();
newColony.setUserActions();
newColony.setVitamin();
newColony.setBreeding();
newColony.getSick();
newColony.sickEffect();
newColony.deadCount();
newColony.setSickText();
newColony.getFinalDialogOutput();
}
}
答案 0 :(得分:0)
假设它不是逻辑错误,我的猜测是总是使用大括号来确保语句是否正确执行。此外,你正在使用随机数字,你仍然有1/5的生病。确保你做多次迭代并打印rNumber。
if (vitamin == true && rNumber <= 20)
sickness = true;
else if (vitamin == false && rNumber <= 25)
sickness = true;
else
sickness = false;
尝试这样:
if (vitamin == true && rNumber <= 20){
sickness = true;
}
else if (vitamin == false && rNumber <= 25){
sickness = true;
}
else{
sickness = false;
}