Scanner input = new Scanner(System.in);
boolean jiesuan=true;
do {
System.out.println("请输入实际交付金额 ¥");
try {
payment = input.nextInt();
} catch (Exception e) {
System.out.println("让你输入的是数字,你输入的是什么,重新输入\n");
continue;
}
returnMoney = payment - shiJi;
if (returnMoney > 0) {
System.out.println("找钱 ¥" + returnMoney);
} else if (returnMoney == 0) {
System.out.println("谢谢惠顾!");
} else {
System.out.println("请支付剩余的金额" + returnMoney * -1 + "元,谢谢惠顾\n");
}
break;
} while (jiesuan);
}这个地方有问题,假如对方输入的是字符类型,那就会直接catch,而且进入死循环,大家帮我改改,谢谢了,我新人************************************************************************************
以下是全部代码:import java.util.*;public class JieSuan { /**
 * @param args
 */
static int number; // 购买商品编号 static int count; // 购买数量 static double price1 = 80, price2 = 240, price3 = 160; // 单价 static double total = 0; // 所有物品总价 static double tXue = 0, wangQiuXie = 0, wangQiuPai = 0; // 总价 static double returnMoney = 0, payment = 0; // 找钱,实际支付 static double discount = 0.8; // 折扣 static double shiJi = 0; // 实际金额 static String answer; static boolean isError = false; static public void main(String[] args) {
shop1();
} public static void shop1() { // TODO Auto-generated method stub
System.out.println("我行我素购物管理系统 > 购物结算\n");
do {
try {
System.out
.println("********************************************************************************");
System.out.println("请选择购买的商品编号:\n");
System.out.println("1.T恤 ¥80     2.网球鞋 ¥240     3.网球拍 ¥160\n");
System.out
.println("********************************************************************************\n"); Scanner input = new Scanner(System.in);
boolean isErrorId = false;
do {
System.out.println("请输入商品的编号:\n");
number = input.nextInt();
break;
} while (isErrorId);
if (checks(new int[] { 1, 2, 3 }, number)) {
System.out.println("就1到3而已,有必要乱写吗,重新输入\n");
answer = "y";
continue;
} else { }
boolean isMax = false;
System.out.println("请输入购买数量,最大9999:\n");
do {
isMax = false;
count = input.nextInt();
if (9999 < count) {
System.out.println("没那么多货,最高9999,重新输入\n");
isMax = true;
}
} while (isMax);
switch (number) {
case 1:
tXue = price1 * count;
System.out.println("T恤    单价¥" + price1 + "   总价¥" + tXue); break;
case 2:
wangQiuXie = price2 * count;
System.out.println("网球鞋   单价¥" + price2 + "    总价¥"
+ wangQiuXie); break;
case 3:
wangQiuPai = price3 * count;
System.out.println("网球拍    单价¥" + price3 + "    总价¥"
+ wangQiuPai); break;
}
do {
System.out.print("\n还要购买其他物品吗?是/否(y/n)\n");
answer = input.next();
if ("y".equals(answer) || "n".equals(answer)) {
isError = false;
} else {
System.out.println("输入错误,只能输入y或n,请重新输入\n");
isError = true;
}
} while (isError); } catch (InputMismatchException e) {
System.out.println("输入错误,眼睛看清楚,让你输入数值而已\n");
answer="y";
}
} while (answer.equals("y"));
total = tXue + wangQiuXie + wangQiuPai;
shiJi = total * discount; System.out.println("折扣:" + discount);
System.out.println("金额总计 ¥" + shiJi); Scanner input = new Scanner(System.in);
boolean jiesuan=true;
do {
System.out.println("请输入实际交付金额 ¥");
try {
payment = input.nextInt();
} catch (Exception e) {
System.out.println("让你输入的是数字,你输入的是什么,重新输入\n");
continue;
}
returnMoney = payment - shiJi;
if (returnMoney > 0) {
System.out.println("找钱 ¥" + returnMoney);
} else if (returnMoney == 0) {
System.out.println("谢谢惠顾!");
} else {
System.out.println("请支付剩余的金额" + returnMoney * -1 + "元,谢谢惠顾\n");
}
break;
} while (jiesuan);
} /*catch (Exception e) {

payment = 0;
jiesuan = true;
}*/
public static boolean checks(int[] shuZi, int selected) {
int i = 0;
while (i < shuZi.length) {
if (shuZi[i] == selected){
return false;
}
i++;
}
return true;
}
}

解决方案 »

  1.   

    我重新缩减了下,大家重新看看这个问题。
    import java.util.*;public class Test2 { /**
     * @param args
     */
    static double payment;
    static boolean jiesuan=true;
    public static void main(String[] args) {
    // TODO 自动生成方法存根
    Scanner input=new Scanner(System.in);
    do {
    System.out.println("请输入实际交付金额 ¥");
    try {
    payment = input.nextInt();
    break;
    } catch (Exception e) {
    System.out.println("让你输入的是数字,你输入的是什么,重新输入\n");
    continue;
    }
    } while (jiesuan);
    }}
    这个简单了吧,一旦输入字符,就进入死循环,为什么呢?
      

  2.   

    改为这样吧
    public class DemoClass {
    /** 
     * @param args 
     */ 
    static double payment; 
    static boolean jiesuan=true; 
    public static void main(String[] args) { 
    Scanner input=new Scanner(System.in); 
    do { 
    System.out.println("请输入实际交付金额 ¥"); 
    try { 
    //payment = input.nextInt(); 
    String s = input.nextLine();
    payment = Integer.valueOf(s);
    break; 
    } catch (Exception e) { 
    System.out.println("让你输入的是数字,你输入的是什么,重新输入\n"); 
    continue; 

    } while (jiesuan); 

    }
      

  3.   

    在catch中加一句input.next();就可以了。
      

  4.   

    你好像只要求他循环一次,所以没有必要加上do...while...
    public class DemoClass {
        /** 
         * @param args 
         */ 
        static double payment; 
        static boolean jiesuan=true; 
        public static void main(String[] args) { 
        Scanner input=new Scanner(System.in);         System.out.println("请输入实际交付金额 ¥"); 
            try { 
                //payment = input.nextInt(); 
                String s = input.nextLine();
                payment = Integer.valueOf(s);
                break; 
            } catch (Exception e) { 
                System.out.println("让你输入的是数字,你输入的是什么,重新输入\n"); 
                continue; 
            } 
          
        } 
    }
    这样就可以了
      

  5.   


    的确是这样的 但是为什么呢  有没有人解答一下 看了半天Scanner类 也不懂