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();
  }
}


目录
相关文章
|
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 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线程内存模型底层实现原理
|
14天前
|
存储 缓存 安全
【Java面试题汇总】多线程、JUC、锁篇(2023版)
线程和进程的区别、CAS的ABA问题、AQS、哪些地方使用了CAS、怎么保证线程安全、线程同步方式、synchronized的用法及原理、Lock、volatile、线程的六个状态、ThreadLocal、线程通信方式、创建方式、两种创建线程池的方法、线程池设置合适的线程数、线程安全的集合?ConcurrentHashMap、JUC
【Java面试题汇总】多线程、JUC、锁篇(2023版)
|
8天前
|
缓存 Java 应用服务中间件
Java虚拟线程探究与性能解析
本文主要介绍了阿里云在Java-虚拟-线程任务中的新进展和技术细节。
|
5天前
|
Java 开发者
Java中的多线程基础与应用
【9月更文挑战第22天】在Java的世界中,多线程是一块基石,它支撑着现代并发编程的大厦。本文将深入浅出地介绍Java中多线程的基本概念、创建方法以及常见的应用场景,帮助读者理解并掌握这一核心技术。