gpt4 book ai didi

java - 为什么我的基本谣言模拟程序不起作用?

转载 作者:行者123 更新时间:2023-11-30 10:05:57 25 4
gpt4 key购买 nike

关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。












想改进这个问题?将问题更新为 on-topic对于堆栈溢出。

2年前关闭。




Improve this question




我的任务
enter image description here
我的任务是尝试用 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 arguments were entered correctly.

System.out.println("You entered the arguments 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”作为第一个数字似乎会使程序停止命令提示符。经过一些测试,变量rumourfail 和rumoursuccess 似乎总是彼此相等。
编辑2:我相当确定我已经设法解决了我的问题;变量谣言失败和谣言成功必须是双倍的!它向上或向下舍入数字,导致 0% 或 100% 标记。不幸的是,我仍然有一个问题,我的程序不允许两个人成为人数,否则它会吓坏。我正在测试更多原因,说明为什么现在可能会出现这种情况,但不希望人们处理另一个问题,因为它已经解决了!奇怪的是,0 正确执行了语句并打印出输入了无效数字,但 1 与 2 存在相同的问题。

最佳答案

马上我看到几个错别字:
你的 while 语句while ( guests[next] = false ) {缺少一个“=”
应该这样写guests[next] == false!guests[next]你也在这个 if 语句中做了同样的事情 if ( guests[j] = true){应该是 guests[j] == true这个额外的“=”的原因是“==”是比较运算符,“=”是设置等于的运算符,所以当你做if(x=1)您检查 not x 是否可以设置为 1,而不是如果它等于 1;
否则你的代码看起来会运行和计算,只需修复这些语法错误。

关于java - 为什么我的基本谣言模拟程序不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54976482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com