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 sc=new Scanner(System.in);
    String arr=sc.nextLine();
    
    if(arr.isBlank())
    {
      System.out.println("No input provided to check.");
      System.exit(0);
    }
    
    if(!arr.matches("[a-zA-Z]+"))
      {
      System.out.println("Input should comprise of alphabetic characters only.");
      System.exit(0);
      }
    int indexA=arr.indexOf('a');
    int indexB=arr.indexOf('b');
    if(indexA!=-1&&indexB!=-1&&indexA<indexB)
    {
      System.out.println(arr+" is valid");
    }
    else
    {
      System.out.println(arr+" is not valid");
      
    }
  sc.close();
  }
}


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