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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23047763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:17:50  来源:igfitidea点击:

Write a program that prompts the user to enter a string, then displays the total number of characters and total number of lower case characters

javastringchar

提问by user3529812

I am a beginner so please help me! so the goal is to write a program in simple java that will ask someone to enter a string, then prints the total characters, as well as the total lower case characters, here is what I have so far...

我是初学者,所以请帮助我!所以目标是用简单的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);    
    }
}

采纳答案by mok

Try this:

尝试这个:

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);
}

回答by ZivS

What is the question?

问题是什么?

It looks like you closed your "for" loop earlier than you should

看起来你比你应该更早地关闭了“for”循环

回答by CMPS

  • nextLine is a method so it should be followed by parenthesis. nextLine()
  • If you compare a char with char, the compiler will automatically compare them using their ASCII, so you don't have to know the ASCII of any letter, just insert the char as it is.
  • The if statement should be in the for loop, it makes sense ;)
  • nextLine 是一个方法,所以它后面应该跟括号。 nextLine()
  • 如果将 char 与 char 进行比较,编译器将自动使用它们的 ASCII 比较它们,因此您不必知道任何字母的 ASCII,只需按原样插入字符即可。
  • if 语句应该在 for 循环中,这是有道理的 ;)

Check this code for better understanding.

检查此代码以更好地理解。

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);
    }

回答by Marco Acierno

  1. Scanner.nextLine()is a method, not a variable so you should put the ()after it. Look at your String s = input.nextLine;and change it to String s = input.nextLine();

  2. Braces

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

    as you can see, the first }closes the forso it means: thisCarwill not be anymore visible and you cannot use it outside! Change it to

    for (int i = 0; i < s.length(); i++) {
        char thisChar = s.charAt(i);
     if (thisChar >= 97 && thisChar <= 122) {
        lower += thisChar;
     }
    }
    
  3. Use 'a' and 'z' as range. So the if will be

    if (thisChar >= 'a' && thisChar <= 'z') {
    
  4. You don't need a total variable.

  5. Use a StringBuilderif you need to create a string, you will avoid the creation of useless temporary strings.

  1. Scanner.nextLine()是一个方法,而不是一个变量,所以你应该把()它放在后面。查看您的String s = input.nextLine;并将其更改为String s = input.nextLine();

  2. 大括号

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

    如您所见,第一个}关闭了for所以这意味着:thisCar将不再可见,您不能在外面使用它!将其更改为

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

    if (thisChar >= 'a' && thisChar <= 'z') {
    
  4. 你不需要一个总变量。

  5. StringBuilder如果需要创建字符串,则使用a ,可以避免创建无用的临时字符串。

Logic

逻辑

  1. You need to only count the number of lowercase letters or show too? If the former, just use a integer variable and increment it everytime instead of put everything inside lower String.

    int lower = 0;
    
    for (int i = 0; i < s.length(); i++) {
        char thisChar = s.charAt(i);
       if (thisChar >= 'a' && thisChar <= 'z') {
          lower ++;
      }
    }
    
  1. 你只需要计算小写字母的数量还是显示?如果是前者,只需使用一个整数变量并每次递增它,而不是将所有内容都放在较低的字符串中。

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