sdut java lab 7.1(法二好理解)

简介: sdut java lab 7.1(法二好理解)

7-1 sdut-JAVA-Pig Latin

分数 12

全屏浏览

切换布局

作者 马新娟

单位 山东理工大学

Write a program that requests a word as input, translates the word into Pig Latin and outputs the word input and its equivalent in Pig Latin. The rules for translating a word are as follows:

Rule 1: If a word begins with a consonant, move the first letter to the end of the word and add ay to the end of the word. For example, Chip becomes hipCay.

Rule 2: If the word begins with a vowel, add way to the end of the word. For example, else becomes elseway.

Input Specification:

Request a word as input.

Output Specification:

Outputs the word input and its equivalent in Pig Latin.

Sample Input1:

Sample Output1:

No input provided to convert to Pig Latin.

Sample Input2:

123anksO

Sample Output2:

Input should comprise of alphabetic characters only.

Sample Output3:

123anksO

Sample Output3:

Input should comprise of alphabetic characters only.

Sample Output4:

day

Sample Output4:

Word Input: day

Pig Latin: ayday

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

栈限制

8192 KB

import java.util.Scanner;
public class Main
{
  public static void main(String[] args)
  {
        Scanner in=new Scanner(System.in);
      String arr=in.nextLine();
        if(arr.isBlank())
        {
            System.out.println("No input provided to convert to Pig Latin.");
            return;
        }
        int n=arr.length();
        for(int i=0;i<n;i++)
        {
            
        
         if(arr.charAt(i)>='1'&&arr.charAt(i)<='9')
        {
             System.out.println("Input should comprise of alphabetic characters only.");
            return;
        }
        }
       
    
       
        if(arr.charAt(0)=='a'||arr.charAt(0)=='e'||arr.charAt(0)=='i'||arr.charAt(0)=='o'||arr.charAt(0)=='u'||arr.charAt(0)=='A'||arr.charAt(0)=='E'||arr.charAt(0)=='I'||arr.charAt(0)=='O'||arr.charAt(0)=='U')
        {
            System.out.println("Word Input: "+arr);
            System.out.print("Pig Latin: "+arr+"way");
           
        }
        else{
            System.out.println("Word Input: "+arr);
            System.out.print("Pig Latin: ");
            for(int i=1;i<n;i++)
            {
                System.out.print(arr.charAt(i));
            }
            System.out.println(arr.charAt(0)+"ay");
        }
}
}


目录
相关文章
|
Java 应用服务中间件 AHAS
sdut java lab6主观题
sdut java lab6主观题
98 0
|
人工智能 Java
sdut java lab5
sdut java lab5
84 0
|
Java
SDUT JAVA lab3.9
SDUT JAVA lab3.9
97 2
|
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)
68 0
|
Java
SDUT Java lab6
SDUT Java lab6
54 0
|
Java
sdut java lab7单选
sdut java lab7单选
88 0
|
2月前
|
安全 算法 Java
Java 多线程:线程安全与同步控制的深度解析
本文介绍了 Java 多线程开发的关键技术,涵盖线程的创建与启动、线程安全问题及其解决方案,包括 synchronized 关键字、原子类和线程间通信机制。通过示例代码讲解了多线程编程中的常见问题与优化方法,帮助开发者提升程序性能与稳定性。
130 0
|
2月前
|
Java API 调度
从阻塞到畅通:Java虚拟线程开启并发新纪元
从阻塞到畅通:Java虚拟线程开启并发新纪元
284 83
|
3月前
|
存储 SQL 安全
Java 无锁方式实现高性能线程实战操作指南
本文深入探讨了现代高并发Java应用中单例模式的实现方式,分析了传统单例(如DCL)的局限性,并提出了多种无锁实现方案。包括基于ThreadLocal的延迟初始化、VarHandle原子操作、Record不可变对象、响应式编程(Reactor)以及CDI依赖注入等实现方式。每种方案均附有代码示例及适用场景,同时通过JMH性能测试对比各实现的优劣。最后,结合实际案例设计了一个高性能配置中心,展示了无锁单例在实际开发中的应用。总结中提出根据场景选择合适的实现方式,并遵循现代单例设计原则以优化性能和安全性。文中还提供了代码获取链接,便于读者实践与学习。
94 0

热门文章

最新文章