Java正在跳过最后一个变量

Java正在跳过最后一个变量,java,Java,我一直在尝试做一个基本的计算器,计算9个数字的平均值。问题是它总是跳过最后一行。 我的代码: /* To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package avarage.calc; im

我一直在尝试做一个基本的计算器,计算9个数字的平均值。问题是它总是跳过最后一行。 我的代码:

/* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package avarage.calc;
import java.util.Scanner;
/**
 *
 * @author taine
 */
public class AvarageCalc {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner input = new Scanner(System.in);
        double num1;
        double num2;
        double num3;
        double num4;
        double num5;
        double num6;
        double num7;
        double num8;
        double num9;
        double num10;
        double ans;
        System.out.print("Enter Number #1:");
        num1 = input.nextDouble();
        System.out.print("Enter Number #2:");
        num2 = input.nextDouble();
        System.out.print("Enter Number #3:");
        num3 = input.nextDouble();
        System.out.print("Enter Number #4:");
        num4 = input.nextDouble();
        System.out.print("Enter Number #5:");
        num5 = input.nextDouble();
        System.out.print("Enter Number #6:");
        num6 = input.nextDouble();
        System.out.print("Enter Number #7:");
        num7 = input.nextDouble();
        System.out.print("Enter Number #8:");
        num8 = input.nextDouble();
        System.out.print("Enter Number #9:");
        num9 = input.nextDouble();
        ans = num1 + num2 + num3 + num4 + num5 + num6 + num8 + num9;
        System.out.println("The Average of the numbers you gave is:" + ans / 9);
    }
}
程序运行时:

run:
Enter Number #1:20
Enter Number #2:20
Enter Number #3:20
Enter Number #4:20
Enter Number #5:20
Enter Number #6:20
Enter Number #7:20
Enter Number #8:20
Enter Number #9:20
The Average of the numbers you gave is:17.77777777777778
BUILD SUCCESSFUL (total time: 10 seconds)

您的平均值不正确,因为您的总和中缺少num7

ans = num1 + num2 + num3 + num4 + num5 + num6 + num8 + num9;
应该是

ans = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9;

您的平均值不正确,因为您的总和中缺少num7

ans = num1 + num2 + num3 + num4 + num5 + num6 + num8 + num9;
应该是

ans = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9;
  • 你有10个变量,但你计算的平均值是9。这是你想要的吗?无论如何,汤姆已经证明了你的错误。您忘记了
    num7
    变量
  • 代码行越少,出错的可能性就越小
  • 这样做可能会更好:

    double ans = 0;
    Scanner input = new Scanner(System.in);
    int x = 9 //Or some other number
    for(int i = 1; i <= x; i++){
        System.out.print("Enter Number #" + i);
        ans += input.nextDouble();
    }
    
    System.out.println("The Average of the numbers you gave is:" + ans / x);
    
    double-ans=0;
    扫描仪输入=新扫描仪(System.in);
    int x=9//或其他数字
    对于(int i=1;i
    
  • 你有10个变量,但你计算的平均值是9。这是你想要的吗?不管怎样,@汤姆已经证明了你的错误。你忘记了
    num7
    变量
  • 代码行越少,出错的可能性就越小
  • 这样做可能会更好:

    double ans = 0;
    Scanner input = new Scanner(System.in);
    int x = 9 //Or some other number
    for(int i = 1; i <= x; i++){
        System.out.print("Enter Number #" + i);
        ans += input.nextDouble();
    }
    
    System.out.println("The Average of the numbers you gave is:" + ans / x);
    
    double-ans=0;
    扫描仪输入=新扫描仪(System.in);
    int x=9//或其他数字
    
    对于(int i=1;我知道有一个名为“num10”的变量,我尝试过删除它,但它仍然不起作用我知道有一个名为“num10”的变量,我尝试过删除它,但它仍然不起作用谢谢。我刚刚开始学习Java,所以仍然在学习基础知识:)谢谢。我刚刚开始学习Java,所以仍然在学习基础知识:)