الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

طريقة الجمع

مغلق
بدأه maro009 في 21 أكتوبر 2005 · 5 رد · 648 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

أولا كل عام وانتم بخير

حاليا أعمل فى برنامج جزء من هذا البرنامج يتطلب قراءة String من المستخدم ثم يقوم بتحويله الى int array

بعد التأكد من صلاحية التحويل ,,

بعد ذلك يجب ان نجمع 2 من ال int array..

اعرف ان يوجد العديد من الطرق التى تجمع مصفوفتان ,,,

سؤالى يا أخوانى ما هى احسن وأقصر طريقه لجمع مصفوفتان من نوع int

شكرا

#2

hey i forgot to say

each element of the 2 arrays has only one digit

the 2 arrays have different sizes 30 and 40

and in the array where i will sum the 2 arrays each element must contain only one digit

it starts like this user will input string of numbers then this string will be added into char array then this char array wil be converted to int array , (this for each of the 2 arrays) then i need to compare them ...etc

#3
 
public class sum{

public static void main(String[] args){
int[] a={1,2,3,4};
int[] b={1,2,3,4,5,6};

int sum=0;

for(int i=0l; i<a.length;i++)
sum+=a+j

System.out.println("The sum is: "+sum); }

}
public Money getRich(String name){
return new Money(1,000,000,000 );
}
#4

اتضح إنني كنت خاطئاً فلم أقراً ردك كاملاً ...

هل تريد الناتج كعدد يمثل مجموع عناصر المصفوفة الأولى مع الثانية ؟

أم تريد مصفوفة ثالثة كل عنصر فيها يمثل مجموع عنصران متوازيان في تلك المصفوفتين ؟

public Money getRich(String name){
return new Money(1,000,000,000 );
}
#5

أعتقد أن هذا الذي تقصده

import java.io.*;
public class Sum{

public static void main(String[] args) throws IOException {

BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

String input;

boolean wrong;

int[] firstArray,secondArray,result;

char[] a,b;

do{

wrong=false;

System.out.println("Enter the elements of the 1st Array in this Format: '1234... ':"); // since it will take only   
                                                                                                                                               // one digit

input=stdin.readLine();

a=new char[input.length()];  
                     
for(int i=0;i<input.length();i++){
if(Character.isDigit(input.charAt(i)))
a=input.charAt(i);
else {
wrong=true;	
System.out.println("\nWrong Input...\n");
break;} } }while(wrong);


do{

wrong=false;

System.out.println("Enter the elements of the 2nd Array in this Format: '1234... ':"); // since it will take only   
                                                                                                                                               // one digit

input=stdin.readLine();

b=new char[input.length()];  
                     
for(int i=0;i<input.length();i++){
if(Character.isDigit(input.charAt(i)))
b=input.charAt(i);
else {
wrong=true;	
System.out.println("\nWrong Input...\n");
break;} } }while(wrong);




firstArray=new int[a.length];

secondArray=new int[b.length];

if(firstArray.length<secondArray.length)

result=new int[firstArray.length];

else

result=new int[secondArray.length];

for(int i=0;i<a.length;i++)
firstArray=Integer.parseInt(new Character(a).toString());

for(int i=0;i<b.length;i++)
secondArray=Integer.parseInt(new Character(b).toString());

System.out.print("\nThe resultant Array:\n\n{ ");

for(int i=0;i<result.length;i++){

result=firstArray+secondArray;

if(i!=result.length-1)

System.out.print(result+", ");

else

System.out.print(result);
}

System.out.print(" }\n\n");

}}

تم تعديل هذه المشاركة بواسطة red_knight في 27 أكتوبر 2005 في 00:46

public Money getRich(String name){
return new Money(1,000,000,000 );
}
#6

شكرا اخى red_knight على الاهتمام

هذا الموضوع مغلق.

مواضيع مشابهة