Java - 基于字母

时间:2015-11-01 19:52:49

标签: java string input char

我一直在寻找这个,但无法找到它。我想使用Scanner从Java中获取用户的命令移动输入。用户可以输入的是U(上),D(下),L(左),R(右)。因此,例如,如果用户编写UDULR(没有任何东西将它们分开),那么我想分别读取字母。我试过这样:

String command;
char command;
Scanner scan = new Scanner(System.in);

System.out.println("Enter commands U(up),D(down),L(left),R(right);
//command = scan.next();                       
command = scan.next().charAt(0);

if(command == 'U'){
    "Do this"
}
else if(command == 'D'){
    "Do that"
}

当我使用此代码时,它只识别第一个字母,(在这种情况下,我理解charAt(0)表示第一个字母)。但是如何获得其他输入呢?我尝试使用String和char。

2 个答案:

答案 0 :(得分:2)

默认情况下,Scanner使用一个或多个空格作为分隔符,用于分隔标记,因此next返回表示整个单词的标记,而不是单个字符。

如果您希望next返回单个非空格字符,则可以将分隔符设置为

  • 正则表达式中的一系列空格(\s+,其中\需要在字符串中写为"\\"

  • 空字符串""

但是,由于zeroone or more of Xzero or more of X相同,而不是+,而正则表达式中的“{1}}表示”一次或多次出现“,我们可以使用{{1} }表示“零次或多次出现”。因此,代表我们的分隔符的正则表达式可能看起来像*(在字符串文字中需要写为\s+,因为"\\s+"需要转义)

演示:

\

根据您的评论,您似乎希望处理作为一个单词传递的命令组,例如Scanner scan = new Scanner(System.in); scan.useDelimiter("\\s*"); System.out.println("Enter commands U(up),D(down),L(left),R(right)"); String command = scan.next(); if (command.equalsIgnoreCase("U")) { System.out.println("Do this"); } else if (command.equalsIgnoreCase("D")) { System.out.println("Do that"); } else { System.out.println("unknown command: "+ command); } ,它应向上,向下,向上,向左和最后向右移动。在这种情况下,您可以组织代码,如:

UDULR

现在你可以添加一个方法,让我们根据我们传递给它的char选择使用哪个方法:

private void moveUp(){
    //code for moving up:
    System.out.println("moving up");
}

private void moveDown(){
    //... similar 
}
//rest of moving methods...

现在,在您的代码中,您应该可以使用以下内容:

public void move(char dirrection){
    switch(dirrection){
       case 'U' : moveUp(); break;
       case 'D' : moveDown(); break;
       case 'L' : moveLeft(); break;
       case 'R' : moveRight(); break;
       default: System.out.println("can't move in dirrection: "+dirrection);
    }
}

答案 1 :(得分:0)

您需要将public void call() { Toast.makeText(this,"call",Toast.LENGTH_SHORT).show(); Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel: 0211234567")); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, callIntent, PendingIntent.FLAG_UPDATE_CURRENT); Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle(getResources().getString(R.string.app_name)) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); notificationBuilder.setStyle(inboxStyle); inboxStyle.setBigContentTitle("sdjshfjnds"); inboxStyle.addLine("sdjjsdfn"); notificationBuilder.setStyle(inboxStyle); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); int NOTIFICATION_ID = 100; notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build()); } 的结果保存在变量中;然后你可以使用它以任何你想要的顺序获取其中的任何字符。

相关问题