import java.util.Scanner;
public class Module2 {
Scanner reader;
String inputString;
int startIndex;
int endIndex;
public Module2(){
reader=null;
inputString="";
startIndex=-1;
endIndex=-1;
}
public static void main(String[] args) {
//Creating new Module2 Object using the constructor from a
Module2 obj = new Module2();
System.out.println("Starting Application");
String firstinput = obj.getinputString("Please enter your name");
System.out.println(firstinput);
String secondinput = obj.getinputStart("Please enter a starting index value");
System.out.println(secondinput);
String thirdinput = obj.getinputEnd("Please enter a ending index value");
System.out.println(thirdinput);
System.out.println("Ending Application");
}
String getinputString(String first) {
System.out.println(first);
reader = new Scanner(System.in);
String input = reader.nextLine();
first = input;
return "Thank you.";
}
String getinputStart(String first) {
System.out.println(first);
reader = new Scanner(System.in);
int input = reader.nextInt();
startIndex = input;
return "Thank you.";
}
String getinputEnd(String first) {
System.out.println(first);
reader = new Scanner(System.in);
int input = reader.nextInt();
endIndex = input;
return "Thank You";
}
输出(它是如何工作的):
盯着申请
请输入您的姓名: Harshal "谢谢你"
请输入您的起始索引值: 1 "谢谢你"
请输入结束指数值: 1 "谢谢你"
结束申请
它不应该如何工作: 盯着申请
请输入您的姓名: 3 "谢谢你"
请输入您的起始索引值: -1 "谢谢你"
请输入结束指数值: -3 "谢谢你"
结束申请
答案 0 :(得分:0)
检查名称是否只包含空格:
public static boolean onlyLettersSpaces(String s){
for(i=0;i<s.length();i++){
char ch = s.charAt(i);
if (Character.isLetter(ch) || ch == ' ') {
continue;
}
return false;
}
return true;
}
如果您想检查数字是否为负数
boolean isPositive(int number) {
if (number < 0) return false;
return true;
}
使用while循环返回并询问相同的问题,如果答案错误,或者让用户重新启动程序本身。