我的任务
我的任务是尝试用Java编写该程序。我已经编写了一个我认为应该可以运行的程序,但是无论我输入什么,因为它说每个人都在每种情况下都听到了谣言。我认为这与跳过我的while循环和稍后我写错其他东西有关...?我完全迷路了,因为我尝试对正在执行的内容和未失败的内容进行故障排除,所以我真的不知道如何解决它。我在尝试编码问题的方法如下。尽管我不确定是否在正确的礼节中掌握了所有内容,但我已经尝试了很多笔记来帮助任何试图帮助我的人。对不起,如果我搞砸了!
public class ProgrammingProblem1 {
public static void main(String[] args) {
int people = Integer.parseInt(args[0]) ;
//Obtains how many people not including Alice are at the party
boolean[] guests;
double averageheard = 0.0;
// This double will be used at the end to determine on average how many people heard the rumor.
double totalheard = 0.0;
// This double is used to help calculate how many people heard the rumor over all the iterations. Keeps track of the total people who knew the rumor throughout the permutations.
double percentsuccess = 0.0;
// This double will be used at the end to determine the percent of how many times the rumor was heard by everyone against how many loops there were.
double rumorsuccess = 0;
// This keeps track of how many times the rumor went all the way around.
double rumorfail = 0;
// This keeps track of how many time the rumor did not make it all the way around.
guests = new boolean[people];
//Fills the array with as many slots as there are people at the party. Guests is the array that stores if someone has heard the rumor or not.
int runtime = Integer.parseInt(args[1]);
// Guests is to figure out how many guests besides Alice are at the party and set them in an array, and Runtime is to figure out how many simulations you are meant to run.
if (people < 1 || runtime < 0){
//This is to check if the arguements were entered correctly.
System.out.println("You entered the arguements incorrectly. The amount of people at the party besides Alice must be at least two and the simulation must be run at least once.");
}else {
for ( int i = 0; i < runtime ; i++) {
// This is for doing however many iterations through are desired.
int heard = 0;
// This variable will be used at the end to determine if everyone has heard the rumor.
int current = 0 ;
// This ensures that we start with our first person ,"Bob", as the progintor of the rumor. Current is whoever is currently telling the rumor to someone else.
for (int l = 0; l < people; l++){
guests[l] = false; }
guests[0] = true ;
// This ensures that Bob already knows the rumor.
int next = (int)(Math.random() * people) ;
// This randomly selects the first person Bob asks about it. Next is the person we are telling the rumor to
while (current == next) {
// This makes sure that the guest we are doing isn't talking to themselves
next = (int)(Math.random() * people );
}
while ( !guests[next] ) {
// This makes the loop go on until the canidate the person it would tell has already heard it
guests[next] = true;
// This line makes whoever was just told the rumor now knows the rumor
int last = current;
// This keeps track of who the last person who said the rumor was
current = next;
// This is making the person we just told the rumor to our new rumor teller.
next = (int)(Math.random() * people);
// This finds a new person to be told the rumor
while (current == next || last == next ){
// This ensures that the person we tell the rumor to next will not be the person telling the rumor to or the person who told them the rumor.
next = (int)(Math.random() * people); }
}
for ( int j = 0; j < people; ++j) {
// This is to determine how many people heard the rumor before it was terminated.
if ( guests[j] == true){
heard = heard + 1;
}
}
if ( heard == people){
//This if statement will add a counter to rumorsuccess if every person was told the rumor, and to rumorfail if everyone didn't hear it.
rumorsuccess = rumorsuccess + 1;
}
else{
rumorfail = rumorfail + 1; }
totalheard = totalheard + heard;
//This is to tally up how many people heard the rumor in total.
}
percentsuccess = (rumorsuccess / (rumorsuccess + rumorfail)) * 100 ;
// This calculates the percent of times the rumor went all the way around to everyone
averageheard = (totalheard / runtime) ;
// This calculates the average amount of times the rumor made its way around
System.out.println("Steven Mikels 20782");
System.out.println("The amount of people in the room besides Alice are: " + people + ". The amount of times the simulation was run is: " + runtime);
System.out.println("The rumor was heard by everyone in the room " + percentsuccess + " percent of the time. The average amount of people who heard the rumor was: " + averageheard);
}
}
}
编辑1:我已更新代码以适应==相关错误的更新。现在,我遇到了一些新问题,尽管每个人成功的百分比似乎都没有效果,但是该代码可以计算出平均多少人正确地听到了声音。在命令行中正确输入“ 3”,然后再输入其他任何数字,将使100%的时间遍历所有人。不幸的是,输入的人数大于3意味着代码有0%的机会一路走来,这是错误的。此外,输入“ 2”作为第一个数字似乎会使程序停顿命令提示符。经过一些测试,变量rumorfail和rumorsuccess总是彼此相等。
编辑2:我相当确定我已经解决了问题;变量rumorfail和rumorsuccess必须加倍!向上或向下四舍五入,结果为0%或100%。不幸的是,我仍然遇到一个问题,即我的程序不允许两个人成为这样的人数,否则它会吓跑。我正在测试更多原因来说明为什么现在可能会出现这种情况,但由于其他问题已经解决,因此不希望人们在处理其他问题!奇怪的是,0正确执行了该语句并打印出输入了无效数字,但是1遇到了与2相同的问题。
答案 0 :(得分:1)
即时发现错别字:
你在声明
while ( guests[next] = false ) {
缺少“ =“
它应该这样写:guests[next] == false
或!guests[next]
您在if语句if ( guests[j] = true){
中也做了同样的事情
应该是guests[j] == true
之所以要加上“ =”,是因为“ ==”是比较运算符,而“ =”是要设置等于的运算符,因此,当您执行if(x=1)
时,您可以将x设置为等于等于1,而不是等于1;
否则,您的代码看起来像可以运行并计算,只需解决这些语法错误即可。