7-3 判断登录信息–Java字符串比较
从键盘接收用户输入的两个字符串,第一个代表用户名,第二个代表密码。
(1)如果用户名是SwpuIot,并且密码是123456,则输出:Welcome
(2)如果用户名正确,密码不正确,输出:Mismatch
(3)其他情况下,输出:NotExist
要输出的信息,建议从题目中复制,确保精确匹配。
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner in =new Scanner(System.in);
String s1=in.next();
String s2=in.next();
String s3=“SwpuIot”;
String s4=“123456”;
if(s1.equals(s3) && s2.equals(s4))
System.out.println(“Welcome”);
else if(s1.equals(s3)==true&&s2.equals(s4)==false)
System.out.println(“Mismatch”);
else
System.out.println(“NotExist”);
}
}
7-4 jmu-Java-06异常-01-常见异常
自己编码以产生常见异常。
输入说明:
arr 代表产生访问数组是产生的异常。然后输入下标,如果抛出ArrayIndexOutOfBoundsException异常则显示,如果不抛出异常则不显示。
null,产生NullPointerException
cast,尝试将String对象强制转化为Integer对象,产生ClassCastException。
num,然后输入字符,转化为Integer,如果抛出NumberFormatException异常则显示。
其他,结束程序。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int []a = new int [5];
while(true){
String first;
first = sc.next();
if(first.equals("other"))
break;
if(first.equals("arr")){
try {
int secend = sc.nextInt();
int t = a[secend];
}catch(Exception e){
System.out.println(e);
}
}
if(first.equals("null"))
{
try {
String t = null;
int length = t.length();
}catch(Exception e) {
System.out.println(e);
}
}
if(first.equals("cast"))
{
try {
Object ss = new String("string");
System.out.println((Integer)ss);
}catch(Exception e){
System.out.println(e);
}
}
if(first.equals("num"))
{
try {
String c = sc.next();
Integer num = Integer.parseInt(c);
}catch(Exception e)
{
System.out.println(e);
}
}
}
sc.close();
}
}