【发布时间】:2015-01-06 17:40:39
【问题描述】:
我尝试编译我的代码,但我的最后一个代码出现“不兼容错误”(否则)。我试图在该代码中说,“如果输入是数字和字母的混合,则只返回数字”,但它告诉我我所做的事情有问题,我无法弄清楚。
import java.util.*;
public class Pr8{
public static void main(String[] args){
Scanner scan = new Scanner (System.in);
//Prompt the user for how many numbers are going to be entered
System.out.print("* Please write how many numbers are going to be entered: ");
if (!scan.hasNextInt())
System.out.println("- Sorry your entery was not correct. The compiler except's digits only.");
else {
int a = scan.nextInt(); //a is the scanned number of the user request
int[] n = new int[a]; //is an array to declare a variable as much as the user entered
int num = 1; //to show the sorting number of the string when printing
//prompt the user to enter a mixture of digits and letters
for (int i = 0; i < a; i++){
System.out.print("* Please enter a string #" + num++ + ": ");
if (scan.hasNextInt()){ //check if the input has only integers
n[i] = scan.nextInt();
System.out.println("- " + n[i] + " = " + n[i]);
}//if
else if (!scan.hasNextInt()){ //if the input was a mixture of digits and letters, return only the digits
n[i] = scan.nextLine();
System.out.println("- there is letters");
}//else
}//for
}//else if
}//main
}//Pr8
【问题讨论】:
标签: java incompatibility