有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

爪哇1。按钮不显示文本2。逻辑正确吗

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

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

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

java代码:

package com.example.trianglegame;

import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.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:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical" >

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

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

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

    <requestFocus />
</EditText>

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

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

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

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

<LinearLayout
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:orientation="horizontal" >

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

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

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

</LinearLayout>

LogCat错误

09-21 18:50:20.739: E/Zygote(32): setreuid() failed. errno: 2
09-21 18:50:24.939: E/Zygote(32): setreuid() failed. errno: 17
09-21 18:50:25.579: E/BatteryService(58): usbOnlinePath not found
09-21 18:50:25.579: E/BatteryService(58): batteryVoltagePath not found
09-21 18:50:25.579: E/BatteryService(58): batteryTemperaturePath not found
09-21 18:50:25.589: E/SurfaceFlinger(58): Couldn't open /sys/power/wait_for_fb_sleep or  /sys/power/wait_for_fb_wake
09-21 18:50:28.279: E/EventHub(58): could not get driver version for /dev/input/mouse0, Not a typewriter
09-21 18:50:28.279: E/EventHub(58): could not get driver version for /dev/input/mice, Not a typewriter
09-21 18:50:28.379: E/System(58): Failure starting core service
09-21 18:50:28.379: E/System(58): java.lang.SecurityException
09-21 18:50:28.379: E/System(58):   at 安卓.os.BinderProxy.transact(Native Method)
09-21 18:50:28.379: E/System(58):   at 安卓.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
09-21 18:50:28.379: E/System(58):   at 安卓.os.ServiceManager.addService(ServiceManager.java:72)
09-21 18:50:28.379: E/System(58):   at com.安卓.server.ServerThread.run(SystemServer.java:184)
09-21 18:50:28.749: E/SoundPool(58): error loading /system/media/audio/ui/Effect_Tick.ogg
09-21 18:50:28.749: E/SoundPool(58): error loading /system/media/audio/ui/KeypressStandard.ogg
09-21 18:50:28.749: E/SoundPool(58): error loading /system/media/audio/ui/KeypressSpacebar.ogg
09-21 18:50:28.749: E/SoundPool(58): error loading /system/media/audio/ui/KeypressDelete.ogg
09-21 18:50:28.749: E/SoundPool(58): error loading /system/media/audio/ui/KeypressReturn.ogg
09-21 18:50:29.359: E/ThrottleService(58): Could not open GPS configuration file /etc/gps.conf
09-21 18:50:29.819: E/logwrapper(133): executing /system/bin/tc failed: No such file or directory
09-21 18:50:29.889: E/logwrapper(135): executing /system/bin/tc failed: No such file or directory
09-21 18:50:29.889: E/logwrapper(136): executing /system/bin/tc failed: No such file or directory
09-21 18:50:32.957: E/HierarchicalStateMachine(58): TetherMaster - unhandledMessage: msg.what=3

共 (2) 个答案

  1. # 1 楼答案

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

    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;
    }
    

    就它显示的错误而言,你能发布日志吗

  2. # 2 楼答案

    而不是:

    input2 = Input2.getText().toString();
    input3 = Input2.getText().toString();
    

    使用:

    input2 = Input2.getText().toString();
    input3 = Input3.getText().toString();