Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 编写一个程序,提示用户输入字符串,然后显示字符总数和小写字符总数_Java_String_Char - Fatal编程技术网

Java 编写一个程序,提示用户输入字符串,然后显示字符总数和小写字符总数

Java 编写一个程序,提示用户输入字符串,然后显示字符总数和小写字符总数,java,string,char,Java,String,Char,我是初学者,请帮帮我!因此,我们的目标是用简单的java编写一个程序,要求某人输入一个字符串,然后打印总字符数,以及总小写字符数,这是我到目前为止得到的 import java.util.Scanner; public class test2 { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.println("Enter

我是初学者,请帮帮我!因此,我们的目标是用简单的java编写一个程序,要求某人输入一个字符串,然后打印总字符数,以及总小写字符数,这是我到目前为止得到的

import java.util.Scanner;

public class test2 {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a string");
        String s = input.nextLine;
        String lower = "";
        String total = "";

        for (int i = 0; i < s.length(); i++) {
            char thisChar = s.charAt(i);
        } if (thisChar >= 97 && thisChar <= 122) {
            lower += thisChar;
        }

        System.out.println("Total amount of characters: " + s.length() + " - " + s);
        System.out.println("Lower case letters: " + lower.length() + " - " + lower);    
    }
}
import java.util.Scanner;
公共类test2{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入字符串”);
字符串s=input.nextLine;
字符串下限=”;
字符串总数=”;
对于(int i=0;i=97&&thisChar请尝试以下操作:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a string");
    String s = input.nextLine();// methods should be called with () even if have no argument

    String lower = "";
    String total = "";

    for (int i = 0; i < s.length(); i++) {
        char thisChar = s.charAt(i);
        // you can replace with : if (Character.isLowerCase(thisChar))
        if (thisChar >= 97 && thisChar <= 122) {
            lower += thisChar;
        }
    }//close the for loop here

    System.out.println("Total amount of characters: " + s.length() + " - " + s);
    System.out.println("Lower case letters: " + lower.length() + " - " + lower);
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入字符串”);
字符串s=input.nextLine();//即使没有参数,也应使用()调用方法
字符串下限=”;
字符串总数=”;
对于(int i=0;i=97&&thisChar问题是什么

看起来您比应该的更早结束了“for”循环

  • nextLine是一个方法,因此它后面应该有括号。
    nextLine()
  • 如果您将一个字符与另一个字符进行比较,编译器将自动使用它们的ASCII码对它们进行比较,因此您不必知道任何字母的ASCII码,只需按原样插入字符即可
  • if语句应该在for循环中,这很有意义;)
检查此代码以便更好地理解

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a string");
        String s = input.nextLine();

        String lower = "";

        for (int i = 0; i < s.length(); i++) {
            char thisChar = s.charAt(i);

            if (thisChar >= 'a' && thisChar <= 'z') {
                lower += thisChar;
            }
        }

        System.out.println("Total amount of characters: " + s.length() + " - " + s);
        System.out.println("Lower case letters: " + lower.length() + " - "+ lower);
    }
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入字符串”);
字符串s=input.nextLine();
字符串下限=”;
对于(int i=0;i='a'&&thisChar
  • Scanner.nextLine()
    是一种方法,而不是变量,因此您应该将
    ()
    放在它后面。查看您的
    字符串s=input.nextLine;
    并将其更改为
    字符串s=input.nextLine();

  • 支架

    for (int i = 0; i < s.length(); i++) {
        char thisChar = s.charAt(i);
    } if (thisChar >= 97 && thisChar <= 122) {
        lower += thisChar;
    }
    
  • 使用“a”和“z”作为范围。因此if将

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

  • 现在,问题出在哪里?您遇到了错误?其他问题?异常?顺便说一下:如果您遇到非BMP字符或组合字符,您会大吃一惊:
    string.length()
    只给出UTF-16代码单位的数量,而不是Unicode字符。成功了!谢谢大家的帮助!!请接受帮助您更多的答案!
    if (thisChar >= 'a' && thisChar <= 'z') {
    
    int lower = 0;
    
    for (int i = 0; i < s.length(); i++) {
        char thisChar = s.charAt(i);
       if (thisChar >= 'a' && thisChar <= 'z') {
          lower ++;
      }
    }