import javautilScanner;
/
@author Administrator
/
public class Data01 {
public static void main(String[] args) {
int englishCount = 0;// 英文字母个数
int spaceCount = 0;// 空格个数
int numCount = 0;// 数字个数
int otherCount = 0;// 其他字符个数
Scanner sc = new Scanner(Systemin);
Systemoutprintln("请您输入一行字符:");
String str = scnextLine();// 取得控制台输入的一行字符
char[] ch = strtoCharArray();// 把取道的字符串变成一个char数组
for (int i = 0; i < chlength; i++) {
if (CharacterisLetter(ch[i])) {
// 判断是否为字母
englishCount++;
} else if (CharacterisSpaceChar(ch[i])) {
// 判断是否为空格
spaceCount++;
} else if (CharacterisDigit(ch[i])) {
// 判断是否为数字
numCount++;
} else {
// 以上都不是则认为是其他字符
otherCount++;
}
}
Systemoutprintln("字母的个数:" + englishCount);
Systemoutprintln("数字的个数:" + numCount);
Systemoutprintln("空格的个数:" + spaceCount);
Systemoutprintln("其他字符的个数:" + otherCount);
}
}
#include <stdioh>
int main(){
double r, pi = 314159;
printf("请输入半径:");
scanf("%lf", &r);
printf("圆的面积是%2lf\n", pi r r);
return 0;
}
ava初学者,一定对从键盘输入数据感到困难,使用下面的类Input,可以
方便的从键盘输入数据:
使用方法举例: String s=InputreadString(); 读入字符串
int i=InputreadInt(); 读入整数
下面是java输入输出基本类Input类的源代码:
最后以从键盘输入10个整数为例说明之。
import javaio;
class Input
{static InputStreamReader in;
static BufferedReader reader;
static
{in=new InputStreamReader(Systemin);
reader=new BufferedReader(in);
}
static String readString()
{String s="";
try
{ s=readerreadLine();
}
catch(IOException e)
{Systemoutprintln(e);
Systemexit(0);
}
return s;
}
static char readChar()
{char ch='a';
try
{
String s=readString();
ch=scharAt(0);
}
catch(Exception e)
{Systemoutprintln("输入的数据类型不对,程序将退出");
Systemexit(0);
}
return ch;
}
static int readInt()
{String s=readString();
int i=0;
try{
i=IntegerparseInt(s);
}
catch(Exception e)
{Systemoutprintln("输入的数据类型不对,程序将退出");
Systemexit(0);
}
return i;
}
static double readDouble()
{String s=readString();
double d=00;
try
{d=DoubleparseDouble(s);
}
catch(Exception e)
{Systemoutprintln("输入的数据类型不对,程序将退出");
Systemexit(0);
}
return d;
}
static float readFloat()
{
String s=readString();
float f=00f;
try
{
f=FloatparseFloat(s);
}
catch(Exception e)
{ Systemoutprintln("输入的数据类型不对,程序将退出");
Systemexit(0);
}
return f;
}
}
/用法举例,从键盘输入十个整数:/
class InoutData
{public static void main(String args[])
{ int a[]=new int[10];
for(int i=0;i<10;i++)
{ Systemoutprintln("请输入第"+(i+1)+"个数:");
a[i]=InputreadInt();
}
for(int i=0;i<10;i++)
Systemoutprintln("a["+i+"]="+a[i]);
}
}
public static void main(String[] args) throws ParseException {
Scanner in = new Scanner(Systemin);
Systemoutprint("请输入整型数据:");
int i = innextInt();
Systemoutprint("你输入的数据是:"+i);
}
1、一般是使用getch获取
方向键(←): 0xe04b
方向键(↑): 0xe048
方向键(→): 0xe04d
方向键(↓): 0xe050
一个方向键要用两个getch才能把它的数据获取完
然后对应着两个字符数据, 一个当高位, 一个当低位, 就可以判断按下的是什么键, 如:
char ch = getch();
if(ch == 0xe000) //如果高位相等
{
ch = getch(); //那么再获取一个
if (ch == 0x004b) //如果低位也相同(和方向键左一样)
//要执行的动作
}
else //如果不是, 把这个字符吸收了
getch()
2、例程:#include <stdioh>
#include <stdlibh>
main()
{
int key,key2;
key = getch();
printf("\nkey value = 0x%02x -- %d\n",key,key);
key = getch();
printf("\nkey value = 0x%02x -- %d\n",key,key);
}
以上就是关于微机原理编程,如何读取从键盘接收字符串的实际个数全部的内容,包括:微机原理编程,如何读取从键盘接收字符串的实际个数、C语言中如何执行 获得一个从键盘上输入的数字、在java中怎样从键盘输入数字(新手问题)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)