C语言从键盘上输入一个大写字母,C语言编程输入一个字符,如果它是一个大写字 – 手机爱问...

72ae8716ae564f475c914be7bcc7ce39.png

2008-03-17

设计字符界面application程序,输入一字符,如果为小写字母,则转换为大写字符,如果为大写字母,则转换为小写字母;其它字符不变。

我写了下,总是错误,帮忙看下:

import java.awt.*;

import java.awt.event.*;

import java.math.*;

public class java1_4

{

public static void main(String args[])

{

new FrameInOut();

}

}

class FrameInOut extends Frame implements ActionListener

{

Label prompt;

TextField input,output;

FrameInOut()

{

super("大小写转换程序");

prompt = new Label("请输入一个字符");

input = new TextField(6);

output = new TextField(20);

setLayout(new FlowLayout());

add(prompt);

add(input);

add(output);

input.addActionListener(this);

setSize(300,200);

show();

}

public void actionPerformed(ActionEvent e)

{

char ch=input.getText();

if(ch>='a'&&ch<='z'){

ch=(char)(ch-32);

}

if(ch>='A'&&ch<='Z'){

ch=(char)(ch+32); }

output.setText(ch);

}

}

改后的程序,测试通过,正确

import java。awt。*;

import java。awt。event。*;

import java。math。*;

public class java1_4

{

public static void main(String args[])

{

new FrameInOut();

}

}

class FrameInOut extends Frame implements ActionListener

{

Label prompt;

TextField input,output;

FrameInOut()

{

super("大小写转换程序");

prom...全部

改后的程序,测试通过,正确

import java。awt。*;

import java。awt。event。*;

import java。math。*;

public class java1_4

{

public static void main(String args[])

{

new FrameInOut();

}

}

class FrameInOut extends Frame implements ActionListener

{

Label prompt;

TextField input,output;

FrameInOut()

{

super("大小写转换程序");

prompt = new Label("请输入一个字符");

input = new TextField(6);

output = new TextField(20);

setLayout(new FlowLayout());

add(prompt);

add(input);

add(output);

input。

addActionListener(this);

setSize(300,200);

show();

}

public void actionPerformed(ActionEvent e)

{

char ch=input。

getText()。charAt(0); //getText()方法返回值是字符串类型,要转化为char类型

if(ch>='a'&&ch='A'&&ch<='Z'){

ch=(char)(ch+32); }

output。

setText(Character。toString(ch)); //setText()方法括号内的参数应该是字符串类型,所以需将ch转化为字符串类型

}

}

。收起

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值