sdut java实验7.1

2024-05-01 16:20
文章标签 java 实验 7.1 sdut

本文主要是介绍sdut java实验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 sc = new Scanner(System.in);String arr = sc.nextLine();if(arr.isBlank()){System.out.println("No input provided to convert to Pig Latin.");System.exit(0);}for(int i = 0;i<arr.length();i++){if(Character.isDigit(arr.charAt(i))){System.out.println("Input should comprise of alphabetic characters only.");System.exit(0);}}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.println("Pig Latin: "+arr+"way");System.exit(0);}else {System.out.println("Word Input: "+arr);System.out.print("Pig Latin: ");for(int i = 1;i<arr.length();i++){System.out.print(arr.charAt(i));}System.out.print(arr.charAt(0)+"ay");}}
}
 

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中。

 

if(arr.isBlank()){

检查用户输入的字符串是否是空白的(只包含空格或没有字符)。

 

System.out.println("No input provided to convert to Pig Latin.");

如果输入是空白的,打印一条消息并退出程序。

 

System.exit(0); }

使用System.exit(0)来正常退出程序。

 

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

遍历字符串arr中的每个字符。

 

if(Character.isDigit(arr.charAt(i))){

检查当前字符是否是数字。

 

System.out.println("Input should comprise of alphabetic characters only.");

如果输入包含数字,打印一条消息并退出程序。

 

System.exit(0); } }

结束for循环和if语句。

 

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'){

检查字符串arr的第一个字符是否是元音字母。

 

System.out.println("Word Input: "+arr);

打印原始输入的单词。

 

System.out.println("Pig Latin: "+arr+"way");

对于以元音开头的单词,将"way"添加到单词的末尾,并打印结果。

 

System.exit(0); }

结束if语句,并退出程序。

 

else {

如果第一个字符不是元音字母,执行else代码块。

 

System.out.println("Word Input: "+arr);

打印原始输入的单词。

 

System.out.print("Pig Latin: ");

打印"Pig Latin: "作为输出的开始。

 

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

从字符串arr的第二个字符开始遍历,直到最后一个字符。

 

System.out.print(arr.charAt(i));

在for循环中,打印当前索引处的字符。

 

}

结束for循环。

 

System.out.print(arr.charAt(0)+"ay");

将原始单词的第一个字符和"ay"拼接,并打印出来,形成猪拉丁表达。

 

}

结束else代码块。

 

}

结束main方法。

 

}

结束Main类。

这个程序首先检查输入是否有效,然后根据猪拉丁的规则将输入的单词转换为猪拉丁形式。对于以元音开头的单词,它简单地在单词后面加上"way"。对于以辅音开头的单词,它将第一个辅音移动到单词的末尾,并加上"ay"。

这篇关于sdut java实验7.1的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/952121

相关文章

Java对象和JSON字符串之间的转换方法(全网最清晰)

《Java对象和JSON字符串之间的转换方法(全网最清晰)》:本文主要介绍如何在Java中使用Jackson库将对象转换为JSON字符串,并提供了一个简单的工具类示例,该工具类支持基本的转换功能,... 目录前言1. 引入 Jackson 依赖2. 创建 jsON 工具类3. 使用示例转换 Java 对象为

SpringBoot快速接入OpenAI大模型的方法(JDK8)

《SpringBoot快速接入OpenAI大模型的方法(JDK8)》本文介绍了如何使用AI4J快速接入OpenAI大模型,并展示了如何实现流式与非流式的输出,以及对函数调用的使用,AI4J支持JDK8... 目录使用AI4J快速接入OpenAI大模型介绍AI4J-github快速使用创建SpringBoot

Java中的Cursor使用详解

《Java中的Cursor使用详解》本文介绍了Java中的Cursor接口及其在大数据集处理中的优势,包括逐行读取、分页处理、流控制、动态改变查询、并发控制和减少网络流量等,感兴趣的朋友一起看看吧... 最近看代码,有一段代码涉及到Cursor,感觉写法挺有意思的。注意是Cursor,而不是Consumer

解决java.lang.NullPointerException问题(空指针异常)

《解决java.lang.NullPointerException问题(空指针异常)》本文详细介绍了Java中的NullPointerException异常及其常见原因,包括对象引用为null、数组元... 目录Java.lang.NullPointerException(空指针异常)NullPointer

javaScript在表单提交时获取表单数据的示例代码

《javaScript在表单提交时获取表单数据的示例代码》本文介绍了五种在JavaScript中获取表单数据的方法:使用FormData对象、手动提取表单数据、使用querySelector获取单个字... 方法 1:使用 FormData 对象FormData 是一个方便的内置对象,用于获取表单中的键值

前端知识点之Javascript选择输入框confirm用法

《前端知识点之Javascript选择输入框confirm用法》:本文主要介绍JavaScript中的confirm方法的基本用法、功能特点、注意事项及常见用途,文中通过代码介绍的非常详细,对大家... 目录1. 基本用法2. 功能特点①阻塞行为:confirm 对话框会阻塞脚本的执行,直到用户作出选择。②

SpringBoot项目注入 traceId 追踪整个请求的日志链路(过程详解)

《SpringBoot项目注入traceId追踪整个请求的日志链路(过程详解)》本文介绍了如何在单体SpringBoot项目中通过手动实现过滤器或拦截器来注入traceId,以追踪整个请求的日志链... SpringBoot项目注入 traceId 来追踪整个请求的日志链路,有了 traceId, 我们在排

Java实战之利用POI生成Excel图表

《Java实战之利用POI生成Excel图表》ApachePOI是Java生态中处理Office文档的核心工具,这篇文章主要为大家详细介绍了如何在Excel中创建折线图,柱状图,饼图等常见图表,需要的... 目录一、环境配置与依赖管理二、数据源准备与工作表构建三、图表生成核心步骤1. 折线图(Line Ch

Spring Boot 3 整合 Spring Cloud Gateway实践过程

《SpringBoot3整合SpringCloudGateway实践过程》本文介绍了如何使用SpringCloudAlibaba2023.0.0.0版本构建一个微服务网关,包括统一路由、限... 目录引子为什么需要微服务网关实践1.统一路由2.限流防刷3.登录鉴权小结引子当前微服务架构已成为中大型系统的标

Java集合中的List超详细讲解

《Java集合中的List超详细讲解》本文详细介绍了Java集合框架中的List接口,包括其在集合中的位置、继承体系、常用操作和代码示例,以及不同实现类(如ArrayList、LinkedList和V... 目录一,List的继承体系二,List的常用操作及代码示例1,创建List实例2,增加元素3,访问元