在Java里输入一个未知数,要求把那个未知数加入总数里,那么要怎样写啊?谢谢!

解决方案 »

  1.   


    public class Test {   
      public static void main(String [] args){
      Scanner sc=new Scanner(System.in);
      int sum=0;
      while(sc.hasNextInt()){
      sum+=sc.nextInt();
      System.out.println(sum);
      }
          sc.close();
      } } 
      

  2.   

    未知数?是int型?说说具体的
      

  3.   

    这个?大概可以这样吧?public static void main(String[] args) {
      int sum = 0;
      Scanner in = new Scanner(System.in);
      while(in.hasNext()) {
        temp = in.nextInt();
        sum += temp;
      }
    }
    还有些细节Lz自己注意吧
      

  4.   

    if ( cleaningJCheckBox.isSelected() )
             {
                total += 35; // add 35 to total
             }
             
             // if patient had cavity filled
             if ( cavityFillingJCheckBox.isSelected() )
             {
                total += 150; // add 150 to total
             }
             
             // if patient had x-ray taken
             if ( xRayJCheckBox.isSelected() )
             {
                total += 85; // add 85 to total
             }
             
             //if patient had fluoride
             {
              total += 50; // add 50 to total
             }
             
             //if patient had rootCanal
             {
              total += 225; // add 225 to total
             }
             
             //if patient had other
             {
              total +=
             }
    一个有选择的牙医服务,但是不知道怎样加自己输入的other价钱!
      

  5.   

    不是很明白LZ要表达什么意思,不过应该很简单~
    如果是从JTextField中获取的话:
    total += Integer.parseInt(textField.getText());
      

  6.   


    public class Test {    /**
         * 判断字符串是否是数字组成【0-9】
         * 
         * @return boolean
         * @param number
         * @author kakukyowu
         */
        public static boolean isNumber(String number) {        int count = 0;
            for (int i = 0; i < number.length(); i++) {
                if (String.valueOf(number.charAt(i)).matches("\\d"))
                    count++;
            }
            if (count == number.length())
                return true;
            else
                return false;
        }    /**
         * main函数
         * 
         * @param args
         * 
         * @author kakukyowu
         */
        public static void main(String[] args) {        int sum = 0;        while (true) {
                System.out.println("请输入查询内容:");
                Scanner sc = new Scanner(System.in);
                String inputStr = sc.nextLine();            if (isNumber(inputStr)) {
                    sum += Integer.parseInt(inputStr);
                    System.out.println('\n' + "SUM: " + sum);
                } else {
                    System.out.println("请输入正确的数字");
                }
            }
        }
    }