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

مشكلة في برمجة الarray

مغلق
بدأه maas في 8 يوليو 2007 · 4 رد · 453 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

يا جماعة انا مبتدىء في الجافا و أرجو مساعدتي

البرنامج يتطلب ان الuser يدخل 3 أرقام و يقوم بعملية الجمع باستخدام الarray و الfor loop

و هذي الcode و حاولت تعديله و لكن للأسف لم أعرف

و هذا هو الكود

import java.io.*;
public class q {

	public static void main(String[] args) {
	float []num;	
	 num= new float [3];
	float i;
	float sum;
	System.out.println("Enter 3 numbers");
	for (i=0;i<3;i++){
		InputStreamReader istream = new InputStreamReader (System.in);
		BufferedReader br = new BufferedReader (istream);
		try{
		 num=br.readLine();
			 sum = sum + num;
			System .out.println (sum); 
		}
		catch (IOException err){
			System.out.println("Error");
		}
	}

	}
}
#2
1  import java.io.*;
 2  public class q {
 3
 4	  public static void main(String[] args) {
 5	  float []num;
 6	   num= new float [3];
 7	  //float i;		  /// for loop variable should be of type int not float 
 8	  int i;					  // this done by me istead of the prev. line
 9	  float sum = 0;			   // I who add "=0" to initialize it
10	  System.out.println("Enter 3 numbers");
11
12	  for (i=0;i<3;i++){
13		  InputStreamReader istream = new InputStreamReader (System.in);
14		  BufferedReader br = new BufferedReader (istream);
15		  /*
16		   *  You may use java.util.Scanner to input data at the command line instead of InputStream, Buffered...
17		   */
18		  try{
19		   //num= br.readLine();			   // readLine returns String, and num of type float, you have to cast
20		   num = Float.valueOf ( br.readLine() );
21			   sum = sum + num;  // you used some before init it, you have to do that at line 9
22			  System .out.println (sum);
23		  }
24		  catch (IOException err){
25				  //System.out.println("Error");		  // i comment this
26				  err.printStackTrace();				  // and add this to show the error
27		  }
28	  }
29
30	  }
31  }
#3

مشكور أخوي على التعديل و يعطيك العافية

و لكن ما المقصود من java.util.Scanner ؟؟

حاولت اني أمحي الBuffered و الInput و وضعت java.util.scanner و طلع لي error ؟؟

#4

i am sorry that i cannot write in Arabic,

java.util.Scanner is a class that more simple in reading user input from the standard input:

ex:

import java.util.*;

Scanner s = new Scanner ( System.in);
int x = s.nextInt();
int y = s.nextInt();

benefits:

in your way ( Bufferedreder,... ) you have to enter each number in a separate line, but with scanner you not, you can enter them in one line separating them by spaces , also you haven't make cast, as nextInt returns int , readDouble returns double ,.. and so on.

#5

مشكور أخوي على التوضيح

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

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