求职 # 每日一道面试题 # 从键盘输入一个字符串,将小写字母全部转换成大写字母,然后输出到一个磁盘文件"test"中保存。

026 for 求职面试圈 · 2018年04月17日 · 最后由 林月 回复于 2022年05月19日 · 3247 次阅读

要求

用 Python 或者 Java 实现

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 9 条回复 时间 点赞
匿名 #1 · 2018年04月17日

py2

print >> open('test','w'),raw_input().upper()

如果我是面试官,这样的答案不合格,没有任何异常处理,没有任何判断,这样的一个代码出自测试手里,
一定是个不合格的测试

Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        FileWriter fw = new FileWriter(new File("test.txt"));
        fw.write(str.toUpperCase());
        fw.close();
s = input('请输入英文字符:')

while not s.isalpha():
    s = input('请输入英文字符:')

with open('test.txt', 'w', encoding='utf-8') as f:
    f.write(s.upper())

Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
System.out.println(line);
FileWriter fw = null;
File file = new File("test.txt");
if (! file.exists()){
file.createNewFile();
}
fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(line.toUpperCase(),0,line.length());
bw.close();

Scanner scan=new Scanner(System.in);
String str=scan.next();
String Ustr=str.toUpperCase();
File f=new File("C:\Users\dsp\Desktop\test.txt");
if(! f.exists()) {
try {
f.createNewFile();
System.out.println("创建成功");

FileOutputStream sr=new FileOutputStream(f);
byte[] bytes = Ustr.getBytes();
sr.write(bytes);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
try {
FileOutputStream sr = new FileOutputStream(f);
byte[] bytes = Ustr.getBytes();
sr.write(bytes);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

根据 ASCII 码转换

strs = input('请输入字符:')
try:
    new = strs.upper()
except Exception as e:
    print(e)
else:
    with open('test.txt','w',encoding='utf-8') as f:
        f.write(new)
需要 登录 後方可回應,如果你還沒有帳號按這裡 注册