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

لعبة من سيربح المليون " من سيربح دعائي له "

بدأه areej ahmad في 16 مارس 2008 · 24 رد · 2,392 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم و رحمة الله و بركاته ,,

انا بعدي مبتدئه في الجافا ما صار لنا اللا شهر من اخذناها و الدكتور الله يسامحه عطانا اسيمنت اكبر من مستوانا اللا و هو عمل لعبة من سيربح المليون و هو يريد ان نضع 15 سؤال في ARRAY واحد و اذا المتسابق اجاب الاجابه الصحيحه يستطيع الذهاب للسؤال الثاني و اذا لم يجب الاجابه الصحيحه يخرج من اللعبه فأنا حاولت في اللعبه و سوف اعطيكم محاولتي و اريدكم ان تساعدونني الله يجزيكم الجنه

public class gamemillion {
			/** Main method */
 public static void main(String[] args) {

  Str i;


		System.out.println("");
		System.out.println("+====================================================+");
		System.out.println("|  ***	 Who								  ***  |");
		System.out.println("|  ***		  Will							***  |");
		System.out.println("|  ***				Win					   ***  |");
		System.out.println("|  ***					 The Million		  ***  |");
		System.out.println("+====================================================+");

 }

  String[] question = ("The chemical symbol C represents the element
   "Fluorine" 
 ,"Nitrogen" 
 "Carbon" 
 "Helium" ); 

if (i=="Carbon")
 System.out.println("You ARE GOOD,GO TO NEXT QUESTION" + i );
else
	   System.out.println("Sorry!!" + i);
}
#2

اقتراح :

تمثيل السؤال على شكل صف (class) قائم بذاته يتضمن كأعضاء :

صيغة السؤال , الإجابات الأربعة المعطاة للمتسابق (على شكل مصفوفة ) , و رقم الإجابة الصحيحة ..

و يتضمن إجرائية تأخذ كوسيط رقماً صحيحاً (يمثل رقم الإجابة التي يعطيها المتسابق ) و تتحقق من كون الإجابة هي الصحيحة أم لا ..

في البرنامج الرئيسي نعرف مصفوفة الأسئلة (الـ 15)

طرح الأسئلة واحداً تلو الآخر يتم ضمن حلقة تكرارية (while) .. شرط التكرار فيها هو أن يعطي المتسابق اجابة صحيحة

طالما المتسابق يعطي اجابة صحيحة يتم الانتقال إلى السؤال التالي ..

حاولي في هذا الاقتراح .. و سأتابع معك إن شاء الله ..

سؤال : كيف سيقوم المتسابق بإعطاء الإجابة ؟؟

أبو خالد السوري

#3

اشكرك اخي ابو خالد و جزاك الله ألف خير لاني جد محتاجه مساعدة احد ,, اخي الكريم الاجابات المفروض تكون يا مرقمه يا a,b,c,d و على اليوزر ان يدخل الرقم او الحرف الصحيح حتى يتاح له الانتقال الى السؤال التالي ,, و بصراحه لم افهم اقتراحك لانا ما اخذنا شي اسمه مصفوفه في الجافا:( ,,

#4

مصفوفه = ARRAY

أبو خالد السوري

#5

على كل حال ليس من الضروري تمثيل الإجابات الأربعة المعطاة على شكل مصفوفة ..

المهم أن تبدأي بالمحاولة ..

البداية ستكون الصف الممثل للسؤال .. و لنسمه Question

هذا الصف سيحوي

- صيغة السؤال : سلسلة محارف (String)

- الإجابات الأربعة : إما مصفوفة (Array) من سلاسل المحارف , أو أربعة سلاسل محارف منفردة ..

- رقم الإجابة الصحيحة (int)

و يتضمن إجرائية (function/procedure) تأخذ كوسيط رقماً صحيحاً (int) (يمثل رقم الإجابة التي يعطيها المتسابق ) و تتحقق من كون الإجابة هي الصحيحة أم لا .. القيمة المرجعة ستكون true أو false (النمط boolean) ..

في انتظار محاولاتك لنتابع ..

أبو خالد السوري

#6

شاكره لك اخي رحابة صدرك ,,

و لكن بالنسبه لملاحظتك ان - الإجابات الأربعة : إما مصفوفة (Array) من سلاسل المحارف , أو أربعة سلاسل محارف منفردة ..

- رقم الإجابة الصحيحة (int)

كيف اخلي الاجابه الصحيحه تساوي الرقم و كم int راح يكون عندي في البرنامج

#7

عندما ندخل معلومات السؤال في البرنامج , سنعرف أيضاً رقم الإجابة الصحيحة لهذا السؤال ..

مثلاً :

String question = "The chemical symbol C represents the element";
String[] responses = {"Fluorine", "Nitrogen", "Carbon", "Helium" };
int correctResponse = 3;

الأن عندما يُدخل المستخدم إجابته (رقم من 1 إلى 4) سنقارن هذا الرقم مع رقم الإجابة الصحيحة المخزنة سلفاً في البرنامج ..

إذاً سيكون لدينا int واحد معرف في الصف Question و هو رقم الإجابة الصحيحة ..

و في البرنامج سيكون لدينا int واحد يمثل الإجابة التي يعطيها المتسابق .. (قيمته تتغير عندما يعطي المتسابق إجابة عن سؤال معين)

تم تعديل هذه المشاركة بواسطة أبو خالد السوري في 17 مارس 2008 في 21:21

أبو خالد السوري

#8

شاكره لك اخي تفاعلك معي ,,

عدلت على البرنامج و بدا لي هكذا و لكن لايزال ال error قائم :(

public class gamemillion {

/** Main method */

public static void main(String[] args) {

int correctResponse;

}

String[] question = {"The chemical symbol C represents the element"};

String[] responses = {"Fluorine", "Nitrogen", "Carbon", "Helium" };

int correctResponse = 3;

int correctResponse = JOptionPane.showInputDialog("Enter your answer please,");

}

#9

:S :S

حسناً

كما أشرت في البداية سنحتاج إلى صف (class) يمثل السؤال .. سنسميه Question

هذا الصف سيحوي

- صيغة السؤال : سلسلة محارف (String)

- الإجابات الأربعة : مصفوفة (Array) من سلاسل المحارف

- رقم الإجابة الصحيحة (int)

public class Question {
	 String question; 
	 String[] responses; 
	 int correctResponse; 
	 ... 
}

باني الصف (constructor) سيأخذ ثلاث وسطاء (parameters) و يقوم بإسنادها إلى أعضاء الصف :

public Question(String question, String[] responses, int correctResponse) {
	this.question = question;
	this.responses = responses;
	this.correctResponse = correctResponse;
}

سيلزمنا أيضاً إجرائية تأخذ كوسيط رقماً صحيحاً (يمثل رقم الإجابة التي يعطيها المتسابق ) و تتحقق من كون الإجابة هي الصحيحة أم لا ..

public boolean checkCorecteness(int userAnswer){
	 /* اترك لك هنا الفرصة لتحاولي كتابة الكود بنفسك  */

}

سيلزمنا أيضاً إجرائية لإظهار السؤال للمتسابق مع الإجابات الأربعة المحتملة .. و تسمح للمتسابق بإدخال إجابته .. و بما أنك أشرت إلى استخدام JOptionPane.showInputDialog فيمكننا استخدامها لهذا الغرض بحيث يظهر السؤال كما في الشكل التالي :

post-152370-1205791239_thumb.png

القيمة المعادة من showInputDialog هي من النمط String ..

String response = JOptionPane.showInputDialog(   );

لتحويلها إلى int :

int userAnswer = new Integer(response);

لتصبح إجرائية طرح السؤال بالشكل التالي :

public int askQuestion(){
	String response = JOptionPane.showInputDialog(question +
			"\n1. "+responses[0]+
			"\n2. "+responses[1]+
			"\n3. "+responses[2]+
			"\n4. "+responses[3]);		
	int userAnswer = new Integer(response);
	return userAnswer; 
}

في البرنامج الرئيسي سنصرح عن مصفوفة الأسئلة الـ 15 ..

Question[] questions = new Question[15];

و نعرّف الأسئلة واحداً تلو الآخر ..

String[] responses0 = {"Fluorine", "Nitrogen", "Carbon", "Helium" };
questions[0] = new Question("The chemical symbol C represents the element", responses0,3);

String[] responses1 = {"Beirut", "Damascus", "Aleppo", "Homs" };
questions[1] = new Question("Which is the capital of Syria", responses1, 2);

...

الآن لم يبقى إلا القليل :

طرح هذه الأسئلة على المتسابق من خلال حلقة تكرارية (while أو for) و إذا أعطى المتسابق إجابة خطأ يتم الخروج من البرنامج ..

هنا أيضاً اترك لك الفرصة لتحاولي كتابة الكود بنفسك ..

تم تعديل هذه المشاركة بواسطة أبو خالد السوري في 18 مارس 2008 في 01:08

أبو خالد السوري

#10

اشكرك اخي ابو خالد و الله ماني عارفه ايش اقولك بجد اخجلتني بمساعدتك و الله اني خجلانه منك كثييييييير ,, و لكن ما اقول الله يجعله في ميزان اعمالك و الله يستجيب دعائي لك بظهر الغيب ,,

بالنسبه للبرنامج راح احاول فيه الحين بعد مساعدتك الطيبه و الله يقدرنا على رد الجزاء ,,

بحاول و بشوف و ارد لك خبر ,, مع جزيل الشكر

#11

هناك استفسار اخي الكريم ابو خالد ,,

ال constructor يكون داخل ال class ولا statement لوحده يعني يكون بهالطريقه

public class Question {String question, String[] responses, int correctResponse

String question;

String[] responses;

int correctResponse;

...

}

و لماذا كررنا تعريف

this.question = question;

this.responses = responses;

this.correctResponse = correctResponse;

و شكرا

#12

الباني (Constructor) يكون داخل ال class .. و الغاية منه تهيئة الصف من خلال إسناد قيم لأعضائه ..

هذا الباني يتم استدعاءه عند تعريف كائن جديد من هذا الصف .. مثلاً :

questions[0] = new Question("The chemical symbol C represents the element", responses0,3);

----- ----- -----

اقتباس
this.question = question;
this.responses = responses;
this.correctResponse = correctResponse;

ليس هذا تكرار في التعريف .. هذه عملية اسناد الوسطاء (parameters) الممررة للباني (على يمين اشارة = ) إلى أعضاء الصف (على يسار اشارة = و المسبوقة بـ this )

لازالة الالتباس يمكنك تغيير اسماء الوسطاء ..

public Question(String question1, String[] responses1, int correctResponse1) {
	question = question1;
	responses = responses1;
	correctResponse = correctResponse1;
}

أبو خالد السوري

#13

اخي الكريم ابو خالد ,, عدلت على البرنامج بعد مساندتك الجزيله و لكن لايزال هناك اروور و لا اعرف اين حلها ,,

و هذا هو الكود

public class Question(String question, String[] responses, int correctResponse){

this.question = question;

this.responses = responses;

this.correctResponse = correctResponse;

boolean checkCorecteness(int userAnswer){

for(String[] responses = 1; responses<=15; responses++){

if(userAnswer == responses)

responses++;

else

return userAnswer;

}

}

int userAnswer = new Integer(response);

int askQuestion(){

String response = JOptionPane.showInputDialog(question +

"\n1. "+responses[1]+

"\n2. "+responses[2]+

"\n3. "+responses[3]+

"\n4. "+responses[4]);

int userAnswer = new Integer(response);

return userAnswer;

}

Question[] questions = new Question[15];

String[] responses1 = {"Fluorine", "Nitrogen", "Carbon", "Helium" };

questions[1] = new Question("The chemical symbol C represents the element", responses1,3);

String[] responses2 = {"Moose", "Deer", "Fish", "Frog"};

questions[2] = new Question("Which one of the following animals is an amphibian", responses2,4);

String[] responses3 = {"2 ÷ 3", "2 + 3", "2 × 3", "2 - 3" };

questions[3] = new Question("Which of the following expressions is the largest", responses3,3);

String[] responses4 = {"Liquid", "Solid", "Plasma", "Gas" };

questions[4] = new Question("A rock is an example of a", responses4,2);

String[] responses5 = {"11", "33", "24", "21" };

questions[5] = new Question("What is the average of 7, 14 and 12", responses5,1);

String[] responses6 = {"Mantle", "Crust", "Magma", "Core" };

questions[6] = new Question("The center of the Earth is also known as the...", responses6,4);

String[] responses7 = {"South", "East", "West", "North" };

questions[7] = new Question("In the southern hemisphere, the sun rises in the...", responses7,2);

String[] responses8 = {"60", "80", "40", "20" };

questions[8] = new Question("Which one of the following numbers is a factor of 100", responses8,4);

String[] responses9 = {"1", "2", "7", "3" };

questions[9] = new Question("How many element are in a molecule of sulfuric acid (H2SO4)", responses9,4);

String[] responses10 = {"Chlorophyll", "Mitochondria", "Ribosomes", "Chloroplasts" };

questions[10] = new Question("Which organelle produces proteins", responses10,3);

String[] responses11 = {"Digestive system", "Circulatory system", "Respiratory system", "Skeletal system" };

questions[11] = new Question("The group of organs that take in oxygen from the air is called the", responses11,3);

String[] responses12 = {"Thomas Edison", "Sir Issac Newton", "Lise Meitner", "Carl Sagan" };

questions[12] = new Question("Which of these scientists has an element named for them", responses12,2);

String[] responses13 = {"Green light only", "Red light only", "Yellow light only", "All colors" };

questions[13] = new Question("White light is made up of...", responses13,4);

String[] responses14 = {"Transpiration", "Stomata", "Cytoplasm", "Chlorophyll" };

questions[14] = new Question("What material helps plants absorb energy from sunlight", responses14,4);

String[] responses15 = {"Fahrenheit", "Celsius", "Farad", "Kelvin" };

questions[15] = new Question("Which of the following is not a unit of temperature", responses15,3);

}

#14

ملاحظة 1 :

في لغة الجافا , ترقيم العناصر في مصفوفة يبدأ بـ 0 و ليس بـ 1 ..

لذا السؤال الأول سيكون questions[0] و السؤال الأخير questions[14]

ملاحظة 2 :

للتأكد من كون إجابة المتسابق صحيحة أم لا .. يكفي مقارنتها برقم الإجابة الصحيحة (المخزنة سلفاً مع السؤال ) :

public boolean checkCorrecteness(int userAnswer) {
	if(userAnswer == correctResponse){
		return true;
	} else{
		return false;
	}
}

ملاحظة 3 :

تعريف الأسئلة ينبغي أن يتم ضمن التابع الرئيسي (main) و ليس ضمن الصف Question مباشرة ..

التابع main يمكن وضعه ضمن الصف Question .. و لكن الأفضل وضعه في صف مختلف و ليكن اسمه Game ..

عند المرور على الأسئلة و احداً تلو الآخر (ضمن حلقة الـ for)

يتم طرح السؤال على المستخدم و اخذ اجابته باستخدام ..

int userAnswer = questions.askQuestion();

حيث i هي رقم السؤال الحالي ..

ثم يتم التأكد من كون الإجابة صحيحة أم لا باستخدام ..

questions.checkCorrecteness(userAnswer);

فاذا كانت الإجابة خاطئة تظهر رسالة معينة للمستخدم بهذا الخصوص و يتم الخروج من البرنامج ..

System.out.println("Wrong Answer !!, you lost ..");
System.exit(0);

أما إذا كانت الإجابة صحيحة فيتم الانتقال إلى السؤال التالي مباشرة (لهذا استخدمنا حلقة for)

إذا أجاب المستخدم على جميع الأسئلة الـ 15 إجابات صحيحة (بعد انتهاء حلقة for بدون الخروج من البرنامج) فستظهر رسالة للمتسابق تفيد أنه قد ربح المليون ..

الصف Question

import javax.swing.JOptionPane;

public class Question {

	String question;
	String[] responses;
	int correctResponse;

	public Question(String question, String[] responses, int correctResponse) {
		this.question = question;
		this.responses = responses;
		this.correctResponse = correctResponse;
	}

	public boolean checkCorrecteness(int userAnswer) {
		if(userAnswer == correctResponse){
			return true;
		} else{
			return false;
		}
	}

	public int askQuestion() {
		String response = JOptionPane.showInputDialog(question + "\n1. "
				+ responses[0] + "\n2. " + responses[1] + "\n3. "
				+ responses[2] + "\n4. " + responses[3]);
		int userAnswer = new Integer(response);
		return userAnswer;
	}
}

الصف Game

public class Game {
	public static void main(String[] args) {

		Question[] questions = new Question[15];

		String[] responses1 = { "Fluorine", "Nitrogen", "Carbon", "Helium" };

		questions[0] = new Question("The chemical symbol C represents the element", responses1, 3);

		String[] responses2 = { "Moose", "Deer", "Fish", "Frog" };

		questions[1] = new Question("Which one of the following animals is an amphibian",responses2, 4);

		String[] responses3 = { "2 ÷ 3", "2 + 3", "2 × 3", "2 - 3" };

		questions[2] = new Question("Which of the following expressions is the largest",responses3, 3);

		String[] responses4 = { "Liquid", "Solid", "Plasma", "Gas" };

		questions[3] = new Question("A rock is an example of a", responses4, 2);

		String[] responses5 = { "11", "33", "24", "21" };

		questions[4] = new Question("What is the average of 7, 14 and 12",responses5, 1);

		String[] responses6 = { "Mantle", "Crust", "Magma", "Core" };

		questions[5] = new Question("The center of the Earth is also known as the...", responses6,4);

		String[] responses7 = { "South", "East", "West", "North" };

		questions[6] = new Question("In the southern hemisphere, the sun rises in the...",responses7, 2);

		String[] responses8 = { "60", "80", "40", "20" };

		questions[7] = new Question("Which one of the following numbers is a factor of 100",responses8, 4);

		String[] responses9 = { "1", "2", "7", "3" };

		questions[8] = new Question("How many element are in a molecule of sulfuric acid (H2SO4)",responses9, 4);

		String[] responses10 = { "Chlorophyll", "Mitochondria", "Ribosomes","Chloroplasts" };

		questions[9] = new Question("Which organelle produces proteins",responses10, 3);

		String[] responses11 = { "Digestive system", "Circulatory system","Respiratory system", "Skeletal system" };

		questions[10] = new Question("The group of organs that take in oxygen from the air is called the",responses11, 3);

		String[] responses12 = { "Thomas Edison", "Sir Issac Newton","Lise Meitner", "Carl Sagan" };

		questions[11] = new Question("Which of these scientists has an element named for them",	responses12, 2);

		String[] responses13 = { "Green light only", "Red light only","Yellow light only", "All colors" };

		questions[12] = new Question("White light is made up of...",responses13, 4);

		String[] responses14 = { "Transpiration", "Stomata", "Cytoplasm","Chlorophyll" };

		questions[13] = new Question("What material helps plants absorb energy from sunlight",responses14, 4);

		String[] responses15 = { "Fahrenheit", "Celsius", "Farad", "Kelvin" };

		questions[14] = new Question("Which of the following is not a unit of temperature",	responses15, 3);


		for (int i = 0; i < 15; i++) {
			int userAnswer = questions.askQuestion();

			if(!questions.checkCorrecteness(userAnswer)){
				System.out.println("Wrong Answer !!, you lost ..");
				System.exit(0);
			}
		}

		System.out.println("Congratulations, you got the 1.000.000.000 ");

	}
}

أبو خالد السوري

#15

ملاحظة أخيرة :

السؤال السابع (questions[6] ) :

اقتباس
In the southern hemisphere, the sun rises in the

الجواب الصحيح هو 4 (North) و ليس 2 (East) لأنه في القطب الجنوبي كل الاتجاهات هي : شمال ..

أبو خالد السوري

#16

جزاك الله المراتب العلا من الجنه ,, و الله يا اخي الكريم اقدر لك جهدك و سعة صدرك و لا اعرف ماذا اقول ,,

اللا ان يجعله الله في ميزان اعمالك ,,

:$

#17

أهلاً بك أخت أريج

أتمنى أن يكون هذا الموضوع قد حقق لك الفائدة المرجوة منه ..

و أن تكوني قد استفدتي منه في تعلم أشياء جديدة عليك في الجافا ..

أبو خالد السوري

#18

مرحبا اخي ابو خالد ,, لقد اتممنا البرنامج على بركة الله و لكن مازال هناك اروور واحد و اظن انه بسيط ,,و لا اعرف اين هو بالضبط !! و لماذا عندنا تو main ,,

و هناك سؤال بسيط لو طلب منا انه عندما تبدأ اللعبة من جديد يعني اي لاعب يدخل للعبة مش شرط يبدأ مع اول سؤال وضع بل اي سؤال عندنا يكون هو اول سؤال ,,

و سؤال اخر ماذا لو طلب اليوزر ان يحذف اجابتين

و شكرا ,,,

import javax.swing.JOptionPane;

public class Question {

public static void main(String[] args)

String question;

String[] responses;

int correctResponse;

public Question(String question, String[] responses, int correctResponse) {

this.question = question;

this.responses = responses;

this.correctResponse = correctResponse;

}

public boolean checkCorrecteness(int userAnswer) {

if(userAnswer == correctResponse){

return true;

} else{

return false;

}

}

public int askQuestion() {

String response = JOptionPane.showInputDialog(question + "\n1. "

+ responses[0] + "\n2. " + responses[1] + "\n3. "

+ responses[2] + "\n4. " + responses[3]);

int userAnswer = new Integer(response);

return userAnswer;

}

}

public class Game {

public static void main(String[] args) {

Question[] questions = new Question[15];

String[] responses1 = { "Fluorine", "Nitrogen", "Carbon", "Helium" };

questions[0] = new Question("The chemical symbol C represents the element", responses1, 3);

String[] responses2 = { "Moose", "Deer", "Fish", "Frog" };

questions[1] = new Question("Which one of the following animals is an amphibian",responses2, 4);

String[] responses3 = { "2 ÷ 3", "2 + 3", "2 × 3", "2 - 3" };

questions[2] = new Question("Which of the following expressions is the largest",responses3, 3);

String[] responses4 = { "Liquid", "Solid", "Plasma", "Gas" };

questions[3] = new Question("A rock is an example of a", responses4, 2);

String[] responses5 = { "11", "33", "24", "21" };

questions[4] = new Question("What is the average of 7, 14 and 12",responses5, 1);

String[] responses6 = { "Mantle", "Crust", "Magma", "Core" };

questions[5] = new Question("The center of the Earth is also known as the...", responses6,4);

String[] responses7 = { "South", "East", "West", "North" };

questions[6] = new Question("In the southern hemisphere, the sun rises in the...",responses7, 2);

String[] responses8 = { "60", "80", "40", "20" };

questions[7] = new Question("Which one of the following numbers is a factor of 100",responses8, 4);

String[] responses9 = { "1", "2", "7", "3" };

questions[8] = new Question("How many element are in a molecule of sulfuric acid (H2SO4)",responses9, 4);

String[] responses10 = { "Chlorophyll", "Mitochondria", "Ribosomes","Chloroplasts" };

questions[9] = new Question("Which organelle produces proteins",responses10, 3);

String[] responses11 = { "Digestive system", "Circulatory system","Respiratory system", "Skeletal system" };

questions[10] = new Question("The group of organs that take in oxygen from the air is called the",responses11, 3);

String[] responses12 = { "Thomas Edison", "Sir Issac Newton","Lise Meitner", "Carl Sagan" };

questions[11] = new Question("Which of these scientists has an element named for them", responses12, 2);

String[] responses13 = { "Green light only", "Red light only","Yellow light only", "All colors" };

questions[12] = new Question("White light is made up of...",responses13, 4);

String[] responses14 = { "Transpiration", "Stomata", "Cytoplasm","Chlorophyll" };

questions[13] = new Question("What material helps plants absorb energy from sunlight",responses14, 4);

String[] responses15 = { "Fahrenheit", "Celsius", "Farad", "Kelvin" };

questions[14] = new Question("Which of the following is not a unit of temperature",responses15, 3);

for (int i = 0; i < 15; i++) {

int userAnswer = questions.askQuestion();

if(!questions.checkCorrecteness(userAnswer)){

System.out.println("Wrong Answer !!, you lost ..");

System.exit(0);

}

}

System.out.println("Congratulations, you got the 1.000.000.000 ");

}

}

#19

أخت أريج ليس هناك سوى main واحد فقط (في الصف Game) .. كما في الكود في المشاركة (14) ..

--------------------------------

اقتباس
عندما تبدأ اللعبة من جديد يعني اي لاعب يدخل للعبة مش شرط يبدأ مع اول سؤال وضع بل اي سؤال عندنا يكون هو اول سؤال ,,

هل المقصود اختيار جميع الأسئلة بشكل عشوائي ، أم فقط السؤال الأول .. ؟

يمكن استخدام Math.random() لتوليد رقم عشوائي بين الصفر و الواحد ..

و بالتالي

int index = (int) (Math.random() * 15);

ستعطي رقم عشوائي من 0 إلى 14 يكون هو رقم السؤال المختار عشوائياً ..

--------------------------------

لإضافة إمكانية حذف إجابتين

ينبغي أولاً أن نحدد لكل سؤال ما هما الإجابتان اللتان ستبقيان بعد الحذف (بحيث تكون الإجابة الصحيحة بينهما ) ..

أي أن نضيف إلى الصف Question عضوين جديدين هما رقما الإجابتين اللتان ستبقيان ..

و عندما يطلب المتسابق حذف الإجابتين .. نعيد عليه طرح نفس السؤال و لكن مع إجابتين فقط بدل أربعة ..

أبو خالد السوري

#20

اخي الكريم ماذا يقصد بــ ,,؟؟؟

class Question is public, should be declared in a file named Question.java

#21

يعني أنه في لغة الجافا .. كل صف عام (public class) يجب وضعه في ملف خاص به يحمل نفس اسم الصف و اللاحقة تكون .java طبعاً ..

و لا يجوز وضع صفين عامين في نفس الملف ..

(يمكن في الملف الواحد أن نضع ما نشاء من صفوف , و لكن واحد منها فقط يجب أن يكون عام public )

في حالة برنامجنا .. إن كنت وضعت الصفين Question و Game في نفس الملف فهذا هو سبب ظهور الخطأ :

اقتباس
class Question is public, should be declared in a file named Question.java

الحلول الممكنة :

إما أن تبقي الصفين في نفس الملف على أن لا يكون Question عاماً .. أي أن تحذفي كلمة public من بداية التصريح عن الصف ..

class Question {

...

}

في هذه الحالة اسم الملف الحاوي على كلا الصفين يجب أن يكون هو اسم الصف العام الوحيد و هو Game في هذه الحالة .. أي اسم الملف يكون Game.java

الحل الثاني أن تضعي كل صف في ملف خاص به يحمل اسم نفس الصف ..

أي ضعي الصف Question في ملف Question.java

و الصف Game في ملف Game.java

أبو خالد السوري

#22

اخي الكريم ,, وضعت الـصف Question بدون كلمة public في ملف يحوي الصفين question و game ,,

import javax.swing.JOptionPane;

class Question {

String question;

String[] responses;

int correctResponse;

public Question(String question, String[] responses, int correctResponse) {

this.question = question;

this.responses = responses;

this.correctResponse = correctResponse;

}

public boolean checkCorrecteness(int userAnswer) {

if(userAnswer == correctResponse){

return true;

} else{

return false;

}

}

public int askQuestion() {

String response = JOptionPane.showInputDialog(question + "\n1. "

+ responses[0] + "\n2. " + responses[1] + "\n3. "

+ responses[2] + "\n4. " + responses[3]);

int userAnswer = new Integer(response);

return userAnswer;

}

}

public class Game {

public static void main(String[] args) {

Question[] questions = new Question[15];

String[] responses1 = { "Fluorine", "Nitrogen", "Carbon", "Helium" };

questions[0] = new Question("The chemical symbol C represents the element", responses1, 3);

String[] responses2 = { "Moose", "Deer", "Fish", "Frog" };

questions[1] = new Question("Which one of the following animals is an amphibian",responses2, 4);

String[] responses3 = { "2 ÷ 3", "2 + 3", "2 × 3", "2 - 3" };

questions[2] = new Question("Which of the following expressions is the largest",responses3, 3);

String[] responses4 = { "Liquid", "Solid", "Plasma", "Gas" };

questions[3] = new Question("A rock is an example of a", responses4, 2);

String[] responses5 = { "11", "33", "24", "21" };

questions[4] = new Question("What is the average of 7, 14 and 12",responses5, 1);

String[] responses6 = { "Mantle", "Crust", "Magma", "Core" };

questions[5] = new Question("The center of the Earth is also known as the...", responses6,4);

String[] responses7 = { "South", "East", "West", "North" };

questions[6] = new Question("In the southern hemisphere, the sun rises in the...",responses7, 2);

String[] responses8 = { "60", "80", "40", "20" };

questions[7] = new Question("Which one of the following numbers is a factor of 100",responses8, 4);

String[] responses9 = { "1", "2", "7", "3" };

questions[8] = new Question("How many element are in a molecule of sulfuric acid (H2SO4)",responses9, 4);

String[] responses10 = { "Chlorophyll", "Mitochondria", "Ribosomes","Chloroplasts" };

questions[9] = new Question("Which organelle produces proteins",responses10, 3);

String[] responses11 = { "Digestive system", "Circulatory system","Respiratory system", "Skeletal system" };

questions[10] = new Question("The group of organs that take in oxygen from the air is called the",responses11, 3);

String[] responses12 = { "Thomas Edison", "Sir Issac Newton","Lise Meitner", "Carl Sagan" };

questions[11] = new Question("Which of these scientists has an element named for them", responses12, 2);

String[] responses13 = { "Green light only", "Red light only","Yellow light only", "All colors" };

questions[12] = new Question("White light is made up of...",responses13, 4);

String[] responses14 = { "Transpiration", "Stomata", "Cytoplasm","Chlorophyll" };

questions[13] = new Question("What material helps plants absorb energy from sunlight",responses14, 4);

String[] responses15 = { "Fahrenheit", "Celsius", "Farad", "Kelvin" };

questions[14] = new Question("Which of the following is not a unit of temperature",responses15, 3);

for (int i = 0; i < 15; i++) {

int userAnswer = questions.askQuestion();

if(!questions.checkCorrecteness(userAnswer)){

System.out.println("Wrong Answer !!, you lost ..");

System.exit(0);

}

}

System.out.println("Congratulations, you got the 1.000.000.000 ");

}

}

ظهر لي الارور ,,

class Game is public, should be declared in a file named Game.java

:( :(

#23

ماذا اسميتي هذا الملف ؟

يجب أن يكون اسمه Game.java

انتبهي أن يكون الحرف الأول كبيراً (G) و ليس (g) .. أي مطابقاً تماماً لاسم الصف ..

Game.rar

تم تعديل هذه المشاركة بواسطة أبو خالد السوري في 20 مارس 2008 في 18:12

أبو خالد السوري

#24

جزاك الله خير يا ابو خالد ,, و الله يناولك اللي في بالك ,, و يوفقك دنيا و اخيره ,, و سامحني على الازعاج

#25

فكره جميله تستاهل التطوير

الى الامــآم........

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

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…