📜  Java程序使用do-while循环反转数字并找到其数字之和

📅  最后修改于: 2022-05-13 01:55:46.262000             🧑  作者: Mango

Java程序使用do-while循环反转数字并找到其数字之和

问题陈述:该数字应该由用户输入,无论是位于保存该数字的原始数据类型内的任何随机数。首先,数字需要颠倒。其次,要使用 do-while 循环来计算数量的总和。

do-while 循环现在用户有时会在 while 和 do-while 循环之间感到困惑。 While 循环先检查条件,然后执行语句,而 Do-while 循环先执行语句,然后检查条件。 Do-while 产生的一个重要点是,即使条件失败,它仍然会被执行一次。

Java中的while循环

do-While 循环的语法:

do 
{
 // statement to be executed
}
while(condition check)

Do-while 循环的使用:

向用户显示一些菜单,换句话说,游戏的实现目标是向用户显示按 1 执行此操作,按 2 执行此操作,依此类推,如果有选项,则按 Q 退出此游戏。所以 do-while 循环希望至少向用户显示一次菜单。当用户采取行动时,适当的步骤就完成了。因此,虽然条件应该是用户按 Q 退出并且菜单在 do-while 循环中。

方法:使用 do-while 循环,求一个数的倒数以及它的数字之和。输入任何数字作为输入,然后使用模数和除法,运算符来反转该特定数字并找到其数字之和。

算法:

  1. 从用户那里取一个号码
  2. 创建两个变量,即 reverse_number 和 sum 并将它们都初始化为 0
  3. 反转数字
  4. 打印倒号
  5. 打印从较小的总和中获得的数字的最终总和。

反转算法讨论如下:

  • 将 rev 乘以 10,并将作为余数的数字的余数添加到 reverseNumber。 // rem = num%10 ; rev = rev*10 + rem;
  • 将 rem 添加到当前总和以查找数字总和并将它们存储在一个变量中,该变量将返回保存最终总和。 Final_sum=current_sum 其中 current_sum=current_sum + 余数
  • 将数字除以 10 以访问数字当前数字之前的数字,如下所示。 // num=num/10;而(数量> 0);

例1: Java程序不做函数就实现数位取反求和

Java
// Java program to reverse and and sum up digits of number
 
// Importing generic Classes/Files
import java.io.*;
// Importing Scanner Class
import java.util.Scanner;
 
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // creating vaiables
        int num, rem;
 
        // Creating variables and initializing at same time
        int rev = 0, sum = 0;
 
        // Using scanner to take input from user
        // Scanner sc = new Scanner(System.in);
 
        // Remove comments from lineNo20
        // to take dynamic user input
 
        // Displaying message
        System.out.println("Enter the number: 25 ");
 
        // Taking input from user
        // num = sc.nextInt();
 
        // Hard coded input
        num = 25;
 
        // Do-while loop for iteration over digits of number
        do {
 
            // Step1: Modulo with 10 to get last digit
            rem = num % 10;
 
            // Step2: Reverse the number
            rev = rev * 10 + rem;
 
            // Sum of the digits of number
            sum = sum + rem;
 
            // Step3: Dividing number by 10 to loose last
            // digit
            num = num / 10;
        }
 
        // Condition check
        // Remember: By this time 1 iteration is over even
        // if conditions false
        while (num > 0);
 
        // Printing the reverse number
        System.out.println("Reverse of given number: "
                           + rev);
 
        // Summing up digits of number as shown in above
        // steps
        System.out.println("Sum of digits of given number: "
                           + sum);
    }
}


Java
import java.io.*;
 
// Importing specific Scanner Class to show menu
import java.util.Scanner;
 
class GFG {
 
    // Iterative function to reverse digits of number
    // static int reversDigits(int num)
 
    // Hardcoded input where input number equals 25
    static int reversDigits(int num)
    {
 
        // Making input hard coded else
        num = 25;
        // else do not initialize num
 
        // creating and Initialising reverseNo with 0
        // Creating remainder variable
        int rev = 0, rem;
 
        // Statements to be executed in do loop
        do {
            // Reversal of a number as discuused in
            // algorithm
            rem = num % 10;
            rev = rev * 10 + rem;
            num = num / 10;
 
        }
 
        // Condition check
        while (num > 0);
 
        // Returning reverse of the user enter number
        return rev;
    }
 
    // Iterative function findingsum of the digits of number
    // static int sumDigits(int num)
 
    // Hardcoded input where input number equals 25
    static int sumDigits(int num)
    {
        // Making input hard coded
        num = 25;
        // else do not initialize num
 
        // creating and Initialising final_sum with 0
        // Creating remainder variable
        int sum = 0, rem;
 
        // Statements to be executed in do loop
        do {
            // Retrieving steps as discussed in above 3
            // steps
            rem = num % 10;
            sum = sum + rem;
            num = num / 10;
 
        }
        // condition check
        while (num > 0);
 
        // Returning Sum of digits of a reversed number
        return sum;
    }
 
    // Main driver method
    public static void main(String[] args)
    {
        // declaring variable to store user entered number
        // int num;
 
        // Scanner Class to read the entered number
 
        // Commented out to hard code the input
        // else remove comments
        // Scanner sc = new Scanner(System.in);
 
        // Input is hardcoded for display
        // else remove comments from line no 77
 
        // Considering hard coded input
        int num = 25;
 
        // Printing message
        System.out.println(num);
 
        // Taking input from user
        // Making input hard coded else
        // remove comments from line no 82
        // num = sc.nextInt();
 
        // Calling above functions to print reversed number
        System.out.println("Reverse of given number: "
                           + reversDigits(num));
 
        // Calling above functions to print reversed number
        System.out.println("Sum of digits of given number: "
                           + sumDigits(num));
    }
}


输出
Enter the number: 25 
Reverse of given number: 52
Sum of digits of given number: 7

示例 2:在此示例中,通过创建它们的函数以及稍后在主驱动程序方法中调用它们来分别显示 do-while 循环作为说明。

Java

import java.io.*;
 
// Importing specific Scanner Class to show menu
import java.util.Scanner;
 
class GFG {
 
    // Iterative function to reverse digits of number
    // static int reversDigits(int num)
 
    // Hardcoded input where input number equals 25
    static int reversDigits(int num)
    {
 
        // Making input hard coded else
        num = 25;
        // else do not initialize num
 
        // creating and Initialising reverseNo with 0
        // Creating remainder variable
        int rev = 0, rem;
 
        // Statements to be executed in do loop
        do {
            // Reversal of a number as discuused in
            // algorithm
            rem = num % 10;
            rev = rev * 10 + rem;
            num = num / 10;
 
        }
 
        // Condition check
        while (num > 0);
 
        // Returning reverse of the user enter number
        return rev;
    }
 
    // Iterative function findingsum of the digits of number
    // static int sumDigits(int num)
 
    // Hardcoded input where input number equals 25
    static int sumDigits(int num)
    {
        // Making input hard coded
        num = 25;
        // else do not initialize num
 
        // creating and Initialising final_sum with 0
        // Creating remainder variable
        int sum = 0, rem;
 
        // Statements to be executed in do loop
        do {
            // Retrieving steps as discussed in above 3
            // steps
            rem = num % 10;
            sum = sum + rem;
            num = num / 10;
 
        }
        // condition check
        while (num > 0);
 
        // Returning Sum of digits of a reversed number
        return sum;
    }
 
    // Main driver method
    public static void main(String[] args)
    {
        // declaring variable to store user entered number
        // int num;
 
        // Scanner Class to read the entered number
 
        // Commented out to hard code the input
        // else remove comments
        // Scanner sc = new Scanner(System.in);
 
        // Input is hardcoded for display
        // else remove comments from line no 77
 
        // Considering hard coded input
        int num = 25;
 
        // Printing message
        System.out.println(num);
 
        // Taking input from user
        // Making input hard coded else
        // remove comments from line no 82
        // num = sc.nextInt();
 
        // Calling above functions to print reversed number
        System.out.println("Reverse of given number: "
                           + reversDigits(num));
 
        // Calling above functions to print reversed number
        System.out.println("Sum of digits of given number: "
                           + sumDigits(num));
    }
}
输出
25
Reverse of given number: 52
Sum of digits of given number: 7