gpt4 book ai didi

java - 当我将 0 添加到一个数字时,结果始终为零。其他数字都还好

转载 作者:行者123 更新时间:2023-12-02 10:59:46 25 4
gpt4 key购买 nike

我正在尝试制作一个应用程序,将我已经编写的两个数字相加,并在下一个 Activity 中显示结果。我让它可以处理非零数字,但是当我将 0 添加到另一个数字时,结果始终为零。我知道这是因为默认值 0。但它与其他值一起工作正常。当我添加除零以外的任何数字时,它都可以正常工作,但是当包含 0 时,整个结果将变为零。请帮我。谢谢!

主要 Activity

package com.example.david.magic;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);}

public void no (View view){
EditText editTextOne = (EditText) findViewById(R.id.textView);
EditText editTextTwo = (EditText) findViewById(R.id.textView1);
int firstNumber = Integer.parseInt(editTextOne.getText().toString());
int secondNumber = Integer.parseInt(editTextTwo.getText().toString());
int sum = firstNumber + secondNumber;
// Now send the result sum to the Activity B through intent.
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra("RESULT_SUM", sum);
startActivity(intent);


}

public void yes (View view){
EditText editTextOned = (EditText) findViewById(R.id.resultno);
EditText editTextTwod = (EditText) findViewById(R.id.textView);
int firstNumberd = Integer.parseInt(editTextOned.getText().toString());
int secondNumberd = Integer.parseInt(editTextTwod.getText().toString());
int sumd = firstNumberd + secondNumberd;
// Now send the result sum to the Activity B through intent.
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra("RESULT_SUMd", sumd);
startActivity(intent);


}}

Activity 主

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.217"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.961"
android:text="Yes"
android:onClick="yes"/>

<Button
android:id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.739"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.961"
android:text="No"
android:onClick="no"/>

<EditText
android:id="@+id/textView"
android:layout_width="182dp"
android:layout_height="58dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="1"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/textView1"
android:layout_width="174dp"
android:layout_height="49dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="0"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.381" />

<EditText
android:id="@+id/resultno"
android:layout_width="170dp"
android:layout_height="50dp"
android:layout_marginEnd="104dp"
android:layout_marginRight="104dp"
app:layout_constraintEnd_toEndOf="parent"
android:text="0"
android:visibility="invisible"
tools:layout_editor_absoluteY="128dp" />

<ImageView
android:layout_width="366dp"
android:layout_height="282dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/one"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.289" />


</android.support.constraint.ConstraintLayout>

Main2Activity

package com.example.david.magic;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {

String fud;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);


EditText hi = findViewById(R.id.textView2);
Bundle extras = getIntent().getExtras();
if (extras != null) {

int sum = getIntent().getIntExtra("RESULT_SUM", 0);

hi.setText(String.valueOf(sum));
}


EditText bye = findViewById(R.id.textView2);
Bundle extra = getIntent().getExtras();
if (extras != null) {

int sumd = getIntent().getIntExtra("RESULT_SUMd", 0);

bye.setText(String.valueOf(sumd));
}
}

public void yyes (View view){
EditText editTextOne = (EditText) findViewById(R.id.textView2);
EditText editTextTwo = (EditText) findViewById(R.id.textView1);
int firstNumber = Integer.parseInt(editTextOne.getText().toString());
int secondNumber = Integer.parseInt(editTextTwo.getText().toString());
int sum = firstNumber + secondNumber;
// Now send the result sum to the Activity B through intent.
Intent intent = new Intent(this, Main3Activity.class);
intent.putExtra("RESULT_SUM1", sum);
startActivity(intent);


}

public void nno (View view){
EditText editTextOne1 = (EditText) findViewById(R.id.saysno);
EditText editTextTwo1 = (EditText) findViewById(R.id.textView2);
int firstNumber1 = Integer.parseInt(editTextOne1.getText().toString());
int secondNumber1 = Integer.parseInt(editTextTwo1.getText().toString());
int sum2 = secondNumber1;
// Now send the result sum to the Activity B through intent.
Intent nno = new Intent(this, Main3Activity.class);
nno.putExtra("RESULT_SUM2", sum2);
startActivity(nno);


}
}


activity_main2

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity">


<EditText
android:id="@+id/textView2"
android:layout_width="147dp"
android:layout_height="51dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:visibility="invisible"
android:text=""
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.217"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.961"
android:text="Yes"
android:onClick="yyes"/>

<Button
android:id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.739"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.961"
android:text="No"
android:onClick="nno"/>



<EditText
android:id="@+id/textView1"
android:layout_width="174dp"
android:layout_height="49dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="2"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.381" />

<EditText
android:id="@+id/saysno"
android:layout_width="170dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="196dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="196dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="0"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:layout_width="366dp"
android:layout_height="282dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/two"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.289" />


</android.support.constraint.ConstraintLayout>

最佳答案

问题来自这部分代码:

 EditText hi = findViewById(R.id.textView2);
Bundle extras = getIntent().getExtras();
if (extras != null) {

int sum = getIntent().getIntExtra("RESULT_SUM", 0);

hi.setText(String.valueOf(sum));
}


EditText bye = findViewById(R.id.textView2);
Bundle extra = getIntent().getExtras();
if (extras != null) {

int sumd = getIntent().getIntExtra("RESULT_SUMd", 0);

bye.setText(String.valueOf(sumd));
}

无论您单击"is"或“否”,extras不为空。因此,当您单击“否”时,您会在第一个 if 中获得正确的值。 。但第二if总是被触发。因为,在这两种情况下,您都为 textView2 调用 setText,bye.setText(String.valueOf(sumd));将正确的值覆盖为 0,因为“RESULT_SUMd”为空且 0 为默认值。

让我用注释解释一下代码内部(没有情况):

EditText hi = findViewById(R.id.textView2);
Bundle extras = getIntent().getExtras();
//always true
if (extras != null) {
// sum is set with right value
int sum = getIntent().getIntExtra("RESULT_SUM", 0);

//textView2 display the right sum
hi.setText(String.valueOf(sum));
}


EditText bye = findViewById(R.id.textView2);
Bundle extra = getIntent().getExtras();

//always true
if (extras != null) {
//no result_sumd found
//sumd == 0
int sumd = getIntent().getIntExtra("RESULT_SUMd", 0);

//textView2 text is override with 0, value of sumd
bye.setText(String.valueOf(sumd));
}

当然,如果您单击"is",一切都会正常,因为在这种情况下定义了“RESULT_SUMd”。

到目前为止,我建议您添加另一个标志(以表达 Intent 来自是/否)并添加更好的 if flow:

在第一个 Activity 中:

public void no (View view){
EditText editTextOne = (EditText) findViewById(R.id.textView);
EditText editTextTwo = (EditText) findViewById(R.id.textView1);
int firstNumber = Integer.parseInt(editTextOne.getText().toString());
int secondNumber = Integer.parseInt(editTextTwo.getText().toString());
int sum = firstNumber + secondNumber;
// Now send the result sum to the Activity B through intent.
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra("RESULT_SUM", sum);

//add extra flag
intent.putExtra("FROM_NO", true);
startActivity(intent);


}

public void yes (View view){
EditText editTextOned = (EditText) findViewById(R.id.resultno);
EditText editTextTwod = (EditText) findViewById(R.id.textView);
int firstNumberd = Integer.parseInt(editTextOned.getText().toString());
int secondNumberd = Integer.parseInt(editTextTwod.getText().toString());
int sumd = firstNumberd + secondNumberd;
// Now send the result sum to the Activity B through intent.
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra("RESULT_SUMd", sumd);
startActivity(intent);


}}

在第二个 Activity 中:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

// hi is enough
EditText hi = findViewById(R.id.textView2);
Bundle extras = getIntent().getExtras();
if (extras != null) {
boolean fromNo = getIntent().getExtras().getBoolean("FROM_NO", false);
//use of ternary to make it simple
int sum = getIntent().getIntExtra(fromNo?"RESULT_SUM":"RESULT_SUMd", 0);
hi.setText(String.valueOf(sum));
}

}

关于java - 当我将 0 添加到一个数字时,结果始终为零。其他数字都还好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51423332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com