Java 如何添加输入的总数,以及如何在for循环中执行if-else语句?
1.当用户输入数字时,如何添加分数。我不明白。它不断打印出4,而不是更高的分数。另外,我需要返回一个布尔答案,当4个分数大于32时,将打印elseif语句Java 如何添加输入的总数,以及如何在for循环中执行if-else语句?,java,for-loop,boolean,Java,For Loop,Boolean,1.当用户输入数字时,如何添加分数。我不明白。它不断打印出4,而不是更高的分数。另外,我需要返回一个布尔答案,当4个分数大于32时,将打印elseif语句 import java.util.Scanner; class forloops { public static void main (String[] param) { runnerscore(); System.exit(0); } //END main publi
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
import java.util.Scanner;
类forloops
{
公共静态void main(字符串[]参数)
{
runnerscore();
系统出口(0);
}//末端总管
公共静态无效runnerscore()
{
int score=1;//初始化保存在整个程序中的分数
int输入=0;
for(int i=0;i您从未实际存储输入的和
,而且分数将在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;
}
}
import java.util.Scanner;
课例{
//在此处保留扫描仪参考
扫描仪=新的扫描仪(System.in);
/**
*建造师
*/
公共示例(){
runnerScore();
}
/**
*主要方法
*
*@param-param
*/
公共静态void main(字符串[]参数){
新例子();
}
/**
*方法
*/
公共无效runnerScore(){
智力得分=1;
int-sumOfInputs=0;
//问题的循环
对于(int i=0;i整数
scannerInput=Integer.parseInt(scanner.nextLine());
//还它
返回扫描输入;
}
}
您从未实际存储输入的总和
而且分数将在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;
}
}
import java.util.Scanner;
课例{
//在此处保留扫描仪参考
扫描仪=新的扫描仪(System.in);
/**
*建造师
*/
公共示例(){
runnerScore();
}
/**
*主要方法
*
*@param-param
*/
公共静态void main(字符串[]参数){
新例子();
}
/**
*方法
*/
公共无效runnerScore(){
智力得分=1;
int-sumOfInputs=0;
//问题的循环
对于(int i=0;i整数
scannerInput=Integer.parseInt(scanner.nextLine());
//还它
返回扫描输入;
}
}
input(score,input);
does?打印出方法input()中的内容使用for循环?并将int score、int input作为参数传递给第二个方法…您正将score
增加4倍。为什么您认为它会超过4倍?我认为我的问题不够清楚,只是很难在这里解释我的意思。我想做的是,在出现问题时添加用户输入的任何内容'System.out.println(“跑步者的残疾等级是多少”+分数+”?)如果分数是32,那么它会打印出else-If语句。用简单的英语,您希望运行for循环4次。如果4个输入的总和>=32,则首先输入If或else,然后输入第二个If?分数永远不会超过4…您认为输入(分数,输入);
会打印出方法输入()中的内容使用for循环?并将int score、int input作为参数传递给第二个方法…您正将score
增加4倍。为什么您认为它会超过4倍?我认为我的问题不够清楚,只是很难在这里解释我的意思。我想做的是,在出现问题时添加用户输入的任何内容'System.out.println(“跑步者的残疾等级是多少”+分数+”?)如果分数是32,那么它会打印出else If语句。用简单的英语,你想运行for循环4次。如果4个输入的总和>=32,则输入第一个If或else,然后输入第二个If?分数永远不会超过4…谢谢!我现在明白了,这很有意义。非常感谢much@Bhavi准备好了!现在它工作了…还问了这个问题(),以防您对我修改代码时发生的错误感兴趣)很好!我想知道,但不明白为什么会发生,谢谢!我现在明白了,这更有意义。非常感谢much@Bhavi准备好了!现在它工作了…还问了这个问题()如果您对我修改代码时发生的错误感兴趣)很好!我想知道,但不明白为什么会发生