编程:输入一个整数,取出它的个位数。

谁可以的帮帮我啊…在这里谢谢了

以下是JAVA做的代码:import java.util.Scanner;public class test { public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
int in;
try{
System.out.println("请输入一个整数:");
in=input.nextInt();
System.out.print("输入的整数的个位数是:"+(in%10));
}catch(Exception e){
System.out.print("输入有误,程序结束!");

}finally{

}

}

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-11-16
//C语言的:#include <stdio.h>
void main()
{
int num;
printf("请输入一个整数:\n");
scanf("%d",&num);
printf("%d的个位数是:%d\n",num,num%10);
} //C++的(功能和楼上的一样):#include <iostream>
#include <string>
using namespace std;
int main()
{
char *ch=new char[];//定义char数组
bool bl=false;
cout<<"请输入一个正整数"<<endl;
cin>>ch;
for(int i=0;i<strlen(ch);i++)//用循环检查输入的char数组里有没有不是数字的无素
{
if(ch[i]>=48&&ch[i]<=57) bl=false;
else
{
bl=true;
break;
}
}
if(bl) cout<<"您的输入有误!"<<endl;//char数组里有不是数字的元素
else cout<<atoi(ch)<<"的个位数是:"<<atoi(ch)%10<<endl;
return 0;
}
相似回答