获取模拟电压值(第二篇)

当序列的长度小于13或者协议字段不为85时,要提出警告“不是酒精传感器的数据,或者长度要等于13!”。如下面的两个图所示:

 

图(1)“字段长度小于13”给出警告

 

图(2)“倒数第3字段要为85”,否则给出“不是酒精传感器的数据!”的警告。

 

图(3)“酒精传感器”数据,电压模拟值是“倒数第2字段”

//文件:ArrayMinValue3.java

package cn.ch.da;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ArrayMinValue3 extends JFrame {

	private static final long serialVersionUID = 5223259684601017509L;
	private JPanel contentPane;
	private JTextField textField;
	private JLabel label;
	private JLabel label_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Throwable e) {
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ArrayMinValue3 frame = new ArrayMinValue3();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ArrayMinValue3() {
		// 标题:获取一维数组模拟电压值
		setTitle("\u83B7\u53D6\u4E00\u7EF4\u6570\u7EC4\u6a21\u62df\u7535\u538b\u503C");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 149);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		textField = new JTextField();
		textField.setBounds(6, 36, 414, 30);
		contentPane.add(textField);
		textField.setColumns(10);
		// 按钮显示的文字为“计算”
		JButton button = new JButton("\u8BA1\u7B97");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				do_button_actionPerformed3(e);
			}
		});
		button.setBounds(16, 76, 90, 30);
		contentPane.add(button);

		label = new JLabel("\u6a21\u62df\u7535\u538b\u503C\uFF1A");
		label.setBounds(116, 82, 304, 18);
		contentPane.add(label);

		// 请在文本框中输入多个整数,以空格为分隔符。例如:3 5 562 125
		label_1 = new JLabel(
				"\u8BF7\u5728\u6587\u672C\u6846\u4E2D\u8F93\u5165\u591A\u4E2A\u6574\u6570\uFF0C\u4EE5\u7A7A\u683C\u4E3A\u5206\u9694\u7B26\u3002\u4F8B\u5982\uFF1A3 5 2 562 125");
		label_1.setBounds(6, 6, 422, 18);
		contentPane.add(label_1);
	}

	protected void do_button_actionPerformed3(ActionEvent e) {
		String arrayStr = textField.getText().trim();
		for (int i = 0; i < arrayStr.length(); i++) {// 过滤非法输入
			char charAt = arrayStr.charAt(i);
			if (!Character.isLetterOrDigit(charAt) && charAt != ' ') {
				JOptionPane.showMessageDialog(null, "输入包含非数字内容");
				textField.setText("");
				return;
			}
		}
		String[] numStrs = arrayStr.split(" {1,}");// 分割字符串
		int[] numArray = new int[numStrs.length];// 创建整数数组
		// 转换输入为整数数组
		for (int i = 0; i < numArray.length; i++) {
			numArray[i] = Integer.parseInt(numStrs[i], 16);// 十六进制转换为十进制
		}

		for (int j = 0; j < numArray.length; j++) {
			if (numArray.length != 13)
				label.setText("长度要等于13!");
			else {
				if (133 == numArray[10]) { // 当第11个数为Ox85=133时,输出电压模拟值
					label.setText("电压模拟值为:" + numArray[11]);
				} else
					label.setText("不是酒精传感器的数据!");
			}
		}

	}

}


 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值