java程序设计代码:显示一个三位整数的各位数字 输入一个整数,分别显示其百位、十位和个位数字。

如题所述

你好,按照你的要求代码如下,可以直接运行,并给出了运行结果:

import java.util.Scanner;

public class test {

public static void main(String[] args) {
// 获得用户输入
System.out.println("请输入一个数:");
Scanner s = new Scanner(System.in);
int i = s.nextInt();
s.close();

// 判断是否是三位数
if (i < 100 || i > 999) {
System.out.println("不是三位数");
return;
}

// 输入结果
String str = String.valueOf(i);
System.out.println("百位:" + str.charAt(0));
System.out.println("十位:" + str.charAt(1));
System.out.println("个位:" + str.charAt(2));
}
}

运行结果:
请输入一个数:
134
百位:1
十位:3
个位:4追问

图形用户界面如图所示。要求:整数文本框可编辑,其他文本框仅用于显示不可编辑;整数文本框响应文本编辑事件;当输入数据错误时,处理异常,弹出对话框,提示重新输入信息。

追答

你好,留下QQ吧,代码这里发不了,超过长度了

温馨提示:内容为网友见解,仅供参考
第1个回答  2011-12-10
这个是别人写的,真是借花献佛了
----------------------------------------------------------
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class MyFrame2 extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton jb1, jb2;
private JTextField jtf1, jtf2, jtf3, jtf4;
private JLabel jl1, jl2, jl3, jl4;

private Integer num;

private int num1;
private int num2;
private int num3;

public MyFrame2() {
this.getContentPane().setLayout(null);
this.setSize(400, 400);
jb1 = new JButton("计算");
jb2 = new JButton("结束");

jtf1 = new JTextField();
jtf2 = new JTextField();
jtf3 = new JTextField();
jtf4 = new JTextField();

jl1 = new JLabel("请输入整数:");
jl2 = new JLabel("百位:");
jl3 = new JLabel("十位:");
jl4 = new JLabel("个位:");

jb1.setBounds(new Rectangle(50, 230, 100, 30));
jb2.setBounds(new Rectangle(220, 230, 100, 30));
jb1.addActionListener(this);
jb2.addActionListener(this);

jl1.setBounds(new Rectangle(10, 50, 80, 25));
jtf1.setBounds(new Rectangle(90, 50, 170, 25));

jl2.setBounds(new Rectangle(10, 140, 40, 25));
jtf2.setBounds(new Rectangle(50, 140, 70, 25));
jl3.setBounds(new Rectangle(130, 140, 40, 25));
jtf3.setBounds(new Rectangle(170, 140, 70, 25));
jl4.setBounds(new Rectangle(250, 140, 40, 25));
jtf4.setBounds(new Rectangle(290, 140, 70, 25));

jtf2.setEditable(false);
jtf3.setEditable(false);
jtf4.setEditable(false);

jtf1.addFocusListener(new FocusListener() {
public void focusLost(FocusEvent e) {
String s = jtf1.getText();
try {
num = Integer.parseInt(s);
} catch (NumberFormatException e1) {
num = null;
JOptionPane.showMessageDialog(null, "请正确输入整型数字!");
System.out.println("...");
jtf1.requestFocus();
}
}

public void focusGained(FocusEvent e) {

}
});

this.getContentPane().add(jb1);
this.getContentPane().add(jb2);

this.getContentPane().add(jl1);
this.getContentPane().add(jl2);
this.getContentPane().add(jl3);
this.getContentPane().add(jl4);

this.getContentPane().add(jtf1);
this.getContentPane().add(jtf2);
this.getContentPane().add(jtf3);
this.getContentPane().add(jtf4);

this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == jb2) {
System.exit(0);
} else if (e.getSource() == jb1) {
solveNum(num);
jtf2.setText(num1 + "");
jtf3.setText(num2 + "");
jtf4.setText(num3 + "");
}
}

private void solveNum(Integer n) {
if (n == null)
return;
num1 = n / 100;
num2 = n / 10 % 10;
num3 = n % 10;
}

public static void main(String[] args) {
new MyFrame2();
}
}本回答被提问者采纳
相似回答
大家正在搜