gpt4 book ai didi

java - 循环在要求用户输入新输入之前执行所有操作,而不是仅仅按照应有的方式重新请求

转载 作者:太空宇宙 更新时间:2023-11-04 13:14:50 25 4
gpt4 key购买 nike

我最近被分配了一项作业,用不同的语言编写代码,这是我在 python 中使用的唯一语言。我的代码应该做的是要求用户输入,然后首先将其设置为高、低并开始计算平均值。

package hgp.pkg13.pkg16;

import java.util.Scanner;

public class HGP1316 {
//Declaration of variables
static int Running_total = 0;
static double Average = 0.0;
static int User_input = 0;
static double counter = 0.0 ;
static int Highest_input = -1;
static int Lowest_input = -1;

public static void main(String[] args) {
//Intro Program (Optional)
{
System.out.println("This program will give you the highest, lowest, and average of the integers you \n"
+ "enter. To end enter a negative number ");
}

//2. Ask user to enter a positive integer (or a negative integer to stop)
//3. Record the user's input
//4. Test the input >= 0
while (User_input >=0){
Scanner user_input = new Scanner( System.in );
Integer User_input;
System.out.print("Please enter an integer: ");
User_input = user_input.nextInt();
//4a. If it is true that input >= 0
if (User_input >= 0)
{

//4a1Add the input to "running total"
Running_total = Running_total + User_input;
//4a2. Increment "number of inputs counter"
counter = counter + 1;
//4a3. Test if "number of inputs counter" is equal to 1
if (counter == 1)
{
//4a31. If true, replace both "highest input" and "lowest input" with the input
Highest_input = User_input;
Lowest_input = User_input;
}

//4a5. If the input > "highest input" then replace "highest input" with input
if (User_input > Highest_input)
{
Highest_input = User_input;
}
//4a6. If the input < "lowest input" then replace "lowest input" with input
if (User_input < Lowest_input)
{
Lowest_input = User_input;
}
//4b. If false
//4b1. Goto step 5
else;
{
//5. Calculate average (formula: "running total" /"number of inputs counter" )
Average = (Running_total / counter);
//6. Display/communicate "highest input", "lowest input" and average
System.out.println ("The Highest value entered was : "+ Highest_input);
System.out.println ("The Lowest value entered was : " + Lowest_input);
System.out.println("The Average of enteries was : "+ Average);
//7. Terminate
}
}
}
}
}

我现在遇到的问题是它遍历整个循环而不是重新请求用户输入。这可能与我的 {} 有关,但我不确定。

最佳答案

由于此代码中存在大量有害的编码约定和最佳实践,因此我将忽略其中大部分并专注于您的案例中重要的内容:

  1. 你的代码永远不会跳出循环。这是因为您定义了static int User_input = 0;,然后在循环内定义了一个同名的变量,这会隐藏静态变量并存储用户输入。但是,在 while 条件中检查的是静态变量。
  2. 您没有在应该打印的地方打印。首先,您有 else; ,它基本上什么也不做,因为它包含一个空语句。之后,您始终执行循环的其余部分。

这是“固定”代码:

public class HGP1316 {
static int Running_total = 0;
static double Average = 0.0;
static int User_input = 0;
static double counter = 0.0 ;
static int Highest_input = -1;
static int Lowest_input = -1;
public static void main(String[] args) {
{
System.out.println("This program will give you the highest, lowest, and average of the integers you \n"
+ "enter. To end enter a negative number ");
}

Scanner user_input = new Scanner( System.in );
while (User_input >= 0) {
System.out.print("Please enter an integer: ");
User_input = user_input.nextInt();

if (User_input >= 0)
{
Running_total = Running_total + User_input;
counter = counter + 1;
if (counter == 1)
{
Highest_input = User_input;
Lowest_input = User_input;
}

if (User_input > Highest_input)
{
Highest_input = User_input;
}

if (User_input < Lowest_input)
{
Lowest_input = User_input;
}
} else {
Average = (Running_total / counter);
System.out.println ("The Highest value entered was : "+ Highest_input);
System.out.println ("The Lowest value entered was : " + Lowest_input);
System.out.println("The Average of enteries was : "+ Average);
}
}
user_input.close();
}
}

关于java - 循环在要求用户输入新输入之前执行所有操作,而不是仅仅按照应有的方式重新请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33639769/

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