设计c语言程序(输入三位数,分别输出该数各个数位上的数字)

emmmm什么意思2023-04-23  23

#include <stdioh>

int main()

{int x,a,b,c;

printf("请输入一个三位数:");

scanf("%d",&x);

a=x/100;

b=x/10%10;

c=x%10;

printf("百位是%d\n十位是%d\n个位是%d\n",a,b,c);

return 0;

}

要在文本框输入之后敲回车在标签中显示,需要给文本框加上ActionListener监听器,并重写其

actionPerformed方法,完整的Java程序如下:(改动的地方见注释,当"输入选号"为空时回车,会把"当前选号"置为空)

import javaawtBorderLayout;

import javaawtFlowLayout;

import javaawtFont;

import javaawtGridLayout;

import javaawteventActionEvent;

import javaawteventActionListener;//这里导入监听接口

import javaxswingBorderFactory;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJLabel;

import javaxswingJPanel;

import javaxswingJTextArea;

import javaxswingJTextField;

public class CCG extends JFrame implements ActionListener{//这里加监听接口

 JLabel jl1=new JLabel("35选7",JLabelLEFT);

 JLabel jl2=new JLabel("输入选号:");

 JLabel jl3=new JLabel("当前选号:");

 JLabel jl4=new JLabel();    //这里改一下

 JTextField jtf=new JTextField(5);

 JTextArea jta=new JTextArea(5,20);

 JButton jb1=new JButton("添加本组号码");

 JButton jb2=new JButton("验证并保存到文件");

 JPanel jp1=new JPanel();

 JPanel jp2=new JPanel();

 JPanel jp3=new JPanel();

 JPanel jp4=new JPanel();

 JPanel jp5=new JPanel();

 JPanel jp6=new JPanel();

 JPanel jp7=new JPanel();

 CCG(){

  super("Lottery");

  jtfaddActionListener(this);   //这里加监听器

  jl1setFont(new Font(null,FontBOLD,20));

  jp1setLayout(new FlowLayout(FlowLayoutLEFT));

  jp1add(jl1);

  jp1setBorder(BorderFactorycreateEtchedBorder ());

  jp4setLayout(new FlowLayout(FlowLayoutLEFT));

  jp4add(jl2);jp4add(jtf);

  jp5setLayout(new FlowLayout(FlowLayoutLEFT));

  jp5add(jl3);jp5add(jl4);

  jp6setLayout(new FlowLayout(FlowLayoutLEFT));

  jp6add(jb1);

  jp2setLayout(new GridLayout(3,1));

  jp2add(jp4);jp2add(jp5);jp2add(jp6);

  jp2setBorder(BorderFactorycreateEtchedBorder ());

  jp3setLayout(new FlowLayout(FlowLayoutLEFT));

  jp3add(jta);jp3add(jb2);

  jp3setBorder(BorderFactorycreateEtchedBorder ());

  jp7setLayout(new GridLayout(2,1));

  jp7add(jp2);

  jp7add(jp3);

  add(jp1,BorderLayoutNORTH);

  add(jp7,BorderLayoutCENTER);

  setSize(400, 300);

  setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

  setLocationRelativeTo(null);

  setVisible(true);

 }

 @Override

 public void actionPerformed(ActionEvent ae) {//这里加监听函数

  if(aegetSource()==jtf){

   if(jtfgetText()equals("")){

    jl4setText("");

   }else{

    if(jl4getText()equals("")){

     jl4setText(jtfgetText());

    }else{

     jl4setText(jl4getText()+","+jtfgetText());

    }

   }

  }

 }

 public static void main(String[] args) {

  new CCG();

 }

}

运行结果:

以上就是关于设计c语言程序(输入三位数,分别输出该数各个数位上的数字)全部的内容,包括:设计c语言程序(输入三位数,分别输出该数各个数位上的数字)、求教java大神,怎么在文本框输入之后,敲回车在标签上显示,如图片所示、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

转载请注明原文地址:https://juke.outofmemory.cn/read/3643508.html

最新回复(0)