简体   繁体   English

如何添加输入的总数以及如何在for循环中执行if-else语句?

[英]How to do add the total of the input and how do I do an if-else statement within a for loop?

1.How do I add the score when the user inputs the number. 1.当用户输入数字时如何添加分数。 I don't understand. 我不明白。 It keeps on printing out 4 instead of a higher score. 它继续打印4而不是更高的分数。 Also, I need to return a boolean answer and when the 4 scores are more than 32 then the else if statement gets printed. 此外,我需要返回一个布尔答案,当4个分数超过32时,则会打印else if语句。

import java.util.Scanner;

class forloops
{
    public static void main (String[] param)
    {
        runnerscore();
        System.exit(0);

    }  //END main 

    public static void runnerscore()
    {
        int score = 1; // initialising the score that is kept throughout the program
        int input = 0;

        for (int i = 0; i<=3; i++) // for loop for the questions
        {

            input(score, input); // 2nd method
            score = score + 1;
        }

        if (score<=32) // boolean expression and have used if-else statement
        {
            System.out.println("The team has " + score + " points so is legal"); // prints out what the score is and if it is legal
        }
        else if (score >32)
        {
            System.out.println("The team has " + score + " points so is NOT legal"); // prints out if the score is not legal
        }
    }

    public static int input(int score, int input) 
    {
        Scanner scanner = new Scanner(System.in); //enables scanner that lets the user input
        System.out.println("What is the disability class of runner " + score + "?");
        input = Integer.parseInt(scanner.nextLine());  
        return input;
    }
}//END DisabilityRate

You never actually store the sum of inputs also the score will maximize at 4 cause the for loop will run 4 times . 你永远不会实际存储sum of inputssum of inputs分数也将最大化为4,因为for循环将运行4次。 Finally have a look at the code you may need: 最后看看你可能需要的代码:

import java.util.Scanner;

class Example {

    // Keep scanner reference here
    Scanner scanner = new Scanner(System.in);

    /**
     * Constructor
     */
    public Example() {
        runnerScore();
    }

    /**
     * Main method
     * 
     * @param param
     */
    public static void main(String[] param) {
        new Example();
    }

    /**
     * A method
     */
    public void runnerScore() {

        int score = 1;
        int sumOfInputs = 0;

        // for loop for the questions
        for (int i = 0; i <= 3; i++) {

            int input = input(score); // You need to save somewhere the input
                                        // you get from the user....
            System.out.println("The (" + i + ") time , input from  user was " + input);

            sumOfInputs += input; // sum every input you get

            ++score; // which is like using score = score + 1 or score+=1
        }

        // boolean expression and have used if-else statement
        if (sumOfInputs <= 32) {

            // prints out what the score is and if it is legal
            System.out.println("\nThe team has " + sumOfInputs + " points so is legal");

        } else if (sumOfInputs > 32) {

            // prints out if the score is not legal
            System.out.println("\nThe team has " + sumOfInputs + " points so is NOT legal");

        }
    }

    /**
     * Return the input from the user
     * 
     * @param score
     * @return
     */
    public int input(int score) {
        int scannerInput = 0;

        System.out.println("\nWhat is the disability class of runner " + score + "?");

        // String -> integer
        scannerInput = Integer.parseInt(scanner.nextLine());

        // Return it
        return scannerInput;

    }
}

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

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