2015-12-09 44 views
0

我必须编写一个程序,要求用户输入一个整数值。在每个值之后,如果用户想要继续该程序,则用户必须以“y”或“n”作出响应,并且用户输入的每个数字被表示为奇数或偶数。如何在一个do-while循环中找到平均值

我已经完成了一个do-while循环,但我对如何获得用户输入的值的平均值感到困惑。你如何得到所有输入数据的平均值?

这是到目前为止我的代码:

import java.util.Scanner; 
class ProgramTest { 
    public static void main(String[] args) { 
     String answer = ""; 

     do { 

      int num, count = 0; 
      Scanner scan = new Scanner(System. in); 
      System.out.print("Enter any number : "); 
      num = scan.nextInt(); 

      if ((num % 2) == 0) System.out.println(num + " is an even number."); 
      else System.out.println(num + " is an odd number"); 
      System.out.println("do you want to continue?"); 
      answer = scan.next(); 
      count++; 

     } while (answer.equals("y")); 

    } 
} 
+1

要计算平均值,您需要2件事:1.已输入数字的总和,2.已输入数字的数量。在用户输入“n”后,将它们都分开(检查是否计数!=)。 – syntagma

+0

平均值(这是我猜你的平均值)是数字的总和除以数字的数量。所以,只需在循环中累加这些值,并在用户不回答'“y”'时分开它们。 –

+0

您是否需要每一步之后的平均值,或者只有当您不再继续时才需要平均值? – EpicPandaForce

回答

0

为了计算平均值,你需要两件事情:所有数字的总和和计数参与平均值计算所有数字。

sumcount其中涉及在平均值计算需要是出do..while范围的,以便它们为已知在计算阶段。

我也参加了修理你的代码一点点

import java.util.Scanner; 

class ProgramTest { 

    public static void main(String[] args) { 
     Scanner scan = new Scanner(System. in); 
     int count = 0; 
     int sum = 0; 
     String answer = ""; 
     do { 
      System.out.print("Enter any number : "); 
      int num = scan.nextInt(); 

      boolean isEven = (num % 2 == 0); 

      System.out.println(num + " is an " + (isEven ? "even" : "odd") + " number."); 
      sum += num; 

      System.out.println("do you want to continue?"); 
      answer = scan.next(); 
      count++; 
     } while (answer.toLowerCase().equals("y")); 

     System.out.println("Average: " + (sum/count)); 
    } 
} 
+1

总数和计数超出范围 – Ezequiel

+0

谢谢,现在已修复。 –

+0

非常感谢你! – Aaron

2

从问题的自由貌似需要办理以下的事情,

  1. 没有添加机制添加到单个变量中。
  2. 把所有变量都从do ... while循环体...
  3. 根据需求创建附加变量。

看到这一切事情由我盖上下面的代码片段。

做一些事情同样,

String answer = ""; 
double sum = 0; // use for storing addition to all entered values.. 
int num, count = 0; 
Scanner scan = new Scanner(System.in); 

     do { 
      System.out.print("Enter any number : "); 
      num = scan.nextInt(); // getting input from user through console 
      sum = sum + num; // add every input number into sum-variable 
      if ((num % 2) == 0) System.out.println(num + " is an even number."); 
      else System.out.println(num + " is an odd number"); 
      System.out.println("do you want to continue?"); 
      answer = scan.next(); // ask for still want to repeat.. 
      count++; 

     } while (answer.equals("y")); 
System.out.println("Average is : " + sum + "/" + count + " = "+ (sum /count)); 
+1

你的变量'avg'实际上是一个'sum'。 – Alex

+0

是......你说得对。用户可以按照他们方便的方式给它。 –

+0

这并不能解释*为什么这个代码修复了这个问题。这只是说“复制此代码”。你应该先解释一下这个问题实际上是什么*首先*。 – Makoto

-2

改变这样的代码:

import java.util.Scanner; 
    class ProgramTest { 
     public static void main(String[] args) { 
      String answer = ""; 
      int avr =0; 
      int num, count = 0; 
      do { 

       Scanner scan = new Scanner(System. in); 
       System.out.print("Enter any number : "); 
       num = scan.nextInt(); 

       if ((num % 2) == 0) System.out.println(num + " is an even number."); 
       else System.out.println(num + " is an odd number"); 
       System.out.println("do you want to continue?"); 
       avr += num; 
       answer = scan.next(); 
       count++; 

      } while (answer.equals("y")); 
      avr = avr /count; 
    System.out.println("The avreage of value is:" + avr); 
     } 
    } 

AVR是平均水平。这意味着当你输入一个整数。我们添加num和avr。并在完成循环时。我们分数计数。像这样:

1-5-9-11

AVR = 1 + 5 + 9 + 11;

count = 4;

avr = avr/4;

+0

'count'超出范围 – Ezequiel

+0

它表示无法找到计数符号 – Aaron

+0

我改变了这一点。你有什么问题吗? – shayan