이것은 간단한 계산기 프로그램입니다. 내 배열을 검사하고 내 프로그램이 입력 된 두 개의 인수를 계속 추가하기 전에 그 안에있는 문자를 막을 수있는 무언가가 필요합니다. 입력은 명령 행에서 가져옵니다. java adder 1 2
배열에 글자가 입력되지 않았는지 어떻게 확인할 수 있습니까?
public class Adder {
public static void main(String[] args) {
//Array to hold the two inputted numbers
float[] num = new float[2];
//Sum of the array [2] will be stored in answer
float answer = 0;
/*
some how need to check the type of agruments entered...
*/
//If more than two agruments are entered, the error message will be shown
if (args.length > 2 || args.length < 2){
System.out.println("ERROR: enter only two numbers not more not less");
}
else{
//Loop to add all of the values in the array num
for (int i = 0; i < args.length; i++){
num[i] = Float.parseFloat(args[i]);
//adding the values in the array and storing in answer
answer += Float.parseFloat(args[i]);
}
System.out.println(num[0]+" + "+num[1]+" = "+answer);
}
}
}
문제가 아니라,하지만 당신은 직접'처음에 경우'문 등이 조건을 작성할 수 있습니다!'args.length을 = 2' 대신 쓸데없이 중복 이중 검사를 사용하여 지금 사용하고 있습니다. – sjaustirni
정규식을 사용할 수 있습니다. 자세한 내용은 http://www.vogella.com/tutorials/JavaRegularExpressions/article.html –
@Dundee args.length이어야합니다. = 2 – PeerNet