简体   繁体   English

C编程For循环和计数器

[英]C programming For loop and counter

I'm trying to make a program that calculates how many states you need to win. 我正在尝试制作一个程序,计算您需要赢得多少个州。 I have the main part of it done but now I'm trying to make a FOR loop that basically checks if the sum of how many votes you have now plus the number of votes the state has is greater than 270, the number to win. 我已经完成了它的主要部分,但是现在我正在尝试创建一个FOR循环,该循环基本上检查您现在拥有多少张选票加上该州拥有的选票总数之和是否大于270,即获胜的数目。 After that I'm trying to print that you will win if you win this state. 之后,我尝试打印,如果您赢得此状态,您将获胜。 At the end it will count and print out how many ways you can win. 最后,它将计算并打印出您可以赢得多少种方式。 This what I have so far but it doesn't say that name of that States when I call them, and it also doesn't give me a 1, 2, or 3 number for how many ways you can win, just a large number that I'm not storing. 这是我到目前为止所拥有的,但是当我打电话给他们时并没有说那个国家的名字,也没有给我1、2或3个数字来说明您可以赢得多少方法,只是一个很大的数字我没有存储。

Can anyone help me? 谁能帮我?

#include <stdio.h>

int main(void){

    //Initialize your variables
    int current_votes, num_votes_1, num_votes_2, x;
    char name_state_1, name_state_2;


    //ask the user for the current number of electoral votes their candidate has
    printf("How many electoral votes has your candidate won?\n");
    scanf("%d", &current_votes);

    //Now ask for the first state in contention
    printf("What is the name of the first state in contention?\n");
    scanf("%s", &name_state_1);

    //now ask hiw many electoral votes the first state has
    printf("How many electoral votes does it have?\n");
    scanf("%d", &num_votes_1);

    //now ask for the second state in contention
    printf("What's the name of the second state in contention?\n");
    scanf("%s", &name_state_2);

    //now ask how many electoral votes the second state has
    printf("How many electoral votes does it have?\n");
    scanf("%d", &num_votes_2);

    //Now here's the formula that checks to see if you win in each state
    //and counts how many ways you can win
    for(x = 0; x < 3; x++) {
        if(current_votes + num_votes_1 >= 270);
            printf("Your candidate wins if he/she wins %s", &name_state_1);
            x++;
        if(current_votes + num_votes_2 >= 270);
            printf("Your candidate wins if he/she wins %s", &name_state_2);
            x++;
        if(current_votes + num_votes_1 + num_votes_2 >= 270);
            printf("your candidate wins if he/she wins %s", &name_state_1, &name_state_2);
            x++;

    }
    //now call to tell how many ways the candidate can win overall
    printf("your candidate can win %d ways", &x);
    return 0;

}

Your if contains empty statement 您的if包含空语句

if(current_votes + num_votes_1 >= 270);

Remove the ; 删除;

Also c is not like python, indentation does not make code part of the if block. 另外c不同于python,缩进不会使代码成为if块的一部分。

if(current_votes + num_votes_1 >= 270)
    printf("Your candidate wins if he/she wins %s", &name_state_1);
    x++;

will always execute x++. 将始终执行x ++。 Only the printf part is part of the if block. 只有printf部分是if块的一部分。 Use {} to enclose the code. 使用{}将代码括起来。

At the end, you are also printing the address of x which is why it is a big number. 最后,您还将打印x的地址,这就是为什么它很大的原因。 Finally, using x to count ways to win and also as your loop condition is not a good idea. 最后,使用x来计算获胜的方式以及循环条件并不是一个好主意。 The way I see it, you don't even need the loop. 从我的角度来看,您甚至不需要循环。

This seems sufficient: 这似乎足够:

x = 0;
if(current_votes + num_votes_1 >= 270);
{
    printf("Your candidate wins if he/she wins %s", &name_state_1);
    x++;
}

if(current_votes + num_votes_2 >= 270);
{
    printf("Your candidate wins if he/she wins %s", &name_state_2);
    x++;
}

if(current_votes + num_votes_1 + num_votes_2 >= 270);
{
    printf("your candidate wins if he/she wins %s", &name_state_1, &name_state_2);
    x++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM