学JAVA的过程中我遇到了N多问题,有的时候搞不懂就想来这里问问大家,
可是每个问题发一贴简直太不人道了,因为我的问题多,那样会发很多帖子,会把大家的帖子全都灌下去。
为了不影响大家我只好把我所有的问题都放到这一个帖子里提问,当然如果大家有什么问题也可以在这里问,我欢迎。
好了先问个嗷嗷简单的问题吧
为什么“The world”是不合法的变量名称?是因为中间多了个空格?
[此贴子已经被作者于2006-2-15 21:01:09编辑过]
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class C231 extends JFrame implements MouseListener
{
public static void main(String[] args)
{
C231 frame1 = new C231();
int px,py,px2,py2;
JLabel lb1;
JButton bu1;
public C231()
{
super("C231");
Container c = getContentPane();
c.setLayout(null);
lb1 = new JLabel("鼠标状态");
lb1.setFont(new Font("仿宋",Font.BOLD,20));
lb1.setForeground(Color.red);
lb1.setSize(200,28);
lb1.setLocation(10,10);
c.add(lb1);
bu1 = new JButton("鼠标按键区");
bu1.setSize(100,100);
bu1.setLocation(100,100);
bu1.addMouseListener(this);
c.add(bu1);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,400);
setVisible(true);
}
public void mousePressed(MouseEvent e)
{
lb1.setText("按下鼠标键");
}
public void mouseReleased(MouseEvent e)
{
lb1.setText("释放鼠标键");
}
public void mouseEntered(MouseEvent e)
{
lb1.setText("进入按键区");
}
public void mouseExited(MouseEvent e)
{
lb1.setText("鼠标离开按键区");
}
public void mouseClicked(MouseEvent e)
{
lb1.setText("按了一下鼠标键");
}
}
}
请大家帮我找找程序的错误
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class C231 extends JFrame implements MouseListener
{
public static void main(String[] args)
{
C231 frame1 = new C231();
}
int px,py,px2,py2;
JLabel lb1;
JButton bu1;
public C231()
{
super("C231");
Container c = getContentPane();
c.setLayout(null);
lb1 = new JLabel("鼠标状态");
lb1.setFont(new Font("仿宋",Font.BOLD,20));
lb1.setForeground(Color.red);
lb1.setSize(200,28);
lb1.setLocation(10,10);
c.add(lb1);
bu1 = new JButton("鼠标按键区");
bu1.setSize(100,100);
bu1.setLocation(100,100);
bu1.addMouseListener(this);
c.add(bu1);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,400);
setVisible(true);
}
public void mousePressed(MouseEvent e)
{
lb1.setText("按下鼠标键");
}
public void mouseReleased(MouseEvent e)
{
lb1.setText("释放鼠标键");
}
public void mouseEntered(MouseEvent e)
{
lb1.setText("进入按键区");
}
public void mouseExited(MouseEvent e)
{
lb1.setText("鼠标离开按键区");
}
public void mouseClicked(MouseEvent e)
{
lb1.setText("按了一下鼠标键");
}
}
问题又来了哦
输入3个非负数,如果是2奇1偶则输出YES,否则输出NO.(用条件语句,不能用IF语句).
上面是我在一个论坛看到的求助帖子,我分别用C语言和JAVA做了解答,用位运算做了奇偶数校验,但是还是用了IF(虽然C用的是(?:)但和IF也一样),大家看看不用IF如何做?
下面是我的代码,编译通过,结果正确。
用C写的:
main()
{
int a,b,c,d;
printf("Please intput 3 int num ,like this 1,2,3\n");
scanf("%d,%d,%d",&a,&b,&c);
((a|b)%2==0?(d=0):((a&b)%2==0?(c%2==0?(d=0):(d=1)):(c%2==0?(d=1):(d=0))));
switch(d)
{
case 0:printf("NO");break;
case 1:printf("YES");break;
}
getch();
}
用JAVA的GUI程序。
import java.awt.*;
import java.awt.event.*;
class Jioushu extends Frame implements ActionListener
{
TextField t1 = new TextField(5);
TextField t2 = new TextField(5);
TextField t3 = new TextField(5);
TextField t4 = new TextField(5);
Label L1 = new Label("校验结果为");
Button btn = new Button("校验");
public Jioushu()
{
setLayout(new FlowLayout());
add(t1);
add(t2);
add(t3);
add(L1);
add(t4);
add(btn);
btn.addActionListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
int a,b,c,d;
try
{
a = Integer.parseInt(t1.getText());
b = Integer.parseInt(t2.getText());
c = Integer.parseInt(t3.getText());
if((a|b)%2==0)
{
d=0;
}
else
{
if((a&b)%2==0)
{
if(c%2==0)
{
d=0;
}
else
{
d=1;
}
}
else
{
if(c%2==0)
{
d=1;
}
else
{
d=0;
}
}
}
switch(d)
{
case 0:t4.setText("NO");break;
case 1:t4.setText("YES");;break;
}
}
catch(Exception ee)
{
t4.setText("数据输入错误!数字超出范围或者类型不正确!");
}
}
public static void main (String args[])
{
Jioushu mainFrame = new Jioushu();
mainFrame.setSize(500,150);
mainFrame.setTitle("奇偶数校验器");
mainFrame.setVisible(true);
}
}