java 从键盘上输入一个字母,如果输入的是小写字母,将其转化成大写字母输出,否则原样输出. 输入0程序结束.

如题所述

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import java.util.Scanner;

/**
 *
 * @author Administrator
 */
public class NewClass {
    public static void main(String[] args) {
        String test ;
        Scanner in = new Scanner(System.in);
        while(true){
        System.out.println("请输入您要输入的字符:");
        test = in.nextLine();
        if(test.equals("0"))
            break;                    //输入0时结束
        char test1 = test.charAt(0);
            System.out.println("输出的字符为:");
        System.out.println(Character.isLowerCase(test1)?Character.toUpperCase(test1):test1);
        /*?:三元表达式:若Character.isLowerCase(test1)为真则代表是小写字母,此时输出
        Character.toUpperCase(test1),否则原样输出*/
        }
    }
}

结果如下:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-08-17
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while(true) {
        String s = sc.next();
        if("0".equals(s)) {
            break;
        }else {
            System.out.println(s.toUpperCase());
        }
    }
}

相似回答