[英]Else without if - android studio/java
我正在為計算器編寫代碼。 我在代碼的最后一個“ else”處出現了“ else if 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
...
注意:我尚未檢查您的邏輯。
請參閱: if-then和if-then-else語句 ,它指出
只要“ then”子句僅包含一個語句,則右括號和右括號是可選的
第60行出錯。此處定義的其他塊均不包含if塊。 另請參閱行號。 另請參見第47行。無任何條件的情況下阻止啟動'{'。
我剛剛通過格式化代碼並在兩個位置添加花括號來消除錯誤:
請檢查兩個括號是否都符合您的邏輯:這兩個都突出顯示:
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("*")) {// added here
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();
// added below before else word
} 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();
} //removed from here
謝謝
適當縮進,您的代碼如下所示:
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();
}
現在應該可以輕松地發現某些地方缺少大括號。
注意事項:有時您正在這樣做:
if(something)
dosomething();
盡管從技術上講這很好,但它可以將錯誤潛入您的代碼中(例如,如果以后再添加其他內容)。 我的建議是謹慎行事,並始終使用方括號-這樣會更像這樣:
if(something){
doSomething();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.