SDUT java lab 7.3

简介: SDUT java lab 7.3

7-3 sdut-JAVA-Common Plurals

分数 9

全屏浏览

切换布局

作者 马新娟

单位 山东理工大学

When forming plurals of common nouns that end in y in English the following rules are observed:

• Common nouns ending in y preceded by a vowel form their plurals by adding s, for example, bay, bays; key, keys; toy, toys.

• Common nouns ending in y preceded by a consonant or by qu change the y to i and add es, for example, city, cities; faculty, faculties; soliloquy, soliloquies; You should accept a word from the end user and if its plural can be formed using the rules specified here you should output the word input and its plural form otherwise, you should state that the plural form of the word cannot be formed using these rules.

Input Specification:

Accept a word from the end user.

Output Specification:

If its plural can be formed using the rules specified here you should output the word input and its plural form otherwise, you should state that the plural form of the word cannot be formed using these rules.

Sample Input:

bay

Sample Output:

bay in plural form is: bays
student

Sample Output:

Cannot form the plural form with the rules given.
soliloquy

Sample Output:

soliloquy in plural form is: soliloquies

代码长度限制

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 n = arr.length();
        if(arr.charAt(n-1) != 'y'){
            System.out.println("Cannot form the plural form with the rules given.");
            System.exit(0);
        }
        else{
          if(arr.charAt(n-3) == 'q' && arr.charAt(n-2) == 'u'){
            
            System.out.print(arr+" in plural form is: " );
            for(int i = 0;i<arr.length()-1;i++){
              System.out.print(arr.charAt(i));
            }
            System.out.println("ies");
            System.exit(0);
          }
          else if(arr.charAt(n-2) == 'a' || arr.charAt(n-2) == 'e' ||arr.charAt(n-2) == 'i' ||arr.charAt(n-2) == 'o' ||arr.charAt(n-2) == 'u' ){
            System.out.println(arr+" in plural form is: " +arr+ "s");
          }
          else{
            System.out.print(arr+" in plural form is: " );
            for(int i = 0;i<arr.length()-1;i++){
              System.out.println(arr.charAt(i));
            }
            System.out.println("ies");
            System.exit(0);
 
          }
 
        }
 
       
}
}


目录
相关文章
|
8月前
|
Java
sdut java lab 7.1(法二好理解)
sdut java lab 7.1(法二好理解)
63 1
|
8月前
|
Java 应用服务中间件 AHAS
sdut java lab6主观题
sdut java lab6主观题
40 0
|
8月前
|
人工智能 Java
sdut java lab5
sdut java lab5
41 0
|
6月前
|
Java
java lab 8------7-1 sdut-JAVA-a contacts list(s)(multi-arraylists)
java lab 8------7-1 sdut-JAVA-a contacts list(s)(multi-arraylists)
27 0
|
8月前
|
Java
SDUT JAVA lab3.9
SDUT JAVA lab3.9
52 2
|
8月前
|
Java
SDUT Java lab6
SDUT Java lab6
32 0
|
8月前
|
Java
sdut java lab7单选
sdut java lab7单选
49 0
|
7天前
|
Java
Java—多线程实现生产消费者
本文介绍了多线程实现生产消费者模式的三个版本。Version1包含四个类:`Producer`(生产者)、`Consumer`(消费者)、`Resource`(公共资源)和`TestMain`(测试类)。通过`synchronized`和`wait/notify`机制控制线程同步,但存在多个生产者或消费者时可能出现多次生产和消费的问题。 Version2将`if`改为`while`,解决了多次生产和消费的问题,但仍可能因`notify()`随机唤醒线程而导致死锁。因此,引入了`notifyAll()`来唤醒所有等待线程,但这会带来性能问题。
Java—多线程实现生产消费者
|
9天前
|
安全 Java Kotlin
Java多线程——synchronized、volatile 保障可见性
Java多线程中,`synchronized` 和 `volatile` 关键字用于保障可见性。`synchronized` 保证原子性、可见性和有序性,通过锁机制确保线程安全;`volatile` 仅保证可见性和有序性,不保证原子性。代码示例展示了如何使用 `synchronized` 和 `volatile` 解决主线程无法感知子线程修改共享变量的问题。总结:`volatile` 确保不同线程对共享变量操作的可见性,使一个线程修改后,其他线程能立即看到最新值。
|
9天前
|
消息中间件 缓存 安全
Java多线程是什么
Java多线程简介:本文介绍了Java中常见的线程池类型,包括`newCachedThreadPool`(适用于短期异步任务)、`newFixedThreadPool`(适用于固定数量的长期任务)、`newScheduledThreadPool`(支持定时和周期性任务)以及`newSingleThreadExecutor`(保证任务顺序执行)。同时,文章还讲解了Java中的锁机制,如`synchronized`关键字、CAS操作及其实现方式,并详细描述了可重入锁`ReentrantLock`和读写锁`ReadWriteLock`的工作原理与应用场景。