import java.util.Scanner;public class xixix { public static void main(String[] args) { System.out.println("*******************************"); System.out.println("请选择购买的商品编号:"+"\n"+"1.T桖\t2.网球鞋\t3.网球拍"); System.out.println("*******************************"); String name=""; double price=0.0; int num=0; double sum=0; String answer="y"; double total=0.0; double playment=0.0; double zheK=0.8; int num1=0; Scanner input =new Scanner(System.in); while("y".equals(answer)){ System.out.println("请输入商品编号:"); while (!input.hasNextInt()){ System.out.println("请输入正确的商品编号!"); return ; } num=input.nextInt(); System.out.println("请输入购买数量:"); if(!input.hasNextInt()){ System.out.println("请输入正确数字!"); return; } num1=input.nextInt(); switch (num) { case 1: name="T桖"; price=245.0; sum=price*num1; break; case 2: name="网球鞋"; price=245.0; sum=price*num1; break; case 3: name="网球拍"; price=245.0; sum=price*num1; break; } System.out.println(name+"\t"+"¥:"+"数量:"+num1+"\t"+price+"合计:"+sum); System.out.print("是否继续(y/n)"); answer=input.next(); total+=sum; if("n".equals(answer)){ System.out.println("折扣是:"+zheK); System.out.println("应付金额:"+total*0.8); System.out.print("实付金额:"); playment=input.nextDouble(); System.out.print("找钱:"+(playment-total*0.8)); } } }}
调用我这个方法 如果返回false 说明不是数字public static boolean isNumeric(String str){ for(int i=str.length();--i>=0;){ int chr=str.charAt(i); if(chr<48 || chr>57) return false; } return true;}