java - 否则没有 if - android studio/java

标签 java android if-statement

我正在为计算器编写代码。我在代码中最后一个“else”处遇到错误“else without if”。如果您能帮我弄清楚它是什么,我将不胜感激:)

ArrayList<String> arrayList = new ArrayList<String>();
String string = "";
String string1 = "";

public void onClick1(View v) {

    TextView textView2 = (TextView) findViewById(R.id.textView2);

    Button button = (Button) v;

    string = (String) button.getText().toString();

    if (!string.contains("+") && !string.contains("-") && !string.contains("*") && !string.contains("/")) {

        string1 = string1 + string;

        if (arrayList.size() > 0) {
            arrayList.remove((arrayList.size() - 1));
        }

        arrayList.add(string1);
    } else {
        arrayList.add(string);
        arrayList.add(string);
        string1 = "";
    }

    //textView2.setText(textView2.getText().toString()+string);

    textView2.setText(arrayList.toString());
}

public void onClick(View v) {

    TextView textView1 = (TextView) findViewById(R.id.textView);

    int calc = 0;
    int c = arrayList.size();

    while (c != 1) {

        if (c > 3) {

            if (arrayList.get(3).contains("*") || arrayList.get(3).contains("/")) {

                if (arrayList.get(3).contains("*")) {
                    calc = Integer.parseInt(arrayList.get(2)) * Integer.parseInt(arrayList.get(4));
                }
                if (arrayList.get(3).contains("/")) {
                    calc = Integer.parseInt(arrayList.get(2)) / Integer.parseInt(arrayList.get(4));
                }

                arrayList.remove(2);
                arrayList.remove(2);
                arrayList.remove(2);
                arrayList.add(2, Integer.toString(calc));
                c = arrayList.size();
            } else {
                if (arrayList.get(1).contains("+"))
                    calc = Integer.parseInt(arrayList.get(0)) + Integer.parseInt(arrayList.get(2));
            }
            if (arrayList.get(1).contains("-"))
                calc = Integer.parseInt(arrayList.get(0)) - Integer.parseInt(arrayList.get(2));
        }
        if (arrayList.get(1).contains("*"))
            calc = Integer.parseInt(arrayList.get(0)) * Integer.parseInt(arrayList.get(2));

        {
            if (arrayList.get(1).contains("/"))
                calc = Integer.parseInt(arrayList.get(0)) / Integer.parseInt(arrayList.get(2));

            arrayList.remove(0);
            arrayList.remove(0);
            arrayList.remove(0);
            arrayList.add(0, Integer.toString(calc));
            c = arrayList.size();

            else {
                if (arrayList.get(1).contains("+")) {
                    calc = Integer.parseInt(arrayList.get(0)) + Integer.parseInt(arrayList.get(2));
                }
                if (arrayList.get(1).contains("-")) {
                    calc = Integer.parseInt(arrayList.get(0)) + Integer.parseInt(arrayList.get(2));
                }
                if (arrayList.get(1).contains("*")) {
                    calc = Integer.parseInt(arrayList.get(0)) + Integer.parseInt(arrayList.get(2));
                }
                if (arrayList.get(1).contains("/")) {
                    calc = Integer.parseInt(arrayList.get(0)) + Integer.parseInt(arrayList.get(2));
                }

                arrayList.remove(0);
                arrayList.remove(0);
                arrayList.remove(0);
                arrayList.remove(0);
                arrayList.add(0, Integer.toString(calc));
                c = arrayList.size();
            }
        }
        textView1.setText(Integer.toString(calc));
    }
}

public void clear(View v) {
    TextView textView1 = (TextView) findViewById(R.id.textView);
    TextView textView2 = (TextView) findViewById(R.id.textView2);

    string1 = "";
    string = "";
    textView1.setText("0");
    textView2.setText("");
    arrayList.clear();
}

最佳答案

您可以检查onClick()方法的代码。你最好格式化你的代码。我添加了评论来指出问题。

...
if (arrayList.get(1).contains("*"))
    calc = Integer.parseInt(arrayList.get(0)) * Integer.parseInt(arrayList.get(2));

{ // <----------------------------this is redundant 
    if (arrayList.get(1).contains("/"))
        calc = Integer.parseInt(arrayList.get(0)) / Integer.parseInt(arrayList.get(2));

    arrayList.remove(0); // this code is not in above if block.
    arrayList.remove(0);
    arrayList.remove(0);
    arrayList.add(0, Integer.toString(calc));
    c = arrayList.size();

    else { // <----------------------------this else is without if
        ...

注意:我还没有检查你的逻辑。

<小时/>

引用:The if-then and if-then-else Statements ,它指出

The opening and closing braces are optional, provided that the "then" clause contains only one statement

关于java - 否则没有 if - android studio/java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32966459/

相关文章:

java - 父类中@Id 和基类中唯一序列的正确 JPA 映射是什么

java - 处理随机扑克牌

java - Java 中的属性表

java - 数组列表中的数组列表

android - 连接一上的 Camera.setParameters() 上的 RuntimeException

java - Android studio如何引用库?

java - 解析 xml 时出错 : unbound prefix

mysql - 如何在 MYSQL 中使用 if 查询创建虚拟列

if-statement - 如何使用 if 语句在 FFMPEG 中添加颜色叠加

PHP如何测试字符串中是否存在(部分)数组的值之一?