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

Hoe can I stop this by using sentinel-controlled loop?

مغلق
بدأه husam في 12 نوفمبر 2002 · 7 رد · 1,159 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

alslam wa alykom

Can anyone help me to stop this program by using sentinel-controlled loop? It is for my project in the

university

Sorry for not writing in Arabic

// husam

[CODe2]

import javax.swing.JOptionPane;

public class QuizProgram

{

public static void main( String args[] )

{

String firstQuestion,

secondQuestion,

thiredQuestion,

fourthQuestion;

int answer1,//answer qution1 by user

answer2,// answer question2

answer3,//answer question

answer4; // answer question

int sentinel =0;

do{

firstQuestion=JOptionPane.showInputDialog(null, "How many oceans are they in world? ");

answer1 =Integer.parseInt( firstQuestion );

if(answer1==4)

JOptionPane.showMessageDialog(null,"The answer is correct");

else

JOptionPane.showMessageDialog(null,"your answer is wrong you should try again");

}while(answer1!=4);

do{

secondQuestion=JOptionPane.showInputDialog(null, " How many stats in USA? ");

answer2 =Integer.parseInt( secondQuestion );

if(answer2==50)

JOptionPane.showMessageDialog(null,"your answer is correct");

else

JOptionPane.showMessageDialog(null,"your answer is wrong");

}while(answer2!=50);

do{

thiredQuestion=JOptionPane.showInputDialog(null, " How many countries in Europe? ");

answer3 =Integer.parseInt( thiredQuestion );

if(answer3==12)

JOptionPane.showMessageDialog(null,"your answer is correct");

else

JOptionPane.showMessageDialog(null,"your answer is wrong you should try again");

}while(answer3!=12);

do{

fourthQuestion=JOptionPane.showInputDialog(null, "How many continents in the world? ");

answer4 =Integer.parseInt( fourthQuestion );

if(answer4==6)

JOptionPane.showMessageDialog(null,"your answer is correct");

else

JOptionPane.showMessageDialog(null,"your answer is wrong");

}while(answer4!=6);

}

}

[/CODe2]

thank you

// husam

[CODe2]

import javax.swing.JOptionPane;

public class QuizProgram

{

public static void main( String args[] )

{

String firstQuestion,

secondQuestion,

thiredQuestion,

fourthQuestion;

int answer1,//answer qution1 by user

answer2,// answer question2

answer3,//answer question

answer4; // answer question

int sentinel =0;

do{

firstQuestion=JOptionPane.showInputDialog(null, "How many oceans are they in world? ");

answer1 =Integer.parseInt( firstQuestion );

if(answer1==4)

JOptionPane.showMessageDialog(null,"The answer is correct");

else

JOptionPane.showMessageDialog(null,"your answer is wrong you should try again");

}while(answer1!=4);

do{

secondQuestion=JOptionPane.showInputDialog(null, " How many stats in USA? ");

answer2 =Integer.parseInt( secondQuestion );

if(answer2==50)

JOptionPane.showMessageDialog(null,"your answer is correct");

else

JOptionPane.showMessageDialog(null,"your answer is wrong");

}while(answer2!=50);

do{

thiredQuestion=JOptionPane.showInputDialog(null, " How many countries in Europe? ");

answer3 =Integer.parseInt( thiredQuestion );

if(answer3==12)

JOptionPane.showMessageDialog(null,"your answer is correct");

else

JOptionPane.showMessageDialog(null,"your answer is wrong you should try again");

}while(answer3!=12);

do{

fourthQuestion=JOptionPane.showInputDialog(null, "How many continents in the world? ");

answer4 =Integer.parseInt( fourthQuestion );

if(answer4==6)

JOptionPane.showMessageDialog(null,"your answer is correct");

else

JOptionPane.showMessageDialog(null,"your answer is wrong");

}while(answer4!=6);

}

}

[/CODe2]

#3

أخ حسام ان غيرت من طريقة عرض الكود حتى يستطيع الأخوة متابعة الكود أيضا.

و الحقيقة انا لم أفهم المشكلة بالتحديد فياليت تحددها أكثر

خالد المصرى

#4

Alslam wa alykom

As you can see if you want to execute this program that the program will not stop, I want in some stage if I want to stop it, for instance if I enter 0 the program will stop. and I will tell the user like ( how many ocean are they in the world? If you want to quit the program enter 0) because there is no answer equal to 0

It is by using (while) loop

I hope that I explain enough. And thank you for your attention.

#5

just add a break statement inside each loop if the user enters 0

if(answer1==0)
break;

this will cause the programme to exit the while loop.I suggest that you use while(true) because you are chacking all the possible cases inside the loop

Hope this helps

Madani

#6

alslam wa alykom

thank you madan, i did as you said and it is working

this is my first year in Java and i don't know that much

thank you everone for your help and happy Rmadan

(yess)

#7

و الله يا اخ حسام،، اذا هذي هي برامجكم .. فهذا حظكم ..

احنا واجب .. اله حاسبه

#8

:D

خالد المصرى

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

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