设计c语言程序(输入三位数,分别输出该数各个数位上的数字)
发布网友
发布时间:2022-04-24 01:56
我来回答
共5个回答
热心网友
时间:2023-10-20 08:19
#include <stdio.h>
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;
}
热心网友
时间:2023-10-20 08:19
关键是把这个三位数分成三个数位上的单个数字,用除法运算和取余运算%来完成,关键几句如下:
int x,a,b,c
x=423
c=x % 10余数为3,得到个位上的数字
b=(x%100)/10说明:x%100得余数23,再利用两个整数相除结果取整数商,得十位上的数字2
a=x/100得4 ,这还是利用了整数除法的特点
printf("a=%d b=%d c=%d",a,b,c)输出a=4 b=2 c=3
希望对你有用
热心网友
时间:2023-10-20 08:20
/**
*其中的事件响应 机制 比较经典,你可以看看
*
*/
package two;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* 窗体
* @author Administrator
*
*/
public class MyFrame extends JFrame {
private JPanel panelNorth;
private JPanel panleCenter;
private JPanel panelSouth;
private JLabel jlbA, jlbB, jlbC;
private JTextField jtf;
private JTextField jtfR;
private JButton jbtn;
private JTextField jtfB, jtfS, jtfG;
public MyFrame(String title) {
this.setTitle(title);
initMyFrame();
}
private void initMyFrame() {
this.setLayout(new BorderLayout());
panelNorth = new JPanel();
panleCenter = new JPanel();
panelSouth = new JPanel();
jtf = new JTextField(15);
jtfR = new JTextField(15);
jtfR.setText("<----输入数字");
jtfR.setEditable(false);
panelNorth.add(jtf);
panelNorth.add(jtfR);
jbtn = new JButton("显示");
panleCenter.add(jbtn);
jtfB = new JTextField(10);
jtfS = new JTextField(10);
jtfG = new JTextField(10);
jlbA = new JLabel("百位:");
jlbB = new JLabel("十位:");
jlbC = new JLabel("个位:");
panelSouth.add(jlbA);
panelSouth.add(jtfB);
panelSouth.add(jlbB);
panelSouth.add(jtfS);
panelSouth.add(jlbC);
panelSouth.add(jtfG);
this.getContentPane().add(panelNorth, BorderLayout.NORTH);
this.getContentPane().add(panleCenter, BorderLayout.CENTER);
this.getContentPane().add(panelSouth, BorderLayout.SOUTH);
/**
* 给按钮 添加 事件 *
*/
ActionListener al=new BTNListener(this);
jbtn.addActionListener(al);
this.setVisible(true);
this.setBounds(200, 200, 500, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
}
public boolean checkInputString(){
String s=getJTFText();
if(s==null || "".equals(s)){
return false;
}
if(s.length()==3){
if((s.charAt(0)> '0' && s.charAt(0)<='9') && (s.charAt(1)>='0' && s.charAt(1)<='9') && (s.charAt(2)>='0' && s.charAt(2)<='9')){
return true;
}
}
return false;
}
public void showMyFrameMessage(){
this.jtfB.setText(String.valueOf(getJTFText().charAt(0)));
this.jtfS.setText(String.valueOf(getJTFText().charAt(1)));
this.jtfG.setText(String.valueOf(getJTFText().charAt(2)));
}
public void clear(){
this.jtfB.setText("");
this.jtfS.setText("");
this.jtfG.setText("");
}
public String getJTFText(){
return this.jtf.getText();
}
public void setJTFRText(String msg){
this.jtfR.setText(msg);
}
public static void main(String[] args) {
new MyFrame("TITLE");
}
}
/**
* 按钮的 *
* @author Administrator
*
*/
class BTNListener implements ActionListener{
private MyFrame frame;
public BTNListener(MyFrame frame){
this.frame=frame;
}
@Override
public void actionPerformed(ActionEvent e) {
if(frame.checkInputString()){
frame.showMyFrameMessage();
frame.setJTFRText("再次输入数字");
}else{
frame.setJTFRText("错误的输入,再次输入数字");
frame.clear();
}
}
}
把核心的算法给你。自己去实现吧。
int m;//m为输入的三位数。
int a,b,c;
a = m/100;
b = m%100/10;
c = m%100%10;
热心网友
时间:2023-10-20 08:20
#include<stdio.h>
main()
{
int a,b,c,d;
printf("Enter a number:");
scanf("%d",&a)"
b=a%100; //各位的
c=a/100; //百位的
d=(a%100)/10; //10位的
printf("%d%d%d\n",b,d,c);
}
应该是这样了
热心网友
时间:2023-10-20 08:21
#include <stdio.h>
int main()
{
int i=0;
printf("please put a number");
scanf("%d",&i);
while(i)
{
printf("%d\n",i%10);
i=i/10;
}
return 0;
}
热心网友
时间:2023-10-20 08:19
#include <stdio.h>
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;
}
热心网友
时间:2023-10-20 08:19
关键是把这个三位数分成三个数位上的单个数字,用除法运算和取余运算%来完成,关键几句如下:
int x,a,b,c
x=423
c=x % 10余数为3,得到个位上的数字
b=(x%100)/10说明:x%100得余数23,再利用两个整数相除结果取整数商,得十位上的数字2
a=x/100得4 ,这还是利用了整数除法的特点
printf("a=%d b=%d c=%d",a,b,c)输出a=4 b=2 c=3
希望对你有用
热心网友
时间:2023-10-20 08:19
#include <stdio.h>
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;
}
热心网友
时间:2023-10-20 08:20
/**
*其中的事件响应 机制 比较经典,你可以看看
*
*/
package two;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* 窗体
* @author Administrator
*
*/
public class MyFrame extends JFrame {
private JPanel panelNorth;
private JPanel panleCenter;
private JPanel panelSouth;
private JLabel jlbA, jlbB, jlbC;
private JTextField jtf;
private JTextField jtfR;
private JButton jbtn;
private JTextField jtfB, jtfS, jtfG;
public MyFrame(String title) {
this.setTitle(title);
initMyFrame();
}
private void initMyFrame() {
this.setLayout(new BorderLayout());
panelNorth = new JPanel();
panleCenter = new JPanel();
panelSouth = new JPanel();
jtf = new JTextField(15);
jtfR = new JTextField(15);
jtfR.setText("<----输入数字");
jtfR.setEditable(false);
panelNorth.add(jtf);
panelNorth.add(jtfR);
jbtn = new JButton("显示");
panleCenter.add(jbtn);
jtfB = new JTextField(10);
jtfS = new JTextField(10);
jtfG = new JTextField(10);
jlbA = new JLabel("百位:");
jlbB = new JLabel("十位:");
jlbC = new JLabel("个位:");
panelSouth.add(jlbA);
panelSouth.add(jtfB);
panelSouth.add(jlbB);
panelSouth.add(jtfS);
panelSouth.add(jlbC);
panelSouth.add(jtfG);
this.getContentPane().add(panelNorth, BorderLayout.NORTH);
this.getContentPane().add(panleCenter, BorderLayout.CENTER);
this.getContentPane().add(panelSouth, BorderLayout.SOUTH);
/**
* 给按钮 添加 事件 *
*/
ActionListener al=new BTNListener(this);
jbtn.addActionListener(al);
this.setVisible(true);
this.setBounds(200, 200, 500, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
}
public boolean checkInputString(){
String s=getJTFText();
if(s==null || "".equals(s)){
return false;
}
if(s.length()==3){
if((s.charAt(0)> '0' && s.charAt(0)<='9') && (s.charAt(1)>='0' && s.charAt(1)<='9') && (s.charAt(2)>='0' && s.charAt(2)<='9')){
return true;
}
}
return false;
}
public void showMyFrameMessage(){
this.jtfB.setText(String.valueOf(getJTFText().charAt(0)));
this.jtfS.setText(String.valueOf(getJTFText().charAt(1)));
this.jtfG.setText(String.valueOf(getJTFText().charAt(2)));
}
public void clear(){
this.jtfB.setText("");
this.jtfS.setText("");
this.jtfG.setText("");
}
public String getJTFText(){
return this.jtf.getText();
}
public void setJTFRText(String msg){
this.jtfR.setText(msg);
}
public static void main(String[] args) {
new MyFrame("TITLE");
}
}
/**
* 按钮的 *
* @author Administrator
*
*/
class BTNListener implements ActionListener{
private MyFrame frame;
public BTNListener(MyFrame frame){
this.frame=frame;
}
@Override
public void actionPerformed(ActionEvent e) {
if(frame.checkInputString()){
frame.showMyFrameMessage();
frame.setJTFRText("再次输入数字");
}else{
frame.setJTFRText("错误的输入,再次输入数字");
frame.clear();
}
}
}
把核心的算法给你。自己去实现吧。
int m;//m为输入的三位数。
int a,b,c;
a = m/100;
b = m%100/10;
c = m%100%10;
热心网友
时间:2023-10-20 08:20
#include<stdio.h>
main()
{
int a,b,c,d;
printf("Enter a number:");
scanf("%d",&a)"
b=a%100; //各位的
c=a/100; //百位的
d=(a%100)/10; //10位的
printf("%d%d%d\n",b,d,c);
}
应该是这样了
热心网友
时间:2023-10-20 08:21
#include <stdio.h>
int main()
{
int i=0;
printf("please put a number");
scanf("%d",&i);
while(i)
{
printf("%d\n",i%10);
i=i/10;
}
return 0;
}
热心网友
时间:2023-10-20 08:19
关键是把这个三位数分成三个数位上的单个数字,用除法运算和取余运算%来完成,关键几句如下:
int x,a,b,c
x=423
c=x % 10余数为3,得到个位上的数字
b=(x%100)/10说明:x%100得余数23,再利用两个整数相除结果取整数商,得十位上的数字2
a=x/100得4 ,这还是利用了整数除法的特点
printf("a=%d b=%d c=%d",a,b,c)输出a=4 b=2 c=3
希望对你有用
热心网友
时间:2023-10-20 08:20
/**
*其中的事件响应 机制 比较经典,你可以看看
*
*/
package two;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* 窗体
* @author Administrator
*
*/
public class MyFrame extends JFrame {
private JPanel panelNorth;
private JPanel panleCenter;
private JPanel panelSouth;
private JLabel jlbA, jlbB, jlbC;
private JTextField jtf;
private JTextField jtfR;
private JButton jbtn;
private JTextField jtfB, jtfS, jtfG;
public MyFrame(String title) {
this.setTitle(title);
initMyFrame();
}
private void initMyFrame() {
this.setLayout(new BorderLayout());
panelNorth = new JPanel();
panleCenter = new JPanel();
panelSouth = new JPanel();
jtf = new JTextField(15);
jtfR = new JTextField(15);
jtfR.setText("<----输入数字");
jtfR.setEditable(false);
panelNorth.add(jtf);
panelNorth.add(jtfR);
jbtn = new JButton("显示");
panleCenter.add(jbtn);
jtfB = new JTextField(10);
jtfS = new JTextField(10);
jtfG = new JTextField(10);
jlbA = new JLabel("百位:");
jlbB = new JLabel("十位:");
jlbC = new JLabel("个位:");
panelSouth.add(jlbA);
panelSouth.add(jtfB);
panelSouth.add(jlbB);
panelSouth.add(jtfS);
panelSouth.add(jlbC);
panelSouth.add(jtfG);
this.getContentPane().add(panelNorth, BorderLayout.NORTH);
this.getContentPane().add(panleCenter, BorderLayout.CENTER);
this.getContentPane().add(panelSouth, BorderLayout.SOUTH);
/**
* 给按钮 添加 事件 *
*/
ActionListener al=new BTNListener(this);
jbtn.addActionListener(al);
this.setVisible(true);
this.setBounds(200, 200, 500, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
}
public boolean checkInputString(){
String s=getJTFText();
if(s==null || "".equals(s)){
return false;
}
if(s.length()==3){
if((s.charAt(0)> '0' && s.charAt(0)<='9') && (s.charAt(1)>='0' && s.charAt(1)<='9') && (s.charAt(2)>='0' && s.charAt(2)<='9')){
return true;
}
}
return false;
}
public void showMyFrameMessage(){
this.jtfB.setText(String.valueOf(getJTFText().charAt(0)));
this.jtfS.setText(String.valueOf(getJTFText().charAt(1)));
this.jtfG.setText(String.valueOf(getJTFText().charAt(2)));
}
public void clear(){
this.jtfB.setText("");
this.jtfS.setText("");
this.jtfG.setText("");
}
public String getJTFText(){
return this.jtf.getText();
}
public void setJTFRText(String msg){
this.jtfR.setText(msg);
}
public static void main(String[] args) {
new MyFrame("TITLE");
}
}
/**
* 按钮的 *
* @author Administrator
*
*/
class BTNListener implements ActionListener{
private MyFrame frame;
public BTNListener(MyFrame frame){
this.frame=frame;
}
@Override
public void actionPerformed(ActionEvent e) {
if(frame.checkInputString()){
frame.showMyFrameMessage();
frame.setJTFRText("再次输入数字");
}else{
frame.setJTFRText("错误的输入,再次输入数字");
frame.clear();
}
}
}
把核心的算法给你。自己去实现吧。
int m;//m为输入的三位数。
int a,b,c;
a = m/100;
b = m%100/10;
c = m%100%10;
热心网友
时间:2023-10-20 08:20
#include<stdio.h>
main()
{
int a,b,c,d;
printf("Enter a number:");
scanf("%d",&a)"
b=a%100; //各位的
c=a/100; //百位的
d=(a%100)/10; //10位的
printf("%d%d%d\n",b,d,c);
}
应该是这样了
热心网友
时间:2023-10-20 08:21
#include <stdio.h>
int main()
{
int i=0;
printf("please put a number");
scanf("%d",&i);
while(i)
{
printf("%d\n",i%10);
i=i/10;
}
return 0;
}
热心网友
时间:2023-10-20 08:19
#include <stdio.h>
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;
}
热心网友
时间:2023-10-20 08:19
关键是把这个三位数分成三个数位上的单个数字,用除法运算和取余运算%来完成,关键几句如下:
int x,a,b,c
x=423
c=x % 10余数为3,得到个位上的数字
b=(x%100)/10说明:x%100得余数23,再利用两个整数相除结果取整数商,得十位上的数字2
a=x/100得4 ,这还是利用了整数除法的特点
printf("a=%d b=%d c=%d",a,b,c)输出a=4 b=2 c=3
希望对你有用
热心网友
时间:2023-10-20 08:20
/**
*其中的事件响应 机制 比较经典,你可以看看
*
*/
package two;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* 窗体
* @author Administrator
*
*/
public class MyFrame extends JFrame {
private JPanel panelNorth;
private JPanel panleCenter;
private JPanel panelSouth;
private JLabel jlbA, jlbB, jlbC;
private JTextField jtf;
private JTextField jtfR;
private JButton jbtn;
private JTextField jtfB, jtfS, jtfG;
public MyFrame(String title) {
this.setTitle(title);
initMyFrame();
}
private void initMyFrame() {
this.setLayout(new BorderLayout());
panelNorth = new JPanel();
panleCenter = new JPanel();
panelSouth = new JPanel();
jtf = new JTextField(15);
jtfR = new JTextField(15);
jtfR.setText("<----输入数字");
jtfR.setEditable(false);
panelNorth.add(jtf);
panelNorth.add(jtfR);
jbtn = new JButton("显示");
panleCenter.add(jbtn);
jtfB = new JTextField(10);
jtfS = new JTextField(10);
jtfG = new JTextField(10);
jlbA = new JLabel("百位:");
jlbB = new JLabel("十位:");
jlbC = new JLabel("个位:");
panelSouth.add(jlbA);
panelSouth.add(jtfB);
panelSouth.add(jlbB);
panelSouth.add(jtfS);
panelSouth.add(jlbC);
panelSouth.add(jtfG);
this.getContentPane().add(panelNorth, BorderLayout.NORTH);
this.getContentPane().add(panleCenter, BorderLayout.CENTER);
this.getContentPane().add(panelSouth, BorderLayout.SOUTH);
/**
* 给按钮 添加 事件 *
*/
ActionListener al=new BTNListener(this);
jbtn.addActionListener(al);
this.setVisible(true);
this.setBounds(200, 200, 500, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
}
public boolean checkInputString(){
String s=getJTFText();
if(s==null || "".equals(s)){
return false;
}
if(s.length()==3){
if((s.charAt(0)> '0' && s.charAt(0)<='9') && (s.charAt(1)>='0' && s.charAt(1)<='9') && (s.charAt(2)>='0' && s.charAt(2)<='9')){
return true;
}
}
return false;
}
public void showMyFrameMessage(){
this.jtfB.setText(String.valueOf(getJTFText().charAt(0)));
this.jtfS.setText(String.valueOf(getJTFText().charAt(1)));
this.jtfG.setText(String.valueOf(getJTFText().charAt(2)));
}
public void clear(){
this.jtfB.setText("");
this.jtfS.setText("");
this.jtfG.setText("");
}
public String getJTFText(){
return this.jtf.getText();
}
public void setJTFRText(String msg){
this.jtfR.setText(msg);
}
public static void main(String[] args) {
new MyFrame("TITLE");
}
}
/**
* 按钮的 *
* @author Administrator
*
*/
class BTNListener implements ActionListener{
private MyFrame frame;
public BTNListener(MyFrame frame){
this.frame=frame;
}
@Override
public void actionPerformed(ActionEvent e) {
if(frame.checkInputString()){
frame.showMyFrameMessage();
frame.setJTFRText("再次输入数字");
}else{
frame.setJTFRText("错误的输入,再次输入数字");
frame.clear();
}
}
}
把核心的算法给你。自己去实现吧。
int m;//m为输入的三位数。
int a,b,c;
a = m/100;
b = m%100/10;
c = m%100%10;
热心网友
时间:2023-10-20 08:20
#include<stdio.h>
main()
{
int a,b,c,d;
printf("Enter a number:");
scanf("%d",&a)"
b=a%100; //各位的
c=a/100; //百位的
d=(a%100)/10; //10位的
printf("%d%d%d\n",b,d,c);
}
应该是这样了
热心网友
时间:2023-10-20 08:21
#include <stdio.h>
int main()
{
int i=0;
printf("please put a number");
scanf("%d",&i);
while(i)
{
printf("%d\n",i%10);
i=i/10;
}
return 0;
}