sdut java lab7.2

简介: sdut java lab7.2

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

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'之前。根据这些条件,程序将输出单词是否有效。


目录
相关文章
|
Java
SDUT JAVA lab3.9
SDUT JAVA lab3.9
181 2
|
Java
sdut java lab 7.1(法二好理解)
sdut java lab 7.1(法二好理解)
175 1
|
Java 应用服务中间件 AHAS
sdut java lab6主观题
sdut java lab6主观题
200 0
|
Java
SDUT Java lab6
SDUT Java lab6
181 0
|
Java
sdut java lab7单选
sdut java lab7单选
181 0
|
人工智能 Java
sdut java lab5
sdut java lab5
157 0
|
Java
sdut java lab7.2(法二)
sdut java lab7.2(法二)
152 0
|
9月前
|
JSON 网络协议 安全
【Java】(10)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
426 1
|
9月前
|
JSON 网络协议 安全
【Java基础】(1)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
392 1
|
10月前
|
数据采集 存储 弹性计算
高并发Java爬虫的瓶颈分析与动态线程优化方案
高并发Java爬虫的瓶颈分析与动态线程优化方案