SDUT java lab7.4

简介: SDUT java lab7.4

7-4 sdut-JAVA-Vowel And Consonant Count

分数 6

全屏浏览

切换布局

作者 马新娟

单位 山东理工大学

You are required to write a Java application program that accepts a word from the user and outputs the total numbers of vowels and the total number of consonants contained in this word. For example, if the user entered the word Today your program would output: There are 2 vowels and 3 consonants in the word Today.

Input Specification:

Accepts a word from the user.

Output Specification:

Outputs the total numbers of vowels and the total number of consonants contained in this word.

Sample Input:

today

Sample Output:

There is/are 2 vowel/s and 3 consonant/s in the input of:
today

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

栈限制

8192 KB

import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
          String arr =sc.nextLine();
          int x = 0;
          int y = 0;
          for(int i = 0 ; i < arr . length() ; i ++ ){
            if(arr.charAt(i) == 'a' ||arr.charAt(i) == 'e' ||arr.charAt(i) == 'i' ||arr.charAt(i) == 'o' ||arr.charAt(i) == 'u' ||arr.charAt(i) == 'A' ||arr.charAt(i) == 'E' ||arr.charAt(i) == 'I' ||arr.charAt(i) == 'O' ||arr.charAt(i) == 'U' ){
              x += 1;
            }
            else{
              y += 1;
            }
          }
          System.out.println("There is/are "+x+" vowel/s and "+y+" consonant/s in the input of:");
          System.out.println(arr);
 
       
}
}

import java.util.Scanner;

导入Java的Scanner类,用于读取用户的输入。

public class Main {

定义了一个名为Main的公共类。

public static void main(String[] args) {

定义了程序的主入口点main方法。

Scanner sc = new Scanner(System.in);

创建了Scanner类的一个实例sc,用于从标准输入读取数据。

String arr = sc.nextLine();

读取用户输入的一行文本,并将其存储在字符串变量arr中。

int x = 0; int y = 0;

初始化两个计数器变量xy,分别用于计数元音和辅音。

for (int i = 0; i < arr.length(); i++) {

开始一个for循环,遍历字符串arr中的每个字符。

if (arr.charAt(i) == 'a' || arr.charAt(i) == 'e' || arr.charAt(i) == 'i' || arr.charAt(i) == 'o' || arr.charAt(i) == 'u' || arr.charAt(i) == 'A' || arr.charAt(i) == 'E' || arr.charAt(i) == 'I' || arr.charAt(i) == 'O' || arr.charAt(i) == 'U') {

检查当前字符是否是元音(包括大小写)。

x += 1;

如果是元音,将元音计数器x加1。

} else {

如果当前字符不是元音,执行else代码块。

y += 1;

将辅音计数器y加1。

}

结束if-else语句。

}

结束for循环。

System.out.println("There is/are " + x + " vowel/s and " + y + " consonant/s in the input of:");

根据元音的数量x,使用"is"或"are",并打印出元音和辅音的数量以及提示信息。

System.out.println(arr);

打印用户输入的原始字符串arr

}

结束main方法。

}

结束Main类。

这个程序首先读取用户输入的一行文本,然后通过遍历每个字符来判断它是元音还是辅音,并相应地更新计数器。最后,程序输出了输入字符串中元音和辅音的数量,并打印出用户输入的原始字符串。注意,这个程序没有考虑标点符号、空格或数字,也没有区分元音前的's'是所有格标志还是复数标志。


目录
相关文章
|
4月前
|
Java
SDUT java lab 7.3
SDUT java lab 7.3
24 0
|
4月前
|
Java
sdut java lab 7.1(法二好理解)
sdut java lab 7.1(法二好理解)
50 1
|
4月前
|
Java 应用服务中间件 AHAS
sdut java lab6主观题
sdut java lab6主观题
30 0
|
4月前
|
人工智能 Java
sdut java lab5
sdut java lab5
32 0
|
4月前
|
Java
sdut java lab7.2(法二)
sdut java lab7.2(法二)
39 0
|
4月前
|
Java
SDUT JAVA lab3.9
SDUT JAVA lab3.9
39 2
|
4月前
|
Java
SDUT Java lab6
SDUT Java lab6
22 0
|
4月前
|
Java
sdut java lab7单选
sdut java lab7单选
34 0
|
4月前
|
存储 Java 索引
sdut java lab 7.6
sdut java lab 7.6
35 0
|
3天前
|
存储 缓存 Java
java线程内存模型底层实现原理
java线程内存模型底层实现原理
java线程内存模型底层实现原理