新手Java问题 [英] Novice Java Problem

查看:72
本文介绍了新手Java问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我有一个用Java的AWT编写的小程序.
但是它不能正常工作.
请.有人可以帮我吗?
没有找到第一个.最重要的是,其他都很好.


Hello,
I have this small programm written in AWT in Java.
But it''s not working properly.
Please. can anyone help me out?
It''s not finding out the 1st no. as greatest, evrything else is fine.


//WAP TO FIND GREATEST & SMALLEST OF NOS.
//© DHARMiL
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code=Greatest height = 400 width = 500></applet>*/
public class Greatest extends Applet implements ActionListener
{
	TextField t1,t2,t3,t4;
	Button b1,b2;
	Label l1,l2,l3,l4;
	public void init ()
	{
		t1 = new TextField (5);
		t2 = new TextField (5);
		t3 = new TextField (5);
		t4 = new TextField (25);
		b1 = new Button ("SMALLEST");
		b2 = new Button ("GREATEST");
		l1 = new Label ("Enter no 1");
		l2 = new Label ("Enter no 2");
		l3 = new Label ("Enter no 3");
		l4 = new Label ("RESULT is :-");
		add(l1);
		add(t1);
		add(l2);
		add(t2);
		add(l3);
		add(t3);
		add(b1);
		add(b2);
		add(l4);
		add(t4);
		b2.addActionListener (this);
	}
	public void actionPerformed (ActionEvent e)	
	{
		if (e.getSource() == b2);
		{
			int a = Integer.parseInt (t1.getText());
			int b = Integer.parseInt (t2.getText());
			int c = Integer.parseInt (t3.getText());
			{
				if((a>b)&&(a>c))
				{
					t4.setText("no.1 is GREATER");
				}
				if ((b>c));
				{
				t4.setText ("n0.2 is GREATER");
				}
				else
				{	
					t4.setText ("n0.3 is GREATER");
				}
			}
		}
	}
}


[edit]删除了虚假的大"字,更正了大写字母,删除了txtspeak.
请不要使用txtspeak:这是一个国际站点,非英语国家的母语者很难确定任何词典中没有单词的含义. -OriginalGriff [/edit]


[edit]Spurious "big" removed, capitalization corrected, txtspeak removed.
Please don''t use txtspeak: this is an international site and it can be hard for non-native English speakers to work out what words mean when they aren''t in any dictionary. - OriginalGriff[/edit]

推荐答案

所有错误的是,您需要另一个"else"子句.目前:
All that is wrong is that you need another "else" clause. At the moment:
if((a>b)&(a>c))

如果通过,则将t4设置为
. 但是然后您再次设置t4要么显示2更大,要么显示3更大.
添加其他:

If it passes, then you set t4 to indicate.
But then you set t4 again either to show 2 is greater, or 3 is greater.
Add another else:

if((a>b)&(a>c))
   {
   t4.setText("no.1 is GREATER");
   }
else
   {
   if ((b>c));
      {
      t4.setText ("n0.2 is GREATER");
      }
   else
      {
      t4.setText ("n0.3 is GREATER");
      }
   }

而且一切都很好.

顺便说一句:更改您的名字是值得的:t1,t2,t3,t4并不能很好地描述它们的功能.考虑使用"input1","results"等等-当项目变得更大时,它将使正在发生的事情变得容易得多!

And all should be fine.

BTW: It is worth changing your names: t1, t2, t3, t4 are not very descriptive of what they do. Consider using "input1", "results" and so on - it will make working out what is happening a lot easier when projects get a bit bigger!


还有一个问题.
我也试图使工作成为最小的按钮,但是当我单击任何一个按钮时,SMALLEST或GREATEST iT将返回SMALLEST的结果.
PlS.帮助
hey one more question.
i M trying to make work the smallest button also, but when i click on any button SMALLEST or GREATEST iT Returns on the result of the SMALLEST.
PlS. help
//WAP TO FIND GREATEST & SMALLEST OF NOS.
//© DHARMiL
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code=Greatest1 height = 400 width = 500></applet>*/
public class Greatest1 extends Applet implements ActionListener
{
	TextField t1,t2,t3,t4;
	Button b1,b2;
	Label l1,l2,l3,l4;
	public void init ()
	{
		t1 = new TextField (5);
		t2 = new TextField (5);
		t3 = new TextField (5);
		t4 = new TextField (25);
		b1 = new Button ("SMALLEST");
		b2 = new Button ("GREATEST");
		l1 = new Label ("Enter no 1");
		l2 = new Label ("Enter no 2");
		l3 = new Label ("Enter no 3");
		l4 = new Label ("RESULT is :-");
		add(l1);
		add(t1);
		add(l2);
		add(t2);
		add(l3);
		add(t3);
		add(b1);
		add(b2);
		add(l4);
		add(t4);
		b1.addActionListener (this);
		b2.addActionListener (this);
	}
	public void actionPerformed (ActionEvent e)	
	{
		//if (e.getSource() == b1);
		if (e.getSource() == b2);
		{
			int a = Integer.parseInt (t1.getText());
			int b = Integer.parseInt (t2.getText());
			int c = Integer.parseInt (t3.getText());
			{
				if((a>b)&&(a>c))
				{
					t4.setText("no.1 is GREATER");
				}
				else
				{
					if ((b>c))
					{
						t4.setText ("n0.2 is GREATER");
					}
					else
					{
						t4.setText ("n0.3 is GREATER");
					}
				}
			}
		}
		if (e.getSource() == b1);
			{
				int x = Integer.parseInt (t1.getText());
				int y = Integer.parseInt (t2.getText());
				int z = Integer.parseInt (t3.getText());
				if ((x<y)&&(x<z))
				{
					t4.setText ("no.1 is SMALLER");
				}
				else
				{
					if ((y<z))
					{
						t4.setText ("no.2 is SMALLER");
					}
					else
					{
						t4.setText ("no. 3 is SMALLER");
					}
				}
			}
		}
	}


您的if语句不起作用:
Your if statements do nothing:
if (e.getSource() == b2);


请注意,最后一个分号是null语句,因此代码将一直持续到最后,最后的有效测试将是对最小值的检查.


Notice the semi-colon at the end which is the null statement, so the code will merely continue through until the end, and the last valid test will be a check for the smallest value.


这篇关于新手Java问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆