日期:2014-05-20  浏览次数:20761 次

java数组
package che;

public class Che {

int[] xinghao = new int[3]; // 车的型号
String[] yanse = new String[3]; // 车的颜色
String[] mingcheng = new String[3]; // 车的名称
double[] jiage = new double[3]; // 车的价格

public void fangfa() {
xinghao[0] = 1; //型号0  
yanse[0] = "红色";
mingcheng[0] = "法拉利";
jiage[0] = 3000000;

xinghao[1] = 2; // 型号1
yanse[1] = "黄色";
mingcheng[1] = "兰博基尼";
jiage[1] = 4000000;

xinghao[2] = 3; //型号2
yanse[2] = "黑色";
mingcheng[2] = "布加迪威龙";
jiage[2] = 5000000;

}

}




package che;

import java.util.Scanner;



public class Guocheng {
Che s = new Che(); //创建一个车类对象

//加载数据
public void hehe() {
s.fangfa();
}

//显示车信息
public void zhu() {
String con = "";
boolean sh = false;
do {
Scanner in = new Scanner(System.in);
System.out.print("请输入车的型号(0,1,2):");
do {
while (!in.hasNextDouble()) {
System.out.println("型号里不含有字母和符号!,请输入重新的型号:");
in.nextLine();
}
int hao = in.nextInt();
if (hao < s.xinghao.length && hao >= 0) {
System.out.println("颜色:" + s.yanse[hao]);
System.out.println("名称:" + s.mingcheng[hao]);
System.out.println("价格:" + s.jiage[hao]);
break;
} else {
System.out.print("你输入车的型号不对!请重新输入:");
sh = true;
}
} while (sh);
System.out.print("是否继续输入型号(y/n):");
con = in.next();
} while (con.equals("y"));
System.out.print("程序结束!");
}

}


package che;



public class Test {

/**
* @param args
*/
public static void main(String[] args) {
Guocheng zxf=new Guocheng();
zxf.hehe(); // 加载数据
zxf.zhu();

}

}


当输入9后,再输入字母是就会出现两次:型号里不含有字母和符号!,请输入重新的型号:
该怎么办啊?

------解决方案--------------------
不要用in.nextLine(),用in.next()试试