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


目录
相关文章
|
4月前
|
Java
sdut java lab 7.1(法二好理解)
sdut java lab 7.1(法二好理解)
50 1
|
4月前
|
Java 应用服务中间件 AHAS
sdut java lab6主观题
sdut java lab6主观题
30 0
|
4月前
|
人工智能 Java
sdut java lab5
sdut java lab5
32 0
|
4月前
|
Java
sdut java lab7.2(法二)
sdut java lab7.2(法二)
39 0
|
4月前
|
Java
SDUT JAVA lab3.9
SDUT JAVA lab3.9
39 2
|
4月前
|
Java
SDUT Java lab6
SDUT Java lab6
22 0
|
4月前
|
Java
sdut java lab7单选
sdut java lab7单选
34 0
|
3天前
|
存储 缓存 Java
java线程内存模型底层实现原理
java线程内存模型底层实现原理
java线程内存模型底层实现原理
|
8天前
|
缓存 Java 应用服务中间件
Java虚拟线程探究与性能解析
本文主要介绍了阿里云在Java-虚拟-线程任务中的新进展和技术细节。
|
5天前
|
Java 开发者
Java中的多线程基础与应用
【9月更文挑战第22天】在Java的世界中,多线程是一块基石,它支撑着现代并发编程的大厦。本文将深入浅出地介绍Java中多线程的基本概念、创建方法以及常见的应用场景,帮助读者理解并掌握这一核心技术。