输入Letter并从大写改为小写,反之亦然?

时间:2014-05-25 17:57:23

标签: java string

所以我必须编写一个执行以下操作的程序:

  1. 一次输入1个字母,然后按enter(" A")
  2. 按Enter键将字母改为大写或小写(" a")
  3. 输出按Enter的结果(结果为a)
  4. 继续上述操作,直到用户输入"。"结束程序
  5. 我不确定为什么我的程序不起作用。它一直在说"无法解决符号"在letter.isUpperCaseletter.isLowerCase上。如果你们/ gals可以解决这个问题,如果你发现任何问题,请告诉我!提前谢谢!

    import java.io.*;
    class Letter {
        public static void main(String[] args) throws IOException {
    
            InputStreamReader inStream = new InputStreamReader (System.in);
            BufferedReader mVHS = new BufferedReader (inStream);
    
            String inData; //Store the input data in a String    
            int length;//Assign the length to the int data type
            String letter, result, result2; //Store the sentence in the String type
    
            //Enter a letter
            System.out.println("Type a letter one at a time");
            System.out.println("Press enter when a letter is entered");
            System.out.println("When done typing all the letters, press . to end the program");
            System.out.println("Enter a letter:");
            String userInput = mVHS.readLine();
            letter = userInput;
    
            result = letter.toUpperCase();
            result2 = letter.toLowerCase();
    
            if (letter.isUpperCase(letter.charAt(0)))
                System.out.println(result);
            else if (letter.isLowerCase(letter.charAt(0)))
                System.out.println(result2); 
    
            //while loop if sentence does not end with a "."
            while(letter.length() == 1){
            {
                if (letter.endsWith("."))
                    System.out.println("Thank you come again."); //What the monitor outputs in this condition
                else  
                    System.out.println("Enter another letter:");//What the monitor outputs in this condition
                userInput = mVHS.readLine();
                letter = userInput;
            }
            }
        }
    }
    

2 个答案:

答案 0 :(得分:0)

isUpperCase上没有名为isLowerCaseString的方法。所以,从

改变
 letter.isUpperCase(letter.charAt(0))

Character.isUpperCase(letter.charAt(0));

答案 1 :(得分:0)

检查以下代码

import java.io.*;

班级信{     public static void main(String [] args)抛出IOException {

    InputStreamReader inStream = new InputStreamReader (System.in);
    BufferedReader mVHS = new BufferedReader (inStream);


    String letter; //Store the sentence in the String type

    //Enter a letter
    System.out.println("Type a letter one at a time");
    System.out.println("Press enter when a letter is entered");
    System.out.println("When done typing all the letters, press . to end the program");
    System.out.println("Enter a letter:");
    String userInput = mVHS.readLine();
    letter = toggleCase(userInput);
       System.out.println(letter);

    //while loop if sentence does not end with a "."
    while(letter.length() == 1){
    {
        if (letter.endsWith("."))
            System.out.println("Thank you come again."); //What the monitor outputs in this condition
        else  
            System.out.println("Enter another letter:");//What the monitor outputs in this condition
        userInput = mVHS.readLine();

        letter = toggleCase(userInput);
        System.out.println(letter);
    }
    }
}

private static String toggleCase(String userInput) {
    String letter;
    String upperString;
    letter = userInput;

    upperString = letter.toUpperCase();


    if (letter.equals(upperString)){
        return (letter.toLowerCase());
    }
    else{
        return (upperString);
    }

}

}