有不等于char的地方吗 [英] is there a not equals for char

查看:66
本文介绍了有不等于char的地方吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将其添加到此代码中

I''m trying to add it to this code

char input;
String state = in.next();
 input = state.charAt(0);
 do
 {
     System.out.println("Please enter a command type:");
 }while(!input);

推荐答案

没有,没有.但是,如果字符串input,则charAt方法将引发IndexOutOfBoundsException(请参见
Nope, there is not. However, if the string input is empty then the charAt method throws the IndexOutOfBoundsException (see the documentation[^]).
What are you trying to do?


尝试这个
Scanner in=new Scanner(System.in);
		char ch ;
		do{
			System.out.println("Enter the a character");
		try{
			ch= in.nextLine().charAt(0);
			System.out.println("Char: "+ch);			
		}
		catch (IndexOutOfBoundsException e) {
		      ch = 'z';
		}			
		}while(ch < 'a' || ch > 'g');


这篇关于有不等于char的地方吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆