java - 每个操作返回一个部门

标签 java android sdk calculator

<分区>

所以,为了学习目的,我正在使用 Android SDK 编写这个无用的计算器 问题是,我做的每一个操作总是返回一个除法 我检查了,但由于我的经验不足,我找不到问题 嗯,这是主要 Java 文件的代码:

package com.example.craxcalculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
enum operations{
NO, ADD, SUB, MUL, DIV
};
public class CraxCalculator extends Activity {

public float actualNumber = 0f;
public float finalResult=0.0f;
operations o = operations.NO;
public String onScreenNumber = new String();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crax_calculator);
    Declarations();

}
@Override
protected void onStop(){
    super.onStop();
    o=operations.NO;
}

protected void Declarations(){
final Button b1 = (Button)findViewById(R.id.add1);
final Button b2 = (Button)findViewById(R.id.add2);
final Button b3 = (Button)findViewById(R.id.add3);
final Button b4 = (Button)findViewById(R.id.add4);
final Button b5 = (Button)findViewById(R.id.add5);
final Button b6 = (Button)findViewById(R.id.add6);
final Button b7 = (Button)findViewById(R.id.add7);
final Button b8 = (Button)findViewById(R.id.add8);
final Button b9 = (Button)findViewById(R.id.add9);
final Button b0 = (Button)findViewById(R.id.add0);
final Button res = (Button)findViewById(R.id.result);
final Button del = (Button)findViewById(R.id.del);
final Button mul = (Button)findViewById(R.id.mul);
final Button add = (Button)findViewById(R.id.add);
final Button sub = (Button)findViewById(R.id.sub);
final Button div = (Button)findViewById(R.id.div);
final TextView finalText = (TextView)findViewById(R.id.resultShow);

res.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        finalText.setText(onScreenNumber);
        switch (o){
        case NO:
            finalText.setText(onScreenNumber);
        case ADD:
            finalResult=actualNumber+Float.parseFloat(onScreenNumber);
            finalText.setText(String.valueOf(finalResult));
        case SUB:
            finalResult=actualNumber-Float.parseFloat(onScreenNumber);
            finalText.setText(String.valueOf(finalResult));
        case MUL:
            finalResult=actualNumber*Float.parseFloat(onScreenNumber);
            finalText.setText(String.valueOf(finalResult));
        case DIV:
            finalResult=actualNumber/Float.parseFloat(onScreenNumber);
            finalText.setText(String.valueOf(finalResult));
    }
    }
});

b1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        onScreenNumber = onScreenNumber+"1";
        finalText.setText(onScreenNumber);

    }
});
b2.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        onScreenNumber = onScreenNumber+"2";
        finalText.setText(onScreenNumber);
    }
});
b3.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        onScreenNumber+="3";
        finalText.setText(onScreenNumber);
    }
});
b4.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        onScreenNumber+="4";
        finalText.setText(onScreenNumber);
    }
});
b5.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        onScreenNumber+="5";
        finalText.setText(onScreenNumber);
    }
});
b6.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        onScreenNumber+="6";
        finalText.setText(onScreenNumber);

    }
});
b7.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        onScreenNumber+="7";
        finalText.setText(onScreenNumber);
    }
});
b8.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        onScreenNumber+="8";
        finalText.setText(onScreenNumber);
    }
});
b9.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        onScreenNumber+="9";
        finalText.setText(onScreenNumber);
    }
});
b0.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v){
        onScreenNumber+="0";
        finalText.setText(onScreenNumber);
    }
});
del.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){
        onScreenNumber="";
        finalText.setText("0");
        actualNumber=0;
        finalResult=0;
    }
});
add.setOnClickListener(new View.OnClickListener(){

    public void onClick(View v){
        o = operations.ADD;
        actualNumber= Float.parseFloat(onScreenNumber);
        onScreenNumber="";
        finalText.setText("0");
    }
});
sub.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        o = operations.SUB;
        actualNumber=Float.parseFloat(onScreenNumber);
        onScreenNumber="";
        finalText.setText("0");
    }
});
mul.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        o = operations.MUL;
        actualNumber=Float.parseFloat(onScreenNumber);
        onScreenNumber="";
        finalText.setText("0");
    }
});
div.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        o = operations.DIV;
        actualNumber=Float.parseFloat(onScreenNumber);
        onScreenNumber="";
        finalText.setText("0");
    }
});
}
}

这是我的 Activity xml

<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"
tools:context="${relativePackage}.${activityClass}" >

<Button
    android:id="@+id/add1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="171dp"
    android:text="1" />

<Button
    android:id="@+id/add2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/add1"
    android:layout_alignBottom="@+id/add1"
    android:layout_marginLeft="21dp"
    android:layout_toRightOf="@+id/add1"
    android:text="2" />

<Button
    android:id="@+id/add3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/add2"
    android:layout_alignBottom="@+id/add2"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/add2"
    android:text="3" />

<Button
    android:id="@+id/add6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/add3"
    android:layout_below="@+id/add3"
    android:text="6" />

<Button
    android:id="@+id/add5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/add2"
    android:layout_toLeftOf="@+id/add3"
    android:text="5" />

<Button
    android:id="@+id/add4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/add1"
    android:layout_toLeftOf="@+id/add2"
    android:text="4" />

<Button
    android:id="@+id/add7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/add4"
    android:layout_below="@+id/add4"
    android:text="7" />

<Button
    android:id="@+id/add8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/add7"
    android:layout_alignBottom="@+id/add7"
    android:layout_toLeftOf="@+id/add3"
    android:text="8" />

<Button
    android:id="@+id/add9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/add6"
    android:layout_below="@+id/add6"
    android:text="9" />

<TextView
    android:id="@+id/resultShow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/add1"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/add3"
    android:layout_marginTop="34dp"
    android:text="0"
    android:textSize="40dp" />

<Button
    android:id="@+id/result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/add0"
    android:layout_alignBottom="@+id/add0"
    android:layout_alignLeft="@+id/add9"
    android:text="=" />

<Button
    android:id="@+id/add0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/add8"
    android:layout_below="@+id/add8"
    android:text="0" />

<Button
    android:id="@+id/add"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/add6"
    android:layout_alignParentRight="true"
    android:layout_marginRight="14dp"
    android:text="+" />


<Button
    android:id="@+id/mul"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/result"
    android:layout_alignLeft="@+id/add"
    android:text="*" />

<Button
    android:id="@+id/sub"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/add"
    android:layout_below="@+id/add"
    android:text="-" />

<Button
    android:id="@+id/div"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/mul"
    android:layout_below="@+id/mul"
    android:text="/" />

<Button
    android:id="@+id/del"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/add7"
    android:layout_toLeftOf="@+id/add2"
    android:text="C" />

好吧,我到处乱撞,但没什么,我被这个问题困住了 c.c

最佳答案

您的 onClick 方法中的 switch 语句没有 break 语句结束案例。没有break,执行将继续通过低于预期运行的情况下的情况。在这种情况下,最后一种情况 DIV 在所有情况下都会覆盖任何其他情况的结果。

break 放在所有 case 的末尾。

switch (o){
    case NO:
        finalText.setText(onScreenNumber);
        break;
    case ADD:
        finalResult=actualNumber+Float.parseFloat(onScreenNumber);
        finalText.setText(String.valueOf(finalResult));
        break;
    case SUB:
        finalResult=actualNumber-Float.parseFloat(onScreenNumber);
        finalText.setText(String.valueOf(finalResult));
        break;
    case MUL:
        finalResult=actualNumber*Float.parseFloat(onScreenNumber);
        finalText.setText(String.valueOf(finalResult));
        break;
    case DIV:
        finalResult=actualNumber/Float.parseFloat(onScreenNumber);
        finalText.setText(String.valueOf(finalResult));
        break;
}

关于java - 每个操作返回一个部门,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25923107/

相关文章:

ios - 我们的司机应用程序的逐向导航

objective-c - 使用 isEqualToString 时访问错误

android - 如何在 Android SDK 中模拟 4G (LTE) 网络

java - 缺少双阵列

java - 如何在 Graphics 方法 drawImage() 中使用 ImageObserver

android - 如果模式与文件匹配,如何返回 0?

android - KR070PC7S LCD 显示器的 Linux 或 Android 驱动程序

java - 使用反射设置类型未知的属性值

java - 为什么我们不能使用扩展边界添加java泛型中的元素?

android - 在 android 4 上询问 android 权限吗?