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

missing return statment

بدأه يارب بك استغيث في 28 نوفمبر 2010 · 8 رد · 415 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع
import java.io.*;
public class WriteData


{		int length=6;
		int A[]=new int[length];



		public void insert()
		{
		for(int j=0;j<A.length;j++)
		A[j]=j;

		}
		public  int show()
		{ 


 for(int i=0;i<A.length;i++)
            return A;

		}


	public static void main(String[]args)throws Exception
	{	
	WriteData o=new WriteData();
	o.insert();
	System.out.println(o.show());
		File file=new File("Scores.TXT");
	if(file.exists())
		{
			System.out.println("file already exists");
			System.exit(0);
		}
		//create a file 
		PrintWriter output=new PrintWriter(file);
		//write formattes output

	output.print(o.show());



		output.close();

	}
}

الكود ده فيه error فى ال show method بيقولmissing return starment

#2

الخطا يتلخص لديك فى انك قومت بعمل return داخل الـ for loop

وبالتالى فالداله لم ترى كلمه return لانها داخل الاقواس الخاصه بالـ for loop

هتقوم بتعريف متغير بداخل الداله

وتقوم بحفظ القيمه بداخله هكذا

x = A;

وتقوم بعدها بعمل return لهذا المتغير

كما تم تعديله فالكود

/**
 *
 * @author Mahmoud Kelany
 */
import java.io.*;

public class WriteData {

	int length = 6;
	int A[] = new int[length];

	public void insert() {
    	for (int j = 0; j < A.length; j++) {
        	A[j] = j;
    	}
	}

	public int show() {
    	int x = 0;
    	for (int i = 0; i < A.length; i++) {
        	x = A;
    	}
    	return x;
	}

	public static void main(String[] args) throws Exception {
    	WriteData o = new WriteData();
    	o.insert();
    	System.out.println(o.show());
    	File file = new File("Scores.TXT");
    	if (file.exists()) {
        	System.out.println("file already exists");
        	System.exit(0);
    	}
    	//create a file
    	PrintWriter output = new PrintWriter(file);
    	//write formattes output
    	output.print(o.show());
    	output.close();
	}
}

بالتوفيق :)

Software Developer
Mahmoudkelany.com


 

#3

بيطبع الرقم 5 بس

مش كل عناصر الarray

#4
يارب بك استغيث كتب:

بيطبع الرقم 5 بس

مش كل عناصر الarray

لو عاوزه تطبعى الكل

يقبى يا تعمل return to array

وبعدين تطبعيه تانى فـ for loop

يا اما تطبعى على طول اذا كان الغرض الطباعه فقط

هكذا

/**
 *
 * @author Mahmoud Kelany
 */
import java.io.*;

public class WriteData {

	int length = 6;
	int A[] = new int[length];

	public void insert() {
    	for (int j = 0; j < A.length; j++) {
        	A[j] = j;
    	}
	}

	public void show() {
    	for (int i = 0; i < A.length; i++) {
        	System.out.println(A);
    	}
	}

	public static void main(String[] args) throws Exception {
    	WriteData o = new WriteData();
    	o.insert();
    	o.show();
    	File file = new File("Scores.TXT");
    	if (file.exists()) {
        	System.out.println("file already exists");
        	System.exit(0);
    	}
    	//create a file
    	PrintWriter output = new PrintWriter(file);
    	//write formattes output
    	o.show();
    	output.close();
	}
}

Software Developer
Mahmoudkelany.com


 

#5

الهدف الاساسى الكتابه على file

#6
import java.io.*;

public class WriteData {

    int length = 6;
    int A[] = new int[length];

    public void insert() {
        for (int j = 0; j < A.length; j++) {
            A[j] = j;
        }
    }

    public int[] show() { //return all elements
        return A;
    }

    public int show(int pos) {  //return single element
        return A[pos - 1];
    }

    public static void main(String[] args) throws Exception {
        WriteData o = new WriteData();
        o.insert();
        System.out.println(o.show(3)); //as example
        File file = new File("Scores.TXT");
        if (file.exists()) {
            System.out.println("file already exists");
            System.exit(0);
        }
        //create a file
        PrintWriter output = new PrintWriter(file);
        //write formattes output
        for (int i = 0; i < o.show().length; i++) {
            output.println(o.show(i + 1));
            System.out.println(o.show(i + 1));
        }
        output.close();
    }
}

تم تعديل هذه المشاركة بواسطة Ahmed-Ghanem في 28 نوفمبر 2010 في 19:38

2coscqr.jpg
#7
Ahmed-Ghanem كتب:

import java.io.*;

public class WriteData {

    int length = 6;
    int A[] = new int[length];

    public void insert() {
        for (int j = 0; j < A.length; j++) {
            A[j] = j;
        }
    }

    public int[] show() { //return all elements
        return A;
    }

    public int show(int pos) {  //return single element
        return A[pos - 1];
    }

    public static void main(String[] args) throws Exception {
        WriteData o = new WriteData();
        o.insert();
        System.out.println(o.show(3)); //as example
        File file = new File("Scores.TXT");
        if (file.exists()) {
            System.out.println("file already exists");
            System.exit(0);
        }
        //create a file
        PrintWriter output = new PrintWriter(file);
        //write formattes output
        for (int i = 0; i < o.show().length; i++) {
            output.println(o.show(i + 1));
            System.out.println(o.show(i + 1));
        }
        output.close();
    }
}

شكرا جدا جداا..فعلا كده بيطبع عناصر ال array كلها

جزاكم الله كل خير

وشكرا على المتابعه السريعه المستمره

#8
يارب بك استغيث كتب:

الهدف الاساسى الكتابه على file

معذره انا كان هدفى انى اصلح الخطأ اللى حضرتك قولتى عليه

انما انا مكنتش عارف هدف الكود ككل

حصل خير اهم حاجه انه اتصلح :)

بالتوفيق :)

Software Developer
Mahmoudkelany.com


 

#9

لحضرتك كل الشكر والتقدير

وربنا يعوضك كل خير عن وقتك ومجهودك الى بذلتو عشان تساعدنى

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