当前位置: 首页 > 知识库问答 >
问题:

在Java中,我得到的不是除法而是两个数的加法

阎建华
2023-03-14

我是Android新手。我创建了一个Android应用程序来计算股票平均价格。

用户输入他们购买的股票和购买时的价格。

我使用下面的公式来实现这个结果。但是,当我没有划分平均价格时,逻辑给了我加法。

1.购买的股份总数=购买的股份(第一)购买的股份(第二)2。总购买金额=股票购买价格(第一)股票购买价格(第二)3。股票平均价格=总购买金额/总购买股份

一切都很好,但问题出在第三步。也许我错过了什么

这里的平均价格应该是7.5,但它显示了购买价格的增加。

MainActivity.java

package com.example.stockavg;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button button;
    private EditText Share1;
    private EditText Share2;
    private EditText Buy1;
    private EditText Buy2;
    private TextView AvgShare;
    private TextView AvgPrice;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);
        Share1 = findViewById(R.id.Share1);
        Share2 = findViewById(R.id.Share2);
        Buy1 = findViewById(R.id.buy1);
        Buy2 = findViewById(R.id.buy2);
        AvgShare = findViewById(R.id.avgshare);
        AvgPrice = findViewById(R.id.avgPrice);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Toast.makeText(MainActivity.this, "Button clicked", Toast.LENGTH_SHORT).show();
                String share1 = Share1.getText().toString();
                int s1 = Integer.parseInt(share1);
                String share2 = Share2.getText().toString();
                int s2 = Integer.parseInt(share2);
                String buy1 = Buy1.getText().toString();
                double b1 = Double.parseDouble(buy1);
                String buy2 = Buy2.getText().toString();
                double b2 = Double.parseDouble(buy2);


                int TotalShare = s1 + s2;

                AvgShare.setText("Total Share:" + TotalShare);

                double amBought=TotalShare*b1+TotalShare*b2;

                double finalPrice = amBought / TotalShare ;
                AvgPrice.setText("Average Price:" + finalPrice);

            }
        });

    }
}

主要活动。JAVA

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/head2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="52dp"
        android:background="#F44336"
        android:fontFamily="sans-serif-condensed-light"
        android:shadowColor="#EADADA"
        android:text="Total Shares"
        android:textColor="#EAEAEA"
        android:textSize="34sp"
        android:textStyle="bold"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <EditText
        android:id="@+id/buy2"
        android:layout_width="118dp"
        android:layout_height="45dp"
        android:layout_marginTop="32dp"
        android:ems="10"
        android:inputType="numberDecimal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.689"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buy1" />

    <EditText
        android:id="@+id/Share2"
        android:layout_width="115dp"
        android:layout_height="50dp"
        android:layout_marginTop="28dp"
        android:ems="10"
        android:inputType="number"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.113"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/Share1" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="88dp"
        android:text="Buy Price"
        android:textColor="#F44336"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.653"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/head"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#F44336"
        android:fontFamily="sans-serif-condensed-light"
        android:shadowColor="#EADADA"
        android:text="Stock Average Calculator"
        android:textColor="#FFC107"
        android:textSize="34sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.628"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.023" />

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="88dp"
        android:text="Shares Bought"
        android:textColor="#2196F3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.137"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/buy1"
        android:layout_width="126dp"
        android:layout_height="46dp"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:inputType="numberDecimal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.689"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text2" />

    <EditText
        android:id="@+id/Share1"
        android:layout_width="118dp"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:inputType="number"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.113"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text1" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="220dp"
        android:text="Calculate"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/head" />

    <TextView
        android:id="@+id/avgshare"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:fontFamily="sans-serif-black"
        android:textColor="#4CAF50"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/head2" />

    <TextView
        android:id="@+id/avgPrice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:textColor="#2581AA"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/avgshare" />

</androidx.constraintlayout.widget.ConstraintLayout>

共有1个答案

陈和裕
2023-03-14

我们一行一行走吧。

int TotalShare = s1 + s2; // 200 <- 100 + 100

AvgShare.setText("Total Share:" + TotalShare); //200

double amBought=TotalShare*b1+TotalShare*b2; // 3000<- 200*10+200*5 (the problematic line)

double finalPrice = amBought / TotalShare ; // 15 <- 3000/2000
AvgPrice.setText("Average Price:" + finalPrice); // 15(prints 15.0 because it is double.

有问题的行应该是:

double amBought=s1*b1+s2*b2;
 类似资料: