0

这是非常基本的java,我正在努力使用n00b 风格。它只是打印出这个

请输入 '。' 当你想计算 1 2 3 。数字是 1 2 3 和是0积是1

当它应该计算这些连续数字的总和和乘积时。出了点问题,感谢任何帮助!

主要方法

    import java.util.*;

    public class NumberScanned {

        public static void main(String[] args) {

                System.out.println("Please enter '.' when you want to calculate");
            Scanner keyboard = new Scanner(System.in);

                String scannedString = keyboard.nextLine();
                Scanning scanz= new Scanning(scannedString);



     while(!keyboard.nextLine().equals("."))
     {
         scanz.set(scannedString);
     }

        keyboard.close();

        System.out.println("Numbers are"+scannedString);   

    scanz.printState();
        }
    }

Class Scanning


public class Scanning {
     int num;
      int sum;
      int product;
      String userInput;
            public Scanning(String userInput)
            {
                 num=0;
                 sum=0;
                  product=1;
            this.userInput=userInput;
            }
      public void set(String userInput)
      {
      for(int index=0; index<userInput.length(); index++)
            {

                if(Character.isDigit(userInput.charAt(index))==true)
                {

                num=userInput.charAt(index);


                sum+=num;
                product*=num;




                }
                else
                {


                    index++;
                }



            } 

      }

      public void printState()
      {
         System.out.println("The Sum is"+sum+"The Product is"+product); 
      }

}
4

3 回答 3

0

如果您只需要用户输入值的总和和乘积值,您可能会发现这很有帮助。

public class ProductSumCalculator{
private static List<Integer> numbers = new ArrayList<Integer>();

public static void main(String[] args){
    getInputs();
    calculateSumAndProduct();
}

private static void getInputs() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Please Enter numbers or ctrl+z to end inputs");
    while(scanner.hasNext()){
        numbers.add(scanner.nextInt());
    }       
}
private static void calculateSumAndProduct() {
    Iterator<Integer> iterator = numbers.iterator();
    int sum=0;
    int product=1;
    int nextVal;
    while(iterator.hasNext()){
        nextVal = iterator.next();
        sum+=nextVal;
        product*=nextVal;
    }
    System.out.println("Value entered are: "+numbers+".\nThe sum is "+
              sum+".The product    is "+product);
}
}
于 2013-08-29T04:21:39.743 回答
0

有几件事要看:

  • 我们知道keyboard.nextLine()从控制台获取输入,但是你在哪里检查它的有效性(更重要的是,你什么时候检查它?)。您是查看所有输入还是仅查看最后一行?
  • isDigit如果传入的字符是数字,则返回 true。您想对for循环中的数字或字符进行操作吗?
    • (附注,如果我在控制台中输入“1 10”会发生什么?)
  • for循环将在循环结束时自动增加其索引,因此不需要++额外的
于 2013-08-29T02:32:41.820 回答
0

你也可以试试这个。您可以从字符串行输入中计算所有 int 的总和和乘积,如下所示:

import java.util.Scanner;

public class Scanning {

    /*
     * This method returns the integer. If while
     * conversion an Exception is thrown it returns
     * null. Otherwise the integer.
     */
    public static Integer tryParse(String text) {
          try {
            return Integer.parseInt(text);
          } catch (NumberFormatException e) {
            return null;
          }
        }

    /*
     * Next String line is scanned. It is split by space. 
     * Stores String tokens in an String array from one String variable.
     * Then passed to tryParse() class method. null or auto Boxed Integer
     * is returned accordingly. It is auto unboxed from Integer
     * object to int variable. Then sum and product is calculated and
     * the final result is printed on the console Or Integrated
     * Development Environment.
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner keyboard = new Scanner(System.in);
        String strInts = keyboard.nextLine();

        String[] splits = strInts.split("\\s+");




        int i = 0;
        Integer anInteger = null;

        int total = 0;
        int product = 1;
        while((i < splits.length)) {

                anInteger = tryParse(splits[i]);



        if(anInteger != null) {
            total = total + anInteger;
            product = product * anInteger;
        }
        ++i;
        }
        System.out.println("The sum is: " + total);
        System.out.println("The product is: " + product);

    }

}
于 2020-04-28T17:54:34.217 回答