Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 循环赢了';不要重复只做一次,而不是重复做_Java - Fatal编程技术网

Java 循环赢了';不要重复只做一次,而不是重复做

Java 循环赢了';不要重复只做一次,而不是重复做,java,Java,我最近被分配了一个家庭作业,用另一种语言编写代码,这是我在python中使用过的唯一一种语言。我的代码应该做的是要求用户输入,然后首先将其设置为高、低,然后开始计算平均值。然而,我现在有两个问题。第一,我的第24行说字符串不能转换成整数。然而,我想我已经告诉程序允许它了。如果有人能帮助我,我将不胜感激 package hgp.pkg13.pkg16; import java.util.Scanner; public class HGP1316 { //Declaration of variab

我最近被分配了一个家庭作业,用另一种语言编写代码,这是我在python中使用过的唯一一种语言。我的代码应该做的是要求用户输入,然后首先将其设置为高、低,然后开始计算平均值。然而,我现在有两个问题。第一,我的第24行说字符串不能转换成整数。然而,我想我已经告诉程序允许它了。如果有人能帮助我,我将不胜感激

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
}


 }
}
}
}    
文件包hgp.pkg13.pkg16;
导入java.util.Scanner;
公共级HGP1316{
//变量声明
静态int运行_总计=0;
静态双平均=0.0;
静态int用户_输入=0;
静态双计数器=0.0;
静态int\u输入=-1;
静态int最低输入=-1;
公共静态void main(字符串[]args){
//介绍程序(可选)
{
System.out.println(“此程序将为您提供整数的最高、最低和平均值\n”
+“输入。若要结束,请输入负数”);
}
//2.要求用户输入一个正整数(或一个要停止的负整数)
//3.记录用户的输入
//4.测试输入>=0
while(用户输入>=0){
扫描仪用户输入=新扫描仪(System.in);
整数用户输入;
System.out.print(“请输入一个整数:”);
User_input=User_input.nextInt();
//4a.如果输入>=0为真
如果(用户输入>=0)
{
//4A1将输入添加到“运行总计”
运行总数=运行总数+用户输入;
//4a2.递增“输入计数器数量”
计数器=计数器+1;
//4a3.测试“输入计数器数量”是否等于1
如果(计数器==1)
{
//4a31.如果为真,则用输入替换“最高输入”和“最低输入”
最高输入=用户输入;
最低输入=用户输入;}
//4a5.如果输入>最高输入,则用输入替换“最高输入”
if(用户输入>最高输入)
{
最高输入=用户输入;
}
//4a6.如果输入<“最低输入”,则用输入替换“最低输入”
if(用户输入<最低输入)
{
最低输入=用户输入;
}
//4b.如果为假
//4b1.转到步骤5
其他的
{
//5.计算平均值(公式:“运行总数”/“输入计数器数”)
平均值=(运行总数/计数器);
//6.显示/传达“最高输入”、“最低输入”和平均值
System.out.println(“输入的最高值为:“+最高输入”);
System.out.println(“输入的最低值为:“+最低输入”);
System.out.println(“肠道平均数为:“+平均值”);
//7.终止
}
}
}
}
}    

我现在遇到的问题是,它会遍历整个循环,而不是重新搜索用户输入。这可能与我的{}有关,但我不确定您是否将用户输入作为
user\u input=user\u input.next()在java中,您需要使用
User\u input=User\u input.nextInt()。这会告诉程序需要的是整数输入,而不是另一个数据类型/字符串/参数


希望这有助于使用
user\u input.nextInt()
而不是
user\u input.next()
您的第二个问题是什么?这似乎是目前唯一的问题。还有,我的整数用户输入;它表示局部变量隐藏了一个字段。这是我应该担心的事吗。据我所知,这仅仅是因为我正在更改存储在User中的数字_input@pckofwolfs-这是因为您有类级别的静态用户输入和本地用户输入。在收到答案后,不要继续更改您的问题。那是非常糟糕的形式。如果你有一个新问题,发布一个新问题,不要重复使用现有的帖子。谢谢!!!!!!!!!!!!更多的代码进来一点。遇到更多的错误,但是,我试图在发布之前找到答案。没问题。如果您遇到任何其他错误-发布它们,我们将继续提供帮助。如果这回答了您最初的问题,请单击复选标记接受答案:)@pckofwolfadded to the code,然后再次点击另一个错误。我虽然定义了所有变量,但显然找不到符号@colt@pckofwolfs现在显示了哪些错误。我很乐意帮忙。也许可以用错误信息更新你原来的帖子?