Android开发人员计数器错误的双数



我用安卓工作室做了一个应用程序,用来数钱。第一排是1美分,第二排是2美分。

这是一张应该是的图片

一切都很好,直到我把柜台调到35,然后我的总钱就变成了愚蠢的

我不知道为什么。。这只是我的钱的双倍

第二排还没有完成,首先我想知道为什么我的第一排不工作

这是我的代码:

package com.tom.dennis.kassensturz;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class kasse_geld_eingeben extends AppCompatActivity {
public double geld=0;
//money
private int cent_ein;
private int cent_zwei;
//EditText views
private EditText textout_cent_ein;
private EditText textout_cent_zwei;
private TextView textout_geld;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_kasse_geld_eingeben);
    textout_cent_ein=(EditText)findViewById(R.id.ausgabe_cent_ein);
    textout_cent_zwei=(EditText)findViewById(R.id.ausgabe_cent_zwei);
    textout_geld=(TextView)findViewById(R.id.geld_ausgabe);
    textout_cent_ein.addTextChangedListener(mTextEditorWatcher); // sollte jetzt watchen
}
private final TextWatcher  mTextEditorWatcher = new TextWatcher() //hmm
{
    public void beforeTextChanged(CharSequence s, int start, int count, int after)
    {
    }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        try
        {
            cent_ein = Integer.parseInt(textout_cent_ein.getText().toString()); // wandelt meinen string in int um
            geld=geld_ausrechnen();
            textout_geld.setText(String.valueOf(geld));
        } catch(NumberFormatException nfe)
        { // fängt fehler ab
            System.out.println("Could not parse " + nfe);
        }
    }
    @Override
    public void afterTextChanged(Editable s)
    {
    }
};
private double  geld_ausrechnen()
{
    double betrag;
    betrag=cent_ein*0.01+cent_zwei*0.02;
    return betrag;
}

@Override
public View findViewById(int id) {
    return super.findViewById(id);
}

public void afterTextChanged(Editable s)
{
}

public void buttonOnClick_geld_plus_cent_ein(View V) {
    cent_ein = cent_ein + 1;
    textout_cent_ein.setText(String.valueOf(cent_ein));
    textout_geld.setText(String.valueOf(geld));
}
public void buttonOnClick_geld_minus_cent_ein(View V)
{
    cent_ein=cent_ein-1;
    textout_cent_ein.setText(String.valueOf(cent_ein));
    textout_geld.setText(String.valueOf(geld));
}
public void buttonOnClick_geld_plus_cent_zwei(View V) {
    cent_zwei = cent_zwei + 1;
    textout_cent_zwei.setText(String.valueOf(cent_zwei));
    geld = geld + 0.02;
    textout_geld.setText(String.valueOf(geld));
}
public void buttonOnClick_geld_minus_cent_zwei(View V)
{
    cent_zwei=cent_zwei-1;
    geld=geld-0.02;
    textout_cent_zwei.setText(String.valueOf(cent_zwei));
    textout_geld.setText(String.valueOf(geld));
}
}

如果有人想试用,这里有我的xml文件中的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.tom.dennis.kassensturz.kasse_geld_eingeben">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/geld_zeile">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Kassen inhalt:"
            android:id="@+id/gesamtes_geld"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="122"
            android:id="@+id/geld_ausgabe"
            android:layout_toRightOf="@+id/gesamtes_geld"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="€"
            android:id="@+id/euro_zeichen"
            android:layout_toRightOf="@+id/geld_ausgabe"
            />
    </RelativeLayout>
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/scrollView"
        android:layout_below="@+id/geld_zeile">
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/cent_ein">
                <ImageView
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:id="@+id/bild_cent_ein"
                    android:src="#050505"
                     />
                <!-- inputType und ems damit nur zahlen eingegeben werden können -->
                <EditText
                    android:layout_width="0dp"
                    android:minWidth="100dp"
                    android:layout_height="wrap_content"
                    android:inputType="number"
                    android:ems="10"
                    android:id="@+id/ausgabe_cent_ein"
                    android:layout_weight=".30"
                    android:editable="false" />
                <Button
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:text="+"
                    android:id="@+id/button_geld_plus_cent_ein"
                    android:onClick="buttonOnClick_geld_plus_cent_ein" />
                <Button
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:text="-"
                    android:id="@+id/button_geld_minus_cent_ein"
                    android:layout_toRightOf="@+id/button_geld_plus_cent_ein"
                    android:onClick="buttonOnClick_geld_minus_cent_ein" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/cent_zwei"
                android:layout_below="@id/cent_ein"
                android:paddingTop="10dp">
                <ImageView
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:id="@+id/bild_cent_zwei"
                    android:src="#050505"
                    />
                <!-- inputType und ems damit nur zahlen eingegeben werden können -->
                <EditText
                    android:layout_width="match_parent"
                    android:minWidth="260dp"
                    android:layout_height="wrap_content"
                    android:inputType="number"
                    android:ems="10"
                    android:id="@+id/ausgabe_cent_zwei"
                    android:layout_weight=".4"
                    android:editable="false" />
                <Button
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:text="+"
                    android:id="@+id/button_geld_plus_cent_zwei"
                    android:onClick="buttonOnClick_geld_plus_cent_zwei" />
                <Button
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:text="-"
                    android:id="@+id/button_geld_minus_cent_zwei"
                    android:layout_toRightOf="@+id/button_geld_plus_cent_ein"
                    android:onClick="buttonOnClick_geld_minus_cent_zwei" />
            </LinearLayout>
            <!--
            <LinearLayout
                android:layout_below="@id/cent_ein"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="10dp">
            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:id="@+id/bild_cent_zwei"
                android:src="#050505"
                android:layout_below="@+id/cent_ein"
                />
            <EditText
                android:layout_width="0dp"
                android:minWidth="100dp"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:ems="10"
                android:layout_weight=".30"
                android:editable="false"
                android:id="@+id/ausgabe_cent_zwei"
                />
            <Button
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:text="+"
                android:id="@+id/button_geld_plus_cent_zwei"
                />
            <Button
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:text="-"
                android:id="@+id/button_geld_minus_cent_zwei"
            />
            </LinearLayout>-->
        </RelativeLayout>
    </ScrollView>


</RelativeLayout>

浮点单位为0.1!=0.1.法写了一篇完美的解释。

看看他对这篇文章的回答。

我的建议是,将结果保存在两个单独的变量中。一个用于整数部分,一个用于小数部分。

最新更新