java除法不显示小数/andriod studio判断edittext是否为空值

计算2500/1200的时候,代码为:

public class Printf {	
	
	public static void main(String[] args) {
		double a=2500/1120;
		System.out.println(a);
	
       }
}

结果只显示2.0没有小数。

 因为java除法默认如果都是整数,输出也是整数,没有小数,即使设置数据类型是float或duoble也没用。但如果其中一个数带小数,如将1200写成1200.0,即可显示正确结果。

andriod studio 在输入框里EditText内无输入数值,但在后面进行数值计算或显示的时候会出错。所以要判断输入的是否是空值。方法如下:

editText.getText().length()==0

用length()==0来判断。editText.getText().tostring()==null || editText.getText().tostring()==""无效。

实验完全代码:

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:id="@+id/tv_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:background="#00BCD4"
        android:gravity="center"
        android:text="Hello World!"
        android:textSize="35dp" />

    <Button
        android:id="@+id/bt1"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="2dp"
        android:onClick="click"
        android:text="按钮"

        android:textSize="30sp" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="edittext1"
        android:inputType="number"
        android:selectAllOnFocus="true"
        android:text="0"
        android:textSize="30dp" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="edittext2"
        android:inputType="number"
        android:selectAllOnFocus="true"
        android:text="0"
        android:textSize="30dp" />
</LinearLayout>

java文件:

package com.example.test;

import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {
    private TextView tv_show; //增加
    private EditText editText;//增加
    private EditText editText1;//增加
    private String editstr1;//增加
    private String editstr2;//增加
    private int total;//增加

    static int x,b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        tv_show=(TextView) findViewById(R.id.tv_show) ;//增加
        editText=(EditText)findViewById(R.id.editText);//增加
        editText1=(EditText)findViewById(R.id.editText1);//增加


        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }
    public void click(View view){
        //editstr1=editText.getText().toString();//增加
        //editstr2=editText1.getText().toString();     //增加

        if (editText.getText().length()==0){
             x = 0;
        }else{
             x=Integer.parseInt(editText.getText().toString());
        }
        if(editText1.getText().length()==0){
            b=0;
        }else{
            b=Integer.parseInt(editText1.getText().toString());
        }

        int total=(int)(x/120.0+b/210.0);//增加
        String i =Integer.toString(total);//增加
        tv_show.setText(i);//增加

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

效果:

参考链接:

Java空字符串与null的区别和判断字符串是否为空的方法 - swustqiu - 博客园

猜你喜欢

转载自blog.csdn.net/kim5659/article/details/120809054