sdut java lab7.2

server/2024/9/23 1:20:29/

7-2 sdut-JAVA-Words Containing AB

分数 9

全屏浏览

切换布局

作者 马新娟

单位 山东理工大学

Write a program that requests a word as input containing the two letters a and b (in this order). Examples of valid words would include, abacus, cab, and anybody, whereas invalid words would include branch, back, and bank. You should only concern yourself with the first occurrence of a and b should they exist.

Input Specification:

a word as input containing the two letters a and b (in this order).

Output Specification:

Output the input word is valid or not.

Sample Input:

 

Sample Output:

No input provided to check.

Sample Input:

about12

Sample Output:

Input should comprise of alphabetic characters only.

Sample Input:

about

Sample Output:

about is valid

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

栈限制

8192 KB

java">import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);String word = scanner.nextLine();String result = checkWordValidity(word);if (result == null) {System.out.println("No input provided to check.");} else {System.out.println(result);}}private static String checkWordValidity(String word) {if (word.isEmpty()) {return null; // No input provided}if (!word.matches("[a-zA-Z]+")) {return "Input should comprise of alphabetic characters only.";}int indexA = word.indexOf('a');int indexB = word.indexOf('b');if (indexA != -1 && indexB != -1 && indexA < indexB) {return word + " is valid";} else {return word + " is not valid";}}
}

程序的目的是检查用户输入的单词是否满足特定的条件,即单词中的字母'a'是否在字母'b'之前。下面是对每一行代码的解释:

import java.util.Scanner;

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

public class Main {

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

public static void main(String[] args) {

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

Scanner scanner = new Scanner(System.in);

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

String word = scanner.nextLine();

调用ScannernextLine方法读取用户输入的一行文本,通常是一个单词,并将其存储在变量word中。

String result = checkWordValidity(word);

调用自定义的checkWordValidity方法,并将用户输入的word作为参数传递。该方法将返回一个字符串,指示该单词是否有效。

if (result == null) {

检查checkWordValidity方法返回的result是否为null

System.out.println("No input provided to check.");

如果resultnull,表示没有输入或输入为空,程序将打印一条消息并退出。

} else { System.out.println(result); }

如果result不是null,则打印checkWordValidity方法返回的结果。

}

结束main方法。

private static String checkWordValidity(String word) {

定义了一个名为checkWordValidity的私有静态方法,它接受一个String类型的参数word,并返回一个String类型的结果。

if (word.isEmpty()) { return null; // No input provided }

检查传入的word是否为空或空白,如果是,则返回null

if (!word.matches("[a-zA-Z]+")) { return "Input should comprise of alphabetic characters only."; }

使用正则表达式验证word是否只包含字母字符,如果不满足条件,则返回一条错误消息。

int indexA = word.indexOf('a'); int indexB = word.indexOf('b');

使用indexOf方法找到单词中'a'和'b'的位置。

if (indexA != -1 && indexB != -1 && indexA < indexB) {

检查'a'和'b'是否都在单词中,并且'a'是否在'b'之前。

return word + " is valid";

如果上述条件满足,返回一条消息表明该单词是有效的。

} else { return word + " is not valid"; }

如果条件不满足,返回一条消息表明该单词是无效的。

}

结束checkWordValidity方法。

}

结束Main类。

这个程序的逻辑是:读取用户输入的单词,检查它是否只包含字母,然后检查'a'是否出现在'b'之前。根据这些条件,程序将输出单词是否有效。


http://www.ppmy.cn/server/39522.html

相关文章

【MySQL基本查询(下)】

文章目录 一、update案例 二、Delete案例注意&#xff1a;delete 全表数据的行为慎用&#xff01;truncate 三、插入查询结果案例 四、了解一些函数1.count函数2.sum函数3. avg函数4.max函数5. min函数 五、group by子句的使用案例having和where 一、update 该关键字的功能就是…

ExcelVBA取序号与合计之间的数据

今天有人提出这样一个问题&#xff0c; ExcelVBA取序号与合计之间的数据 数据如下: 分析一下&#xff0c;问题关键&#xff1a; 问题&#xff1a;1.我要在“序号”两字后面开始取数&#xff0c;因为序号是合并的&#xff0c;所以。。。2.我要取合计前面的数据&#xff0c;所以要…

SpringCloud生态体系介绍

Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发&#xff0c;如服务发现注册、配置中心、智能路由、消息总线、负载均衡、断路器、数据监控等&#xff0c;都可以用Spring Boot的开发风格做到一键启动和部署。 SpringC…

鸿蒙——即将是国内全部物联网的搭载系统

国内物联网时代 中国国内物联网时代是指在中国国内&#xff0c;物联网&#xff08;Internet of Things&#xff0c;简称IoT&#xff09;技术得到广泛应用和发展的时代。在这个时代&#xff0c;各种设备和物品都可以通过互联网进行连接和交互&#xff0c;实现信息的采集、传输和…

Go语言流程控制(二)

switch语句 Go 语言中的 switch 语句是一种选择结构&#xff0c;用于基于不同条件执行不同的代码块。Go 的 switch 相较于其他语言的 switch 有一些独特的特点&#xff0c;使其更为灵活和强大。下面是Go语言中 switch 语句的详细介绍&#xff1a; 基本语法 switch expressio…

notepad++安装 hex-editor插件

打开notepad 点击插件 搜索 hex-editor,点击右侧 安装install 安装成功后&#xff0c;在已安装插件中就有显示了

CAPL如何实现TLS握手认证

CAPL有专门的章节介绍如何实现TLS握手认证的函数: CAPL调用哪些函数实现TLS握手认证,需要了解TLS在整个通信过程的哪个阶段。 首先TCP需要建立连接,这是TLS握手的前提。当TLS握手认证完成后,可以传输数据。 所以TLS握手开始前需要确保TCP建立连接,TCP传输数据前需要确保…

【LLama】Llama3 的本地部署与lora微调(基于xturn)

系列课程代码文档&#xff08;前2节课可跳过&#xff09;&#xff1a;https://github.com/SmartFlowAI/Llama3-Tutorial 课程视频&#xff1a;https://space.bilibili.com/3546636263360696/channel/series XTuner &#xff1a;https://github.com/InternLM/xtuner/blob/main/R…