首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java计算器给出了奇怪的结果

Java计算器给出了奇怪的结果
EN

Stack Overflow用户
提问于 2013-11-06 11:45:51
回答 5查看 226关注 0票数 0

我的爪哇计算器出问题了。它有时会给我一些奇怪的结果,比如357856 * 42342 = -2027530432。

有人能告诉我为什么会发生这种事吗?而且,当我使用太大的数字时,它会崩溃,但我认为这是正常的。

这是我的activity_main.xml

代码语言:javascript
复制
    <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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#ABE033" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="@string/Zahl1"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/zahl1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:ems="10"
    android:inputType="numberDecimal" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/zahl1"
    android:layout_below="@+id/zahl1"
    android:text="@string/Zahl2"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/zahl2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="11dp"
    android:ems="10"
    android:inputType="numberDecimal" />

<EditText
    android:id="@+id/Ergebnis"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/zahl2"
    android:layout_below="@+id/zahl2"
    android:inputType="numberDecimal"
    android:ems="10" />

<Button
    android:id="@+id/button1"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:onClick="ButtonKlick"
    android:text="@string/Rechnen" />

这是我的MainActivity.java

代码语言:javascript
复制
package com.example.rechner;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void ButtonKlick (View view) {
    int zahl1;
    int zahl2;
    int Ergebnis;
    EditText Feld1 = (EditText)findViewById(R.id.zahl1);
    EditText Feld2 = (EditText)findViewById(R.id.zahl2);
    EditText FeldErgebnis = (EditText)findViewById(R.id.Ergebnis);
    if (Feld1.getText().toString().length() == 0) {
        return;
    }
    if (Feld2.getText().toString().length() == 0) {
        return;
    }
    zahl1 = Integer.parseInt(Feld1.getText().toString());
    zahl2 = Integer.parseInt(Feld2.getText().toString());
    Ergebnis = zahl1 * zahl2;

    FeldErgebnis.setText(String.valueOf(Ergebnis));


}
}

这是我的strings.xml,如果你需要的话。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">Rechner</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="Zahl1">Zahl 1:</string>
<string name="Zahl2">Zahl 2:</string>
<string name="Plus">:</string>

</resources>
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-11-06 11:48:16

检查int的最大大小,我很确定您通过了它。

代码语言:javascript
复制
int Range : minimum value of -2^31 and a maximum value of (2^31)-1
long Range : minimum value of -2^63 and a maximum value of (2^63)-1

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

尝试使用一个长数据类型,或者其他一些数据类型。

票数 4
EN

Stack Overflow用户

发布于 2013-11-06 11:50:52

int原语类型的最大极限是2,147,483,647,这是lesser而不是(357856 * 42342) = 15152338752.,因此它导致了这个奇怪的答案。使用long而不是int

票数 2
EN

Stack Overflow用户

发布于 2013-11-06 11:48:08

只是溢出

代码语言:javascript
复制
groovy:000> 357856 * 42342
===> -2027530432
groovy:000> 357856 * 42342L
===> 15152338752
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19811289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档