java中怎么实现从对话框输入一个大写字母将其转化为小写字母输出?
解决时间 2021-08-21 14:40
- 提问者网友:戎马万世
- 2021-08-21 09:16
java中怎么实现从对话框输入一个大写字母将其转化为小写字母输出?
我写的带码如下:
import javax.swing.JOptionPane;
class letter
{
int sr()
{
String x = JOptionPane.showInputDialog(null,"请输入一个大写字母:","输入框",JOptionPane.QUESTION_MESSAGE);
int a = Integer.parseInt(x);
return a;
}
void conversion(int capital_Asc)
{
char capital = (char)(capital_Asc);
char small = (char)(capital_Asc+32);
System.out.print("字母"+capital+"的小写字母是:"+small);
}
}
public class Test2_7
{
public static void main(String[] args)
{
letter mylet = new letter();
int x = mylet.sr();
mylet.conversion(x);
这段代码输入一个大写字母不能运行,比如输个A,提示如下:
Exception in thread "main" java.lang.NumberFormatException: For input string: "A"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at test.letter.sr(Test2_7.java:9)
at test.Test2_7.main(Test2_7.java:24)
但是可以输入数字,如输入A的ASCII码65可以运行:如下
子母A的小写字母是:a
我是初学者,弄不懂这是怎么回事,请教各位高手,谢谢。
最佳答案
- 五星知识达人网友:你可爱的野爹
- 2021-08-21 10:44
已经改好并调试通过:
import javax.swing.JOptionPane;
class letter
{
int sr()
{
String x = JOptionPane.showInputDialog(null,"请输入一个大写字母:","输入框",JOptionPane.QUESTION_MESSAGE);
int a = (int)x.charAt(0); // 直接用(int)强转就行,就这里有问题,其他地方没什么错误
return a;
}
void conversion(int capital_Asc)
{
char capital = (char)(capital_Asc);
char small = (char)(capital_Asc+32);
System.out.print("字母"+capital+"的小写字母是:"+small);
}
}
public class Test2_7
{
public static void main(String[] args)
{
letter mylet = new letter();
int x = mylet.sr();
mylet.conversion(x);
}
}
全部回答
- 1楼网友:一叶十三刺
- 2021-08-21 16:06
import java.io.*;
public class wt
{
public static void main(String args[]) throws Exception
{
String eng1;
BufferedReader buffer1;
buffer1=new BufferedReader(new InputStreamReader(System.in));
while(true){
try{
System.out.print("也请输入字符串");
eng1=buffer1.readLine();
if(eng1.length()<1){
System.out.println("您必须输入字符串!");
}else{
break;
}
}catch(IOException e){
continue;}
}
String s2=eng1.toUpperCase();
System.out.print("转换后的字符串为:"+s2);
}
}
- 2楼网友:深街酒徒
- 2021-08-21 14:48
class letter
{
String sr()
{
String x = JOptionPane.showInputDialog(null,"请输入一个大写字母:","输入框",JOptionPane.QUESTION_MESSAGE);
return x;
}
void conversion(String x)
{
if(x.length<1)
System.out.print("输入为空");
else
System.out.print(x.toUpperCase()
);
}
}
- 3楼网友:迷人又混蛋
- 2021-08-21 13:23
void conversion(int capital_Asc)
你定义的int 参数应该输入数字
输入一个A当然会出错!
- 4楼网友:妄饮晩冬酒
- 2021-08-21 12:00
String a = "a".toUpperCase();//将小写转换成大写
String b = "A".toLowerCase();//将大写转换成小写
我要举报
大家都在看
推荐资讯