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

أين الخطأ في هذا الكود ؟

مغلق
بدأه جودانه في 29 ديسمبر 2006 · 8 رد · 704 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

ان من المبتدئين في الجافا اتمنى احد يساعدني في اكتشاف الخطأ في هذا الكود

I.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{if(m.charAt(p)=='i')
				{

					x+="i";
			  answer_word.setText(x);

			  p++;
	   	}else

	 JOptionPane.showMessageDialog(ScrambledWord .this,String.format("baaaad"));   				


		}});
			   J.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{if(m.charAt(p)=='j')
				{

					x+="j";
			  answer_word.setText(x);
			  p++;
	   	}else

	 JOptionPane.showMessageDialog(ScrambledWord .this,String.format("baaaad"));   				


		}});

تم تعديل هذه المشاركة بواسطة nardien28 في 30 ديسمبر 2006 في 01:18

#2

استبدل ScrambledWord.this إلى this فقط

واذا لم تنجح تأكد من أن ScrambledWord.this يعود بأوبحكت من نوع Frame or JFrame

#3

شكرا يا احمد بس المشكلة الي تطلع لي مش هنا

امشكلة ان البرنامج المفروض منه انه يقرا string من ال user بواسطة button ويقارنه بكلمه من ال file لكن المقارنة تكون حرف بحرف

وارجو الرد

انا اسفة عيسى ما انتبهت للاسم بس امبسطت انه لقيت رد

وارجع اكرر اسفي

#4
undefined

for(p=0;p<m.length();)
	  {

	  A.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{if(m.charAt(p)=='a')
				{	x+="a";
			  answer_word.setText(x);
			  p++;
	   	}else

	 JOptionPane.showMessageDialog(ScrambledWord .this,String.format("baaaad"));   				


		}});

المشكلة انه يعمل compile عادي لكن ما يظهر المطلوب

مع اني جربت اني ما اضع for واقارن كلمة بكلمة وليس حرف بحرف ومشي معايا لكن مش هذا المطلوب فأرجو المساعدة

عند كتابة كود برمجي يرجى تظليله ومن ثم ضغط الزر #

تم تعديل هذه المشاركة بواسطة nardien28 في 31 ديسمبر 2006 في 15:31

#5

لماذا لا تقومين بقراءة الكلمة من الملف ثم تحويلها إلى مصفوفة char والمقارنة ؟

على كل ضعي ملف الـ.java كاملاً إن كان بالإمكان !!

بالتوفيق

فحنوت ألثم جرحه الرعافَ فانتكأت جراحي..

وهمت على خدي الدموع فقلت يا روحي وراحي..

هلّا رحمتَ قلوبنا .. وعدلت عن هذا الرّواحِ ِ ..؟

فأجابني البطل المسجّى هازئاً بيَ .. باقتراحي

كفكف دموعك ليس في عبراتك الحرّى ارتياحي

هذي سبيلي .. إن صدقت محبتي .. فاحمل سلاحي

Sun Certified Java Programmer

#6

شكرا على الافادة بشروط المنتدى لكن انا جد محتاجة لحل البرنامج سريعا

فاعذروني لاني جديدة في المنتدى

انا راح اضع الكود كامل مع العلم انة ناقص القراءة من ال file لاني لسا برضوا ما عرفت اقراء من الfile لكن فرضت كلمة موجودة

وعملت با نصيحة اني اضعها في array of char ونفس المشكلة تحصل معايا

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.Container;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JOptionPane;




public class  ScrambledWord extends JFrame
{
	//JLabel

	private JLabel space;
	private JLabel s;
	//JTextField
	private JTextField scrambled_word;
	private JTextField  answer_word;

	//JButton
	private JButton  help;
	private JButton  stop;
	private JButton start;
  private JButton test;  
	private JButton  nextWord;

	private JButton Q;
	private JButton W;
	private JButton E;
	private JButton R;
	private JButton T;
	private JButton Y;
	private JButton U;
	private JButton I;
	private JButton O;
	private JButton P;

	private JButton A;
	private JButton S;
	private JButton D;
	private JButton F;
	private JButton G;
	private JButton H;
	private JButton J;
	private JButton K;
	private JButton L;

	private JButton Z;
	private JButton X;
	private JButton C;
	private JButton V;
	private JButton B;
	private JButton N;
	private JButton M;

	//JPanel
	private JPanel wordsPanel;
	private JPanel spacePanel;
	private JPanel panel_1;
	private JPanel StopHelpPanel;

	private JPanel L1;
	private JPanel L2;
	private JPanel L3;
	private JPanel lettersPanel;

	private JPanel finalPanel;

	private JPanel t;
	 	String x="",m;
int p;
  char b[]=new char[10];
	//Constructer
	public ScrambledWord ()
	{
		super("Scrambled Word ");



	   scrambled_word=new JTextField("joodi");
		String f=scrambled_word.getText();
	   	m=f;
	   	for(int i=0;i<m.length();i++)
	   	{
	   		b=m.charAt(i);
	   	}
	   answer_word=new JTextField(x,6);



	   nextWord	=new JButton("Next Word");

	   help=new JButton("Help");

	   stop=new JButton("Stop");

	   start=new JButton("Start");
	   test=new JButton("Test");
	   space=new JLabel("");																									

	   spacePanel=new JPanel();
	   spacePanel.add(space);

	   s=new JLabel("											   ");


//-------------------------------------------------------------------

	   wordsPanel =new JPanel();

	   wordsPanel.setLayout(new GridLayout(7,1,13,13));
	   wordsPanel.add(spacePanel);
	   wordsPanel.add(scrambled_word);  
	   wordsPanel.add(answer_word);
	   wordsPanel.add(nextWord);



//--------------------------------------------------------------------

	  panel_1=new JPanel();

	  panel_1.setLayout(new FlowLayout());

	  panel_1.add(wordsPanel,BorderLayout.CENTER);

	  panel_1.add(spacePanel);



//----------------------------------------------------------------------
	 t=new JPanel();
	 t.setLayout(new GridLayout(1,3));

	   lettersPanel=new JPanel();
	   lettersPanel.setLayout(new GridLayout(3,1,10,10));
	   L1=new JPanel();

	   A=new JButton("Aa");
	   B=new JButton("Bb");	
	  C=new JButton("Cc");
	   D=new JButton("Dd");
	   E=new JButton("Ee");		  
	   F=new JButton("Ff");
	   G=new JButton("Gg");
	   H=new JButton("Hh"); 
	   I=new JButton("Ii");
	   J=new JButton("Jj");

	   L1.setLayout(new GridLayout(1,9,2,2));


	   L1.add(A);
	   L1.add(B);
	   L1.add(C);
	   L1.add(D);
	   L1.add(E);								 
	   L1.add(F);
	   L1.add(G);
	   L1.add(H);



	   lettersPanel.add(L1); 

	  L2=new JPanel();
	  K=new JButton("Kk");  
	  L=new JButton("Ll");
	  M=new JButton("Mm");
	  N=new JButton("Nn");
	  O=new JButton("Oo");
	  P=new JButton("Pp");
	  Q=new JButton("Qq");
	  R=new JButton("Rr");
	  S=new JButton("Ss");



	  L2.setLayout(new GridLayout(1,8));


	   L2.add( I);
	   L2.add( J);
	   L2.add(K);
	   L2.add( L);
	   L2.add(M);
	   L2.add(N);
	   L2.add( O);
	   L2.add( P);
	   L2.add(Q);


	  lettersPanel.add(L2); 

	  L3=new JPanel();
	  T=new JButton("Tt");
	  U=new JButton("Uu");
	  V=new JButton("Vv");
	  W=new JButton("Ww");
	  X=new JButton("Xx");
	  Y=new JButton("Yy");
	  Z=new JButton("Zz");	


	  L3.setLayout(new GridLayout(1,9,1,1));


	  L3.add(R);
	  L3.add(S);
	  L3.add(T);
	  L3.add(U);
	  L3.add(V);
	  L3.add(W);
	  L3.add(X); 
	  L3.add(Y);	
	   L3.add(Z);  


		lettersPanel.add(L3); 


//------------------------------------------------------------------------
	StopHelpPanel=new JPanel();
	StopHelpPanel.setLayout(new FlowLayout());

	StopHelpPanel.add(space);
	StopHelpPanel.add(start,BorderLayout.EAST);
	 StopHelpPanel.add(test,BorderLayout.EAST);
	StopHelpPanel.add(help,BorderLayout.EAST);
	StopHelpPanel.add(stop,BorderLayout.EAST);



//-------------------------------------------------------------------------

	  finalPanel=new JPanel(); 
	   finalPanel.setLayout(new GridLayout(4,1,5,5));

	   finalPanel.add(panel_1);

			  finalPanel.add(spacePanel);


	   finalPanel.add(lettersPanel);

	   finalPanel.add(spacePanel);

	   finalPanel.add(StopHelpPanel,BorderLayout.SOUTH);

	  add(finalPanel);
	for(p=0;p<b.length;)
	  {

	  A.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
				if(b[p]=='a')
				{

					x+="a";
			  answer_word.setText(x);
			  p++;

	   	}
	   	else
	   	for(int i=0;i<2;i++)
	   	 JOptionPane.showMessageDialog(ScrambledWord .this,String.format("baaaad"));   				


		}});

		 /*B.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="b";
			  answer_word.setText(x);


		}});
				C.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="c";
			  answer_word.setText(x);


		}});*/
				 D.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="d";
			  answer_word.setText(x);
	   	p++;

		}});
				   /* E.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="e";
			  answer_word.setText(x);


		}});
				 F.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="f";
			  answer_word.setText(x);


		}});  
		G.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="g";
			  answer_word.setText(x);


		}});
			   H.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="h";
			  answer_word.setText(x);


		}});
			   */				
		   I.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{

				if(b[p]=='i')
				{

					x+="i";
			  answer_word.setText(x);
			  p++;}

	   	else
	   	{

	   	for(int i=0;i<2;i++)
	   	 JOptionPane.showMessageDialog(ScrambledWord .this,String.format("baaaad"));   				
		}
		}});
			   J.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{

				if(b[p]=='j')
				{

					x+="j";
			  answer_word.setText(x);
			  p++;

	   	}
	   	else
	   	for(int i=0;i<2;i++)
	   	 JOptionPane.showMessageDialog(ScrambledWord .this,String.format("baaaad"));   				


		}});
		/*
			   K.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="k";
			  answer_word.setText(x);


		}});
	  L.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="l";
			  answer_word.setText(x);


		}});


		M.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="m";
			  answer_word.setText(x);


		}});
		  N.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="n";
			  answer_word.setText(x);


		}});*/


		O.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{


				if(b[p]=='o')
				{

					x+="o";
			  answer_word.setText(x);
	   	  p++;

	   	  }
	   	  else
	   	for(int i=0;i<2;i++)

	   	 JOptionPane.showMessageDialog(ScrambledWord .this,String.format("baaaad"));   				


		}});
		/*
		  P.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="p";
			  answer_word.setText(x);


		}});
		  Q.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="q";
			  answer_word.setText(x);


		}});

		 R.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="r";
			  answer_word.setText(x);


		}});


		S.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="s";
			  answer_word.setText(x);


		}});
		T.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="t";
			  answer_word.setText(x);


		}});
				 U.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="u";
			  answer_word.setText(x);


		}});

			  V.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="v";
			  answer_word.setText(x);


		}});

		  W.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="w";
			  answer_word.setText(x);


		}});

		   X.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="x";
			  answer_word.setText(x);


		}});
		   Y.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="y";
			  answer_word.setText(x);


		}});
			 Z.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					x+="z";
			  answer_word.setText(x);


		}});*/}

   start.addActionListener(
			new ActionListener()
			{ 
				public void actionPerformed(ActionEvent event)
				{
					String m,y="",z="",n="";


				m=scrambled_word.getText();


				for(int i=0;i<m.length();i++)
				{
				if(i==0||i==3||i==5||i==7)
					y+=m.charAt(i);
					else
					z+=m.charAt(i);


				}
			  z+=y;
			 x="";
	   answer_word.setText(x);		  
		scrambled_word.setText(z);
		}});   
		stop.addActionListener(

			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{

		}		}
);	   

					   }}

علما ان البرنامج مكتوب ب ال Jcreater وارجو الرد

#7

شكرا لكل من حاول يساعدني في إيجاد حل لهذا البرنامج

لان المشكلة الاولى وجدة لها ان شاء الله الحل المناسب

لكن المشكلة الثانية اني لا اعلم كيفية القراءة من الfile او انشاء file

وهذا الحل الي توصلت له

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.FlowLayout;

import java.awt.Container;

import java.awt.Color;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import javax.swing.JFrame;

import javax.swing.JTextField;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JButton;

import javax.swing.JOptionPane;

public class ScrambledWord extends JFrame

{

//JLabel

private JLabel space;

private JLabel s;

//JTextField

private JTextField scrambled_word;

private JTextField answer_word;

//JButton

private JButton help;

private JButton stop;

private JButton start;

private JButton test;

private JButton nextWord;

private JButton Q;

private JButton W;

private JButton E;

private JButton R;

private JButton T;

private JButton Y;

private JButton U;

private JButton I;

private JButton O;

private JButton P;

private JButton A;

private JButton S;

private JButton D;

private JButton F;

private JButton G;

private JButton H;

private JButton J;

private JButton K;

private JButton L;

private JButton Z;

private JButton X;

private JButton C;

private JButton V;

private JButton B;

private JButton N;

private JButton M;

//JPanel

private JPanel wordsPanel;

private JPanel spacePanel;

private JPanel panel_1;

private JPanel StopHelpPanel;

private JPanel L1;

private JPanel L2;

private JPanel L3;

private JPanel lettersPanel;

private JPanel finalPanel;

private JPanel t;

String x="",m,v,j;

int p,i=0,u;

char b[]=new char[10];

//Constructer

public ScrambledWord ()

{

super("Scrambled Word ");

scrambled_word=new JTextField("joodi");

String f=scrambled_word.getText();

m=f;

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

{

b=m.charAt(i);

}

answer_word=new JTextField(x,6);

nextWord =new JButton("Next Word");

help=new JButton("Help");

stop=new JButton("Stop");

start=new JButton("Start");

test=new JButton("Test");

space=new JLabel("");

spacePanel=new JPanel();

spacePanel.add(space);

s=new JLabel(" ");

//-------------------------------------------------------------------

wordsPanel =new JPanel();

wordsPanel.setLayout(new GridLayout(7,1,13,13));

wordsPanel.add(spacePanel);

wordsPanel.add(scrambled_word);

wordsPanel.add(answer_word);

wordsPanel.add(nextWord);

//--------------------------------------------------------------------

panel_1=new JPanel();

panel_1.setLayout(new FlowLayout());

panel_1.add(wordsPanel,BorderLayout.CENTER);

panel_1.add(spacePanel);

//----------------------------------------------------------------------

t=new JPanel();

t.setLayout(new GridLayout(1,3));

lettersPanel=new JPanel();

lettersPanel.setLayout(new GridLayout(3,1,10,10));

L1=new JPanel();

A=new JButton("Aa");

B=new JButton("Bb");

C=new JButton("Cc");

D=new JButton("Dd");

E=new JButton("Ee");

F=new JButton("Ff");

G=new JButton("Gg");

H=new JButton("Hh");

I=new JButton("Ii");

J=new JButton("Jj");

L1.setLayout(new GridLayout(1,9,2,2));

L1.add(A);

L1.add(B);

L1.add(C);

L1.add(D);

L1.add(E);

L1.add(F);

L1.add(G);

L1.add(H);

lettersPanel.add(L1);

L2=new JPanel();

K=new JButton("Kk");

L=new JButton("Ll");

M=new JButton("Mm");

N=new JButton("Nn");

O=new JButton("Oo");

P=new JButton("Pp");

Q=new JButton("Qq");

R=new JButton("Rr");

S=new JButton("Ss");

L2.setLayout(new GridLayout(1,8));

L2.add( I);

L2.add( J);

L2.add(K);

L2.add( L);

L2.add(M);

L2.add(N);

L2.add( O);

L2.add( P);

L2.add(Q);

lettersPanel.add(L2);

L3=new JPanel();

T=new JButton("Tt");

U=new JButton("Uu");

V=new JButton("Vv");

W=new JButton("Ww");

X=new JButton("Xx");

Y=new JButton("Yy");

Z=new JButton("Zz");

L3.setLayout(new GridLayout(1,9,1,1));

L3.add(R);

L3.add(S);

L3.add(T);

L3.add(U);

L3.add(V);

L3.add(W);

L3.add(X);

L3.add(Y);

L3.add(Z);

lettersPanel.add(L3);

//------------------------------------------------------------------------

StopHelpPanel=new JPanel();

StopHelpPanel.setLayout(new FlowLayout());

StopHelpPanel.add(space);

StopHelpPanel.add(start,BorderLayout.EAST);

StopHelpPanel.add(help,BorderLayout.EAST);

StopHelpPanel.add(stop,BorderLayout.EAST);

//-------------------------------------------------------------------------

finalPanel=new JPanel();

finalPanel.setLayout(new GridLayout(4,1,5,5));

finalPanel.add(panel_1);

finalPanel.add(spacePanel);

finalPanel.add(lettersPanel);

finalPanel.add(spacePanel);

finalPanel.add(StopHelpPanel,BorderLayout.SOUTH);

add(finalPanel);

A.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent event)

{

if(b[p]=='a')

{

x+="a";

answer_word.setText(x);

v=answer_word.getText();

String j=m;

u= j .compareTo(v);

p++;

if(u==0)

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("gooog"));

}

else

{

if(i<2)

{

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("baaaad"));

i++;

}

else

answer_word.setText(m);

}

}});

B.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent event)

{

if(b[p]=='b')

{

x+="b";

answer_word.setText(x);

v=answer_word.getText();

String j=m;

u= j .compareTo(v);

p++;

if(u==0)

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("gooog"));

}

else

{

if(i<2)

{

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("baaaad"));

i++;

}

else

answer_word.setText(m);

}

}});

C.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent event)

{

if(b[p]=='c')

{

x+="c";

answer_word.setText(x);

v=answer_word.getText();

String j=m;

u= j .compareTo(v);

p++;

if(u==0)

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("gooog"));

}

else

{

if(i<2)

{

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("baaaad"));

i++;

}

else

answer_word.setText(m);

}

}});

D.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent event)

{

if(b[p]=='d')

{

x+="d";

answer_word.setText(x);

v=answer_word.getText();

String j=m;

u= j .compareTo(v);

p++;

if(u==0)

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("gooog"));

}

else

{

if(i<2)

{

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("baaaad"));

i++;

}

else

answer_word.setText(m);

}

}});

E.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent event)

{

if(b[p]=='e')

{

x+="e";

answer_word.setText(x);

v=answer_word.getText();

String j=m;

u= j .compareTo(v);

p++;

if(u==0)

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("gooog"));

}

else

{

if(i<2)

{

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("baaaad"));

i++;

}

else

answer_word.setText(m);

}

}});

F.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent event)

{

if(b[p]=='f')

{

x+="f";

answer_word.setText(x);

v=answer_word.getText();

String j=m;

u= j .compareTo(v);

p++;

if(u==0)

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("gooog"));

}

else

{

if(i<2)

{

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("baaaad"));

i++;

}

else

answer_word.setText(m);

}

}});

G.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent event)

{

if(b[p]=='g')

{

x+="g";

answer_word.setText(x);

v=answer_word.getText();

String j=m;

u= j .compareTo(v);

p++;

if(u==0)

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("gooog"));

}

else

{

if(i<2)

{

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("baaaad"));

i++;

}

else

answer_word.setText(m);

}

}});

H.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent event)

{

if(b[p]=='h')

{

x+="h";

answer_word.setText(x);

v=answer_word.getText();

String j=m;

u= j .compareTo(v);

p++;

if(u==0)

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("gooog"));

}

else

{

if(i<2)

{

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("baaaad"));

i++;

}

else

answer_word.setText(m);

}

}});

I.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent event)

{

if(b[p]=='i')

{

x+="i";

answer_word.setText(x);

v=answer_word.getText();

String j=m;

u= j .compareTo(v);

p++;

if(u==0)

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("gooog"));

}

else

{

if(i<2)

{

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("baaaad"));

i++;

}

else

answer_word.setText(m);

}

}});

J.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent event)

{

if(b[p]=='j')

{

x+="j";

answer_word.setText(x);

v=answer_word.getText();

String j=m;

u= j .compareTo(v);

p++;

if(u==0)

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("gooog"));

}

else

{

if(i<2)

{

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("baaaad"));

i++;

}

else

answer_word.setText(m);

}

}});

K.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent event)

{ if(b[p]=='k')

{

x+="k";

answer_word.setText(x);

v=answer_word.getText();

String j=m;

u= j .compareTo(v);

p++;

if(u==0)

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("gooog"));

}

else

{

if(i<2)

{

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("baaaad"));

i++;

}

else

answer_word.setText(m);

}

}});

L.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent event)

{

if(b[p]=='l')

{

x+="l";

answer_word.setText(x);

v=answer_word.getText();

String j=m;

u= j .compareTo(v);

p++;

if(u==0)

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("gooog"));

}

else

{

if(i<2)

{

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("baaaad"));

i++;

}

else

answer_word.setText(m);

}

}});

M.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent event)

{

if(b[p]=='m')

{

x+="m";

answer_word.setText(x);

v=answer_word.getText();

String j=m;

u= j .compareTo(v);

p++;

if(u==0)

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("gooog"));

}

else

{

if(i<2)

{

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("baaaad"));

i++;

}

else

answer_word.setText(m);

}

}});

N.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent event)

{

if(b[p]=='n')

{

x+="n";

answer_word.setText(x);

v=answer_word.getText();

String j=m;

u= j .compareTo(v);

p++;

if(u==0)

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("gooog"));

}

else

{

if(i<2)

{

JOptionPane.showMessageDialog(ScrambledWord .this,String.format("baaaad"));

i++;

}

else

answer_word.setText(m);

}

}});

#8

اولا هناك مشكلة في حلقة for.

ثانيا ليتك وضحت اكثر عن وظيفة البرنامج.

ثالثا الكود التالي يقلص الشفره الى حد ما

public class  ScrambledWord2 extends JFrame{
	public static void main(String[]ae){		
		new ScrambledWord2().setVisible(true);
	}

	private JTextField field, userField;
	public ScrambledWord2(){
		super("");
		setLayout(new BorderLayout());

		//		panel 1
		JPanel fieldsPanel=new JPanel();
		fieldsPanel.setLayout(new GridLayout(3,1));
		field=new JTextField("joodi",12);
		userField=new JTextField(12);
		JButton nextWord=new JButton("Next Word");
		nextWord.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent event){
					equal(field.getText().trim(), userField.getText().trim());
				}
			}	
		);
		fieldsPanel.add(field);
		fieldsPanel.add(userField);
		fieldsPanel.add(nextWord);


		// panel 2
		JPanel charsPanel=new JPanel();
		charsPanel.setLayout(new GridLayout(3,9,2,2));
		for(int i=65;i<=90;i++){
			JButton button=new JButton();
			button.setActionCommand(""+(char)i);
			button.setText(""+((char)i) + ((char)(i+32)));
			button.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent event){
						userField.setText(userField.getText()+event.getActionCommand());
					}
				}
			);
			charsPanel.add(button);
		}

		// panel 3
		JPanel buttonsPanel=new JPanel();
		/*
		 *	
		 */ 

		add(fieldsPanel, BorderLayout.NORTH);
		add(charsPanel, BorderLayout.CENTER);
		add(buttonsPanel, BorderLayout.SOUTH);
		pack();
		setVisible(true);
	}

	private void equal(String field, String userField){
		field=field.toUpperCase();
		if(field.length()==userField.length()){
			for(int i=0; i<field.length();i++){
				if(field.charAt(i)!=userField.charAt(i)){
					JOptionPane.showMessageDialog(null, "baaaad");
					return;
				}
			}
		}else{
			JOptionPane.showMessageDialog(null, "baaaad");
			return;
		}
		JOptionPane.showMessageDialog(null, "Equals");
	}

}
#9

شكرا لك اخي على المساعدة

لكن مافهمت منك انت تقصد اي for او ي برنامج الاول او الثاني لان الثاني

هو الصحيح والاول انا كنت عارفه انه الخطاء في الfor وكنت طالبة الحل

ثانيا: البرنامج هو تصميم لعبة للاطفال تكون كالتالي

يقراء كلمة من ال file ويخرجها للطفل مش مرتبه

ويحاول الطفل ترتيبها

وله في ذالك محاولتين فقط اذا استنفذ المحاولتين تظهر له الكلمه الصحيحة

وهذا هو الي توصلت له في البرنامج الثاني

ولكن ينقصه القراءه من الfile وانشاء الfile

وارجو الرد

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

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

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

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

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

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