【问题标题】:How to do add the total of the input and how do I do an if-else statement within a for loop?如何添加输入的总数以及如何在 for 循环中执行 if-else 语句?
【发布时间】:2016-12-01 20:36:44
【问题描述】:

1.如何在用户输入数字时添加分数。我不明白。它继续打印出 4 而不是更高的分数。另外,我需要返回一个布尔值,当 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

【问题讨论】:

  • 你认为input(score, input); 是做什么的?
  • 使用 for 循环打印出方法 input() 中的内容?并将 int 分数、int 输入作为参数传递给第二种方法...
  • 您将 score 增加了 4 次。为什么你认为它会超过 4 个?
  • 我认为我的问题不够清楚,在这里很难解释我的意思。我想要做的是,当问题'System.out.println(“跑步者的残疾等级是什么”+得分+“?”)时添加用户输入的任何内容;被问到。如果分数是 32,那么它打印出 else if 语句。
  • 嗯,用简单的英语,你想运行 for 循环 4 次。如果4个输入的总和> = 32,首先输入if,否则输入第二个if?分数永远不会超过 4...

标签: java for-loop boolean


【解决方案1】:

您实际上从未存储过sum of inputs,而且分数将在 4 处最大化,因为 for 循环将运行 4 次。最后看看你可能需要的代码:

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;

    }
}

【讨论】:

  • 谢谢!我现在明白了,这更有意义。
  • 非常感谢
  • @Bhavi 准备好了!现在它可以工作了......还问了这个问题(stackoverflow.com/questions/40921049/…),以防您对我修改代码时发生的错误感兴趣)
  • 不错的一个!我很想知道,但不明白为什么会这样
猜你喜欢
  • 1970-01-01
  • 2019-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-17
  • 2021-12-21
相关资源
最近更新 更多