使用Loop结构的Java Simple程序

时间:2016-05-22 00:48:20

标签: java loops structure

示例输入:3456 样本输出: 数字:3,4,5,6 总和:18

这是我尝试过的代码,但遗憾的是,由于我不使用循环,这是错误的。有人可以帮助我吗?

import java.util.Scanner;

public class Lab1_5 {
    public static void main (String args[])
    {
        int insert1, insert2, insert3, insert4;
        int sum ;

        Scanner console = new Scanner(System.in);

         System.out.print("Please enter First Number: ");
         insert1 =console.nextInt();
         System.out.print("Please enter Second Number: ");
         insert2 =console.nextInt();
         System.out.print("Please enter Third Number: ");
         insert3 =console.nextInt();
         System.out.print("Please enter Fourth Number: ");
         insert4 =console.nextInt();

         System.out.println("Digits: "+ insert1+","+insert2+","+insert3+","+insert4);

         sum = insert1+insert2+insert3+insert4;
         System.out.print("Sum: "+ sum);

    }

}

3 个答案:

答案 0 :(得分:0)

以下是如何通过循环获得总和:

这样做是从用户那里得到一个号码并循环到号码的各个数字。

$itemid = $data2['fields']['CALC STOCK NO'];  
$pdesc = $data2['fields']['Item Description'];

mysqli_query($con,
    "INSERT INTO  600XXX  
    (CALC_STOCK_NO, pdesc) VALUES
    ($itemid, $pdesc)"
);

这是另一个版本,它不会为Integer.MAX_VALUE上的输入值提供整数溢出。

public static void main(String[] args)
{
    int sum = 0;

    Scanner console = new Scanner(System.in);

    System.out.print("Please enter a Number: ");
    String num = console.nextLine();

    try
    {
        num = num.trim();
        int index = 0;
        int n = Integer.parseInt(num);

        System.out.print("Digits: ");
        while (n > 0)
        {
            int digit = n % 10;
            sum += n % 10;
            n = n /10;

            char d = num.charAt(index++);

            System.out.print(d + ", ");
        }

        System.out.print("Sum: " + sum);
    }
    catch (NumberFormatException e)
    {
        System.out.print("Invalid Number entered");
    }   

    // Close the scanner
    console.close();
}

答案 1 :(得分:0)

您可以使用此示例中显示的for loop

public static void main(String args[]){
  int sum = 0;
  Scanner sc = new Scanner(System.in);

  System.out.println("Number: ");
  //get number input:
  int num = sc.nextInt(); 

  //convert number to String:
  String str = Integer.toString(num);

  //iterate through each char in string:
  for(int i = 0; i < str.length(); i++){
    //convert char value to int, and add it to the sum:
    sum += Character.getNumericValue(str.charAt(i));
  }
}

答案 2 :(得分:0)

public class Lab1_5 {
    public static void main (String args[])
    {
        int insert;
        int sum ;
        int[] numArray = new int[4];
        Scanner console = new Scanner(System.in);

         for(int i=0; int<4; i++){
             if(i == 1) {
                 System.out.println("Please enter First Number: ");
             } else {
                 System.out.println("Please enter the next Number: ");
             }
             numArray[i] = console.nextInt();
             sum += numArray[i];
         }

         System.out.println("Digits: "+ numArray[0]+","+numArray[1]+","+numArray[2]+","+numArray[3]);

         System.out.println("Sum: "+ sum);

    }

}