Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 1.按钮不显示文本2。逻辑正确吗_Java_Android_Xml_Button_If Statement - Fatal编程技术网

Java 1.按钮不显示文本2。逻辑正确吗

Java 1.按钮不显示文本2。逻辑正确吗,java,android,xml,button,if-statement,Java,Android,Xml,Button,If Statement,问题1出于某种原因,当我单击“生成”时,它不会显示任何文本。我希望该按钮执行以下操作:在用户输入3个值int后,例如:1,1,2,然后单击“生成”按钮,显示消息应为等腰三角形:2个全等边——这在以前是有效的,但我的逻辑是错误的,我在这里问了一个问题:。然后我尝试实施这些建议,但现在它出现了一个不同的错误 问题2:我的逻辑正确吗?解决此问题后,我是否能够显示正确的结果 请帮忙,我对android编程还不熟悉。非常感谢 java代码: package com.example.trianglegame

问题1出于某种原因,当我单击“生成”时,它不会显示任何文本。我希望该按钮执行以下操作:在用户输入3个值int后,例如:1,1,2,然后单击“生成”按钮,显示消息应为等腰三角形:2个全等边——这在以前是有效的,但我的逻辑是错误的,我在这里问了一个问题:。然后我尝试实施这些建议,但现在它出现了一个不同的错误

问题2:我的逻辑正确吗?解决此问题后,我是否能够显示正确的结果

请帮忙,我对android编程还不熟悉。非常感谢

java代码:

package com.example.trianglegame;

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

public class TriangleGame extends Activity {// implement-have to use all of the
// methods
// set up the variables here

Button Gen;
EditText Input1;
EditText Input2;
EditText Input3;
String input1;
String input2;
String input3;

TextView Display;

int a;
int b;
int c;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

    // assigning the values
    Input1 = (EditText) findViewById(R.id.editText1);
    Input2 = (EditText) findViewById(R.id.editText2);
    Input3 = (EditText) findViewById(R.id.editText3);

    Display = (TextView) findViewById(R.id.textView5);

    Gen = (Button) findViewById(R.id.button1);

    Gen.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            // getting the text and converting it to string value
            input1 = Input1.getText().toString();
            input2 = Input2.getText().toString();
            input3 = Input2.getText().toString();

            // converting those text values back into int
            a = Integer.parseInt(input1);
            b = Integer.parseInt(input2);
            c = Integer.parseInt(input3);

            // displaying the message

            if ((a == b && b != c) || (a == c && b != c)
                    || (b == c && a != c)) {
                Display.setText("Isosceles Triangle: 2 Congruent Sides");
            } else if (a == b && a == c) {
                Display.setText("Equilateral Triangle:All sides are equal");
            }

            else if (a != b && a != c && b != c) {
                Display.setText("Scalene Triangle: No Congruent Sides");
            } else {
                Display.setText("Error");
            }

        }
    });
}
}
xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/enter_text" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/side_1" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/type_hint"
    android:inputType="number" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/side_2" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/type_hint"
    android:inputType="number" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/type_hint"
    android:text="@string/side_3" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/type_hint"
    android:inputType="number" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/generate" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/clear" />
</LinearLayout>

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>
而不是:

input2 = Input2.getText().toString();
input3 = Input2.getText().toString();
使用:


就我所见,逻辑是好的,尽管我会用另一种方式检查,更具可读性

int n = 0; //number of equal sides

if(a==b)
  n++;
if(a==c)
  n++;
if(b==c)
  n++;

//Display seems like a class, but is a variable, should start with lower case and have camel notation such as displayTextView
switch(n) {
  case 0:
    Display.setText("Scalen");
    break;
  case 1:
    Display.setText("Isosceles");
    break;
  case 2: case 3:
    Display.setText("Equilateral");
    break;
}

至于显示的错误,您可以发布日志吗?

单击按钮时要执行什么操作。请指定。在Java和Android中,对象和变量名必须以小写字母开头,如button gen;,除非它们是公共静态finalhi,否则我将所有输入更改为小写,但在我单击“生成”按钮后,它仍然不会显示文本日志显示的内容???在按钮单击中使用system.out.println来检测按钮单击是否为firedIt,是否为模拟器的路径。我调整了它,一切都很好。主要的问题是我的按钮覆盖了我的文本,所以代码和逻辑都很好。谢谢你的帮助。谢谢你注意到这一点。我试过了,但还是出现了同样的错误,所以一定是这个错误,还有其他错误。1,1,2的输出是什么?起初它没有显示任何内容,这是因为我的按钮覆盖了它:我知道,现在一切都很好,除了你给我看的打字错误。再次感谢你知道,因为我一直盯着你的代码看,不知道出了什么问题:哈哈,我是这个网站的新手,所以我问的问题,我想其他人已经问过了,出于某种原因,我的声望下降了。。。希望将来我还能问问题。但是,是的,我总是研究问题,通常找不到答案,这就是我发布它的原因:嗨,当我运行程序时,我在LogCat中没有看到任何错误,所以我无法发布:当应用程序崩溃时,如果你的手机已连接,LogCat应该打印一些内容,请尝试使用IDE conenctedHi,谢谢你的回答。是的,它现在打印了一些错误。我会编辑我的帖子我想那不是我们要找的日志哈哈,你有例外说明吗?为了清楚起见,您的xml是game.xml?是的,它是game.xml。嗨,我没有为输入值抛出异常。我应该这么做吗?您是否建议我查看如何为这些输入值抛出异常?
input2 = Input2.getText().toString();
input3 = Input3.getText().toString();
int n = 0; //number of equal sides

if(a==b)
  n++;
if(a==c)
  n++;
if(b==c)
  n++;

//Display seems like a class, but is a variable, should start with lower case and have camel notation such as displayTextView
switch(n) {
  case 0:
    Display.setText("Scalen");
    break;
  case 1:
    Display.setText("Isosceles");
    break;
  case 2: case 3:
    Display.setText("Equilateral");
    break;
}