所以,我'使用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
答案 0 :(得分:4)
switch
方法中的onClick
语句没有break
个语句结束案例。如果没有break
,则执行将继续执行打算运行的案例。在这种情况下,最后一种情况DIV
会在所有情况下覆盖任何其他案例的结果。
将break
放在所有案例的最后。
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;
}
答案 1 :(得分:0)
您错过了开关中的中断:
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;