久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何平均輸出小數?

How to output the decimal in average?(如何平均輸出小數?)
本文介紹了如何平均輸出小數?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在顯示平均小數時遇到問題.它一直顯示 0.00 或 0.1.我試著把它加倍,但我仍然得到相同的結果.另外,我想將我輸入的第一個整數包含在總和中,但我不知道如何.請幫助:

I have a problem in showing the decimals on the average. It keeps showing .00 or .1. I tried to put it in double, but I still get the same results.Also, I want to include the first integer that I input to the sum, but I have no idea how.Please help:

import java.io.*;
import java.util.*;

public class WhileSentinelSum 
{
static final int SENTINEL= -999;

public static void main(String[] args)
{
    // Keyboard Initialization
        Scanner kbin = new Scanner(System.in);

    //Variable Initialization and Declaration
        int number, counter;
        int sum =0;
        int average;

    //Input
        System.out.print("Enter an integer number: " );
        number = kbin.nextInt();
        counter=0;


    //Processing
    //Output
        while(number != SENTINEL)
        {
            counter++;
            System.out.print("Enter an  integer number: ");
            number = kbin.nextInt();
            sum += number;
        }

        if (counter !=0)
            System.out.println("
Counter is: " + counter);
        else
            System.out.println("no input");

        average= sum/counter;
        System.out.println("The sum is " + sum);
        System.out.println("The average is " + average);


        System.out.println();

    }//end main
}//end class

推薦答案

即使你聲明 averagedoublesumcounter 變量是 int,所以 Java 仍然使用 整數除法 在分配給 average 之前,例如5/10 結果是 0 而不是 0.5.

Even if you declared average to be double, the sum and counter variables are int, so Java still uses integer division before assigning it to average, e.g. 5 / 10 results in 0 instead of 0.5.

在除法之前將變量之一轉換為 double 以強制進行浮點運算:

Cast one of the variables to double before the division to force a floating point operation:

double average;

...
average = (double) sum / counter;

您可以通過在進入 while 循環之前對其進行處理來將第一個數字包含在計算中(如果它不是標記值).

You can include the first number in your calculation by processing it before you go into the while loop (if it's not the sentinel value).

這篇關于如何平均輸出小數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 欧美一区二区三区日韩 | 日日骚网 | 午夜精品一区二区三区在线视 | 91精品久久久久久综合五月天 | 成人在线观看亚洲 | 成人欧美一区二区三区在线观看 | 91欧美激情一区二区三区成人 | 欧美黑人又粗大 | 一区二区三区国产好 | 中文字幕在线一区二区三区 | 一区在线播放 | 国产精品国产精品 | 亚洲精品4 | 人人九九 | 亚洲精品福利视频 | a视频在线 | 黄瓜av| 成人免费观看男女羞羞视频 | 日本精品一区二区 | 中文在线а√在线8 | 爱综合 | 精品中文视频 | 人妖av| 日日夜夜精品视频 | 亚洲一区二区三区免费在线观看 | 国产高清一区二区三区 | 色在线免费 | 精品久久影院 | 国产伦精品一区二区三区精品视频 | h肉视频| 日韩中文字幕一区二区 | 国产偷自视频区视频 | 神马久久久久久久久久 | 福利国产 | 久久精品一区二区三区四区 | 日韩在线小视频 | 久久不卡 | 青娱乐自拍| 国产美女特级嫩嫩嫩bbb片 | 999久久久| 91精品久久久久 |