انجمن سایت کلیدستان


رتبه موضوع:
  • 1 رای - 5 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
مشکل ماشین حساب (در برنامه نویسی اندروید)

مشکل ماشین حساب (در برنامه نویسی اندروید)

#1
Rainbow 
لطف کنین اگه میتونین بگین من چیکار کنم این کد برای ضرب و تقسیم بر صفر ارور نده ممنون
فایل پوشه ورک اسپیس این پروژه رو ضمیمه کردم لطفا کمکم کنین.

 

دانلود Calculator.rar

نام فایل Calculator.rar
نوع فایل .rar
دفعات دانلود 113
اندازه 3.34 MB
ارسال کننده فایل amnrah
پاسخ

مشکل ماشین حساب (در برنامه نویسی اندروید)

#2
سلام
اقا این کدهاش چرا اینقدر زیاده کلی شرط براش گذاشتی
من قبلا یه ماشین حساب نوشته بودم که کدهاش کمتره میزارم اگه دوست داشتی از این کد کمک بگیر
کدهای جاوا

کد پی‌اچ‌پی:
package ir.kando.mashinhesab;

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

public class 
MainActivity extends Activity {

    @
Override
    
protected void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        
setContentView(R.layout.activity_main);
        
        final 
EditText n1 = (EditTextfindViewById(R.id.editText1);
        final 
EditText n2 = (EditTextfindViewById(R.id.editText2);
        
        final 
TextView an = (TextViewfindViewById(R.id.answer);
        
        
Button mosbat = (ButtonfindViewById(R.id.jam);
        
Button manfi = (ButtonfindViewById(R.id.kam);
        
Button zarb = (ButtonfindViewById(R.id.zarb);
        
Button taghsim = (ButtonfindViewById(R.id.taghsim);
        
        
mosbat.setOnClickListener(new OnClickListener() {
            
            @
Override
            
public void onClick(View arg0) {
                
                
int e1 =Integer.parseInt(n1.getText().toString());
                
int e2 Integer.parseInt(n2.getText().toString());
                
int e3 e1 e2
                
an.setText("javab"+e1+"+"+e2+"="+e3);        
            }
        });
        
manfi.setOnClickListener(new OnClickListener() {
            
            @
Override
            
public void onClick(View arg0) {
                
                
int e1 =Integer.parseInt(n1.getText().toString());
                
int e2 Integer.parseInt(n2.getText().toString());
                
int e3 e1 e2
                
an.setText("javab"+e1+"-"+e2+"="+e3);        
            }
        });
        
        
zarb.setOnClickListener(new OnClickListener() {
            
            @
Override
            
public void onClick(View arg0) {
                
                
int e1 =Integer.parseInt(n1.getText().toString());
                
int e2 Integer.parseInt(n2.getText().toString());
                
int e3 e1 e2
                
an.setText("javab"+e1+"*"+e2+"="+e3);        
            }
        });
        
        
taghsim.setOnClickListener(new OnClickListener() {
            
            @
Override
            
public void onClick(View arg0) {
                
                
int e1 =Integer.parseInt(n1.getText().toString());
                
int e2 Integer.parseInt(n2.getText().toString());
                if(
e2 != 0){ 
                
int e3 e1 e2
                
an.setText("javab"+e1+"/"+e2+"="+e3);
                }
                else{
                    
an.setText("تقسیم بر صفر امکان ندارد!");
                    
                }
            }
        });
        
    }
    


کدهای xml

کد پی‌اچ‌پی:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
xmlns:tools="http://schemas.android.com/tools"
    
android:layout_width="match_parent"
    
android:layout_height="match_parent"
    
android:paddingBottom="@dimen/activity_vertical_margin"
    
android:paddingLeft="@dimen/activity_horizontal_margin"
    
android:paddingRight="@dimen/activity_horizontal_margin"
    
android:paddingTop="@dimen/activity_vertical_margin"
    
tools:context=".MainActivity">
   

    <
EditText
        android
:id="@+id/editText1"
        
android:layout_width="match_parent"
        
android:layout_height="wrap_content"
        
android:layout_alignParentLeft="true"
        
android:layout_alignParentTop="true"
        
android:layout_marginTop="29dp"
        
android:ems="10"
        
android:inputType="number" >

        <
requestFocus />
    </
EditText>

    <
EditText
        android
:id="@+id/editText2"
        
android:layout_width="match_parent"
        
android:layout_height="wrap_content"
        
android:layout_alignLeft="@+id/editText1"
        
android:layout_below="@+id/editText1"
        
android:layout_marginLeft="14dp"
        
android:layout_marginTop="42dp"
        
android:ems="10"
        
android:inputType="number" />

    <
TextView
        android
:id="@+id/textView1"
        
android:layout_width="wrap_content"
        
android:layout_height="wrap_content"
        
android:layout_alignParentLeft="true"
        
android:layout_alignParentTop="true"
        
android:text="Number 1:" />

    <
TextView
        android
:id="@+id/textView2"
        
android:layout_width="wrap_content"
        
android:layout_height="wrap_content"
        
android:layout_alignLeft="@+id/editText1"
        
android:layout_below="@+id/editText1"
        
android:layout_marginTop="21dp"
        
android:text="Number 2:" />

    <
Button
        android
:id="@+id/kam"
        
android:layout_width="match_parent"
        
android:layout_height="wrap_content"
        
android:layout_alignRight="@+id/zarb"
        
android:layout_below="@+id/jam"
        
android:background="#FF4848"
        
android:text="-" />

    <
Button
        android
:id="@+id/zarb"
        
android:layout_width="match_parent"
        
android:layout_height="wrap_content"
        
android:layout_alignLeft="@+id/jam"
        
android:layout_below="@+id/kam"
        
android:background="#A55FEB"
        
android:text="*" />

    <
Button
        android
:id="@+id/taghsim"
        
android:layout_width="match_parent"
        
android:layout_height="wrap_content"
        
android:layout_alignLeft="@+id/zarb"
        
android:layout_below="@+id/zarb"
        
android:background="#48FB0D"
        
android:text="/" />

    <
Button
        android
:id="@+id/jam"
        
android:layout_width="match_parent"
        
android:layout_height="wrap_content"
        
android:layout_alignLeft="@+id/textView2"
        
android:layout_below="@+id/answer"
        
android:background="#06DCFB"
        
android:text="+" />

    <
TextView
        android
:id="@+id/textView3"
        
android:layout_width="wrap_content"
        
android:layout_height="wrap_content"
        
android:layout_alignParentBottom="true"
        
android:layout_toRightOf="@+id/textView2"
        
android:text="lماشین حساب"
        
android:textAppearance="?android:attr/textAppearanceLarge" />

    <
TextView
        android
:id="@+id/answer"
        
android:layout_width="wrap_content"
        
android:layout_height="wrap_content"
        
android:layout_alignLeft="@+id/textView3"
        
android:layout_below="@+id/editText2"
        
android:layout_marginLeft="40dp"
        
android:layout_marginTop="26dp"
        
android:text="جواب" />

</
RelativeLayout

اینم کدهای manifest

کد پی‌اچ‌پی:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ir.kando.mashinhesab"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="1"
        android:targetSdkVersion="19" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="ir.kando.mashinhesab.Splash"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="ir.kando.mashinhesab.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="ir.kando.mashinhesab.MAINACTIVITY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest> 

یه توضیح کوچیک بدم که من تو کدهای خودم اومدم از تابع زیر استفاده کردم و مشکل تقسیم بر صفر رو با نمایش یک متن به کاربر حل کردم که پیغام میده که میگه تقسیم بر صفر امکان نداره

کد پی‌اچ‌پی:
        taghsim.setOnClickListener(new OnClickListener() {
            
            @
Override
            
public void onClick(View arg0) {
                
                
int e1 =Integer.parseInt(n1.getText().toString());
                
int e2 Integer.parseInt(n2.getText().toString());
                if(
e2 != 0){ 
                
int e3 e1 e2
                
an.setText("javab"+e1+"/"+e2+"="+e3);
                }
                else{
                    
an.setText("تقسیم بر صفر امکان ندارد!");
                    
                }
            }
        }); 

امیدوارم کمکت کنه این کدها
موفق باشی
فهمیدن بهتر از دانستن است.
پاسخ
 سپاس شده توسط admin ، شماره مجازی امارات ، تلگرام ضد فیلتر 2023


پرش به انجمن:


کاربران در حال بازدید این موضوع: 1 مهمان