我在开关上输入有问题,“选择”是整数,当您输入 在“选择”时,String编译器显示出期望值。因此,我放置了一个try-catch块 在我的swicht上,但是现在当“ choice”为String时,我的程序不会停止。 ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ...................................
java.input.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at Main.main(Main.java:81)
我已经休息了;在我的视野中
import java.util.*;
import java.io.IOException;
public class Main
{
public static void main(String args[]){
Scanner scan= new Scanner(System.in);
User cg = new User("Clarke" ,"griffinc@gmail.com");
User bb = new User("Bellamy" ,"blakeb@gmail.com");
User ob = new User("Octavia" ,"blakeo@gmail.com");
User rr = new User("Raven" ,"reyesr@gmail.com");
User jm = new User("John" ,"murphyj@gmail.com");
User mg = new User("Monty" ,"greenm@gmail.com");
Message message1= new Message("Clarke","Clake :...");
Message message2= new Message("Bellame","Bellame:");
Message message3= new Message("Octavia","Octavia:..");
Message message4= new Message("Raven","Raven:..");
Message message5= new Message("John","John:...");
Message message6= new Message("Monty","Monty:Be the good guys.May
we met again");
ArrayList <User> users= new ArrayList<>();
ArrayList<Message> message= new ArrayList<>();
users.add(cg);
users.add(bb);
users.add(ob);
users.add(rr);
users.add(jm);
users.add(mg);
message.add(message1);
message.add(message2);
message.add(message3);
message.add(message4);
message.add(message5);
message.add(message6);
//login
boolean login=false;
boolean newlogin=false;
User loginUser=new User(" "," ");
while(newlogin==false){
while (login==false){
for(int j=0; j<users.size(); j++)
{
System.out.println(j+1 +":"+ users.get(j).getUsername());
}
System.out.println("Give your username:");
String username=scan.next();
for(int i = 0; i < users.size(); i++)
{
if(username.equals(users.get(i).getUsername() ) ) {
loginUser=users.get(i);
login=true;
}
}
}
//Menu
boolean luna=false;
System.out.println("You are online!");
while(luna==false){
System.out.println("||Menu|| Choose:");
System.out.println("Press 1 -> See Your Wall");
System.out.println("Press 2-> See Your Friend Wall");
System.out.println("Press 3-> Send friend request");
System.out.println("Press 4->Accept/Reject friend request");
System.out.println("Press 5->See My Friends");
System.out.println("Press 6->Remove a Frined");
System.out.println("Press 7->Create a Message");
System.out.println("Press 8->New Login");
System.out.println("Press 9->Exit");
try{
int choice =scan.nextInt();
switch(choice){
case 1:
for(int z = 0; z < message.size(); z++){
if(loginUser.getUsername().equals(message.get(z).getUsername() )){
System.out.println("//Your Wall//" + message.get(z).getPost() +
message.get(z).Time());
}
}
luna=false;
break;
case 2:
System.out.println("Who's wall do you want to see");
String myf=scan.next();
for(int w = 0; w< users.size(); w++){
if(myf.equals(users.get(w).getUsername()) ){
System.out.print( "//" + myf + "'s Wall//" );
for(int q = 0; q < message.size(); q++){
if (myf.equals(message.get(q).getUsername())){
System.out.println(message.get(q).getPost()+
message.get(q).Time());
}
}
}
}
luna=false;
break;
case 3:
loginUser.sendFR();
String fr= scan.next();
for(int p = 0; p< users.size(); p++){
if(fr.equals(users.get(p).getUsername()) &&
!fr.equals(loginUser.getUsername())){
users.get(p).addFR(loginUser);
}
}
luna =false;
break;
case 4:
loginUser.readFR();
System.out.println("Do you want someone to be your friend?(write
his/her username)");
String yn=scan.next();
for(int t = 0; t< users.size(); t++)
{
if(loginUser.getFriendsR(yn)==true){
User u1= new User(yn,"");
loginUser.addFriends(u1);
}
}
luna =false;
break;
case 5:
loginUser.yourFriends();
luna=false;
break;
case 6:
loginUser.yourFriends();
System.out.println("Which user do you delete?(write his/her username) ");
String del=scan.next();
User delU= new User(del,"");
if( loginUser.getFriends(del)==true)
loginUser.removeFriends(delU);
luna=false;
break;
case 7:
System.out.println("Press 1 to post on your wall \nPress 2 to post on a friend's wall");
int b=scan.nextInt();
if(b==1){
System.out.println("What do you want to post?");
String pm=scan.nextLine();
Message ms= new Message(loginUser.getUsername(),pm);
message.add(ms);
}
if(b==2){
System.out.println("Whose friend wall do you want to post? (Write his/her username)");
String f=scan.nextLine();
if(loginUser.getFriends(f)==true){
System.out.println("What do you want to post?");
String pm=scan.nextLine();
Message me= new Message(f,pm);
message.add(me);
}
}
luna=false;
break;
case 8:
luna=true;
login=false;
break;
case 9:
luna=true;
newlogin=true;
break;
default :
luna=false;
break;
}
}
catch(Exception e){
}
}
}
}
}