简体   繁体   English

Android Developer计数器错误的双数

[英]Android Developer counter wrong double numbers

i've made an app with android studio, to count money. 我已经用android studio制作了一个应用程序来数钱。 The first row is for 1 cent, the secont row is for 2 cents. 第一行是1美分,第二行是2美分。

theres a picture how it should be 有一张图片应该是

everithing is fine until i got the counter to 35, then my total money makes something stupid 一切都好,直到我将计数器提高到35,然后我的总收入才变得愚蠢

and i dont know why.. it's just an double that counts my money 我不知道为什么..这只是我的钱的两倍

The second row is not finished yet, first i want to know why my first row doesn't work 第二行尚未完成,首先我想知道为什么我的第一行不起作用

Heres my code: 这是我的代码:

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));
}
}

and there is the code from my xml file, if someone want to try this out 如果有人想尝试一下,我的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>

In floating point units 0.1 != 0.1. 以浮点数单位0.1!= 0.1。 Fab Sa wrote a perfect explanation about the reason. Fab Sa为原因写了一个完美的解释。

See his answer of this post. 看到他对这篇文章的回答。

My suggestion, save the result in two separate variable. 我的建议是,将结果保存在两个单独的变量中。 One for the integer part and one for the decimal part. 一个代表整数部分,另一个代表小数部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM