for (int i = 0; i < 3; i++) {
ary[i] = rd.nextInt(10);
if (i % 2 == 0) {
txt1.setText(Integer.toString(ary[i]));
}
else if (i % 2 != 0) {
txt2.setText(Integer.toString(ary[i]));
}
我产生了两个随机数,并把他们显示在了txt1和txt2文本框中, 不过我还有一个名为txt3的文本框,要求用它来接受用户输入的数然后跟我的第二个txt2里产生的随机数做比较哪个大, 这个要如何实现? 哪位大哥给点提示啊
----------------解决方案--------------------------------------------------------
getText()得到一个String类型变量...然后再转换一下Interget..再比较大小..不就行?.
----------------解决方案--------------------------------------------------------
我是这样转换对吗?
String ss=txt2.getText();
int i=Integer.parseInt(ss);
String s3=txt3.getText();
int i3=Integer.parseInt(s3);
不过用if判断数字是否相同又不会了,报错
----------------解决方案--------------------------------------------------------
错误信息是什么
----------------解决方案--------------------------------------------------------
String ss=txt2.getText();
int i=Integer.parseInt(ss);
String s3=txt3.getText();
int i3=Integer.parseInt(s3);
我这样写了.
是不是IF里面就可以判断i是不是与i3相等了?
如
if(i==i3)
{
txt5.setText("Y");
}
else
{
txt5.setText("N");
}
为什么都显示"N" IF里面不能这样写?
----------------解决方案--------------------------------------------------------
定的是对的啊,
不过,还有,你输入的是不是数字啊
如果不是数字,会有异常的
----------------解决方案--------------------------------------------------------
还是我的那个用于输入的文本框那里有错
有空给我看看呀.全部代码如下:
package framdemo;
import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
/**
*
* <p>Title: </p>
* 要求:创建四个JTextField对象,产生三个随机数保存到数组,再从数组取出随机数.显示到三个文本框中,
* 用第四个接收接收用户输入的数字,和第三个文本框中的随机数做比较.
*
*
*/
public class Frame5_1_6
extends JFrame {
public Frame5_1_6() {
try {
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
this.disp();
getContentPane().setLayout(null);
jLabel1.setText("随机数1");
jLabel1.setBounds(new Rectangle(18, 55, 48, 14));
txt3.setBounds(new Rectangle(315, 53, 64, 19));
jLabel3.setText("随机数3");
jLabel3.setBounds(new Rectangle(267, 55, 43, 14));
txt2.setBounds(new Rectangle(188, 53, 64, 19));
jLabel2.setText("随机数2");
jLabel2.setBounds(new Rectangle(144, 55, 49, 14));
txtInfo.setBounds(new Rectangle(138, 166, 150, 33));
txt5.setBounds(new Rectangle(146, 115, 123, 22));
jLabel4.setText("请输入对比数");
jLabel4.setBounds(new Rectangle(76, 120, 66, 14));
jLabel5.setText("结果:");
jLabel5.setBounds(new Rectangle(106, 174, 34, 14));
jButton1.setBounds(new Rectangle(133, 231, 73, 23));
jButton1.setText("确认");
jButton1.addActionListener(new Frame5_1_6_jButton1_actionAdapter(this));
jButton2.setBounds(new Rectangle(238, 232, 73, 23));
jButton2.setText("关闭");
jButton2.addActionListener(new Frame5_1_6_jButton2_actionAdapter(this));
txt6.setBounds(new Rectangle(19, 155, 64, 19));
this.getContentPane().add(jLabel1);
this.getContentPane().add(txt1);
this.getContentPane().add(jLabel2);
this.getContentPane().add(jLabel3);
this.getContentPane().add(txt3);
this.getContentPane().add(txt2);
this.getContentPane().add(txt5);
this.getContentPane().add(jLabel4);
this.getContentPane().add(jLabel5);
this.getContentPane().add(txtInfo);
this.getContentPane().add(jButton1);
this.getContentPane().add(jButton2);
this.getContentPane().add(txt6);
// StringBuffer sb = new StringBuffer();
txt1.setBounds(new Rectangle(68, 53, 64, 19));
}
JLabel jLabel1 = new JLabel();
JTextField txt1 = new JTextField();
JLabel jLabel2 = new JLabel();
JTextField txt2 = new JTextField();
JLabel jLabel3 = new JLabel();
JTextField txt3 = new JTextField();
JTextField txtInfo = new JTextField();
JTextField txt5 = new JTextField();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
StringBuffer sb = new StringBuffer();
JTextField txt6 = new JTextField();
public void jButton2_actionPerformed(ActionEvent e) {
dispose();
}
/**
* 定义方法,参数随机数;
*/
public void disp() {
java.util.Random rd = new Random();
sb.delete(0, sb.length());
// int s, k;
int ary[] = new int[10];
// s = rd.nextInt(10);
// k = rd.nextInt(10);
for (int i = 0; i < 3; i++) {
ary[i] = rd.nextInt(10);
if (i % 2 == 0) {
txt1.setText(Integer.toString(ary[i]));
}
else if (i % 2 != 0) {
txt2.setText(Integer.toString(ary[i]));
}
txt3.setText(Integer.toString(ary[i] - 1));
}
/* String ss = txt2.getText();
int i = Integer.parseInt(ss);
String s3 = txt3.getText();
int i3 = Integer.parseInt(s3);
if (i == i3) {
txtInfo.setText("是的");
}
else {
txtInfo.setText("不是");
}
// txt2.setText(Integer.toString(s));
*/
}
/**
* 点击事件
*/
public void jButton1_actionPerformed(ActionEvent e) {
//this.disp();
// String ss = txt2.getText();
// int i = Integer.parseInt(ss);
String s3 = txt3.getText();
int i3 = Integer.parseInt(s3);
String sss=txt5.getText();
int i5=Integer.parseInt(sss);
/**
* 文本框txt5是输入框;
*/
txt6.setText(txt5.getText() + txt3.getText());
// String.copyValueOf();
//txt6.setText(String.copyValueOf(i));
if ( i5== i3) {
txtInfo.setText("是的");
}
else {
txtInfo.setText("不是");
}
// String s=this.toString();
//txt1.setText(String.copyValueOf());
//txt1.setText());
}
}
class Frame5_1_6_jButton1_actionAdapter
implements ActionListener {
private Frame5_1_6 adaptee;
Frame5_1_6_jButton1_actionAdapter(Frame5_1_6 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class Frame5_1_6_jButton2_actionAdapter
implements ActionListener {
private Frame5_1_6 adaptee;
Frame5_1_6_jButton2_actionAdapter(Frame5_1_6 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
----------------解决方案--------------------------------------------------------