簡體   English   中英

如何獲得第 1 步最右邊的數字? 那么我如何獲得步驟 2 中未包含的數字?

[英]How do I get the rightmost digits for step 1? Then how do I get the digits that weren't included for step 2?

import java.util.Scanner;

public class Ass. {

public static void main(String[] args) {
    /*blanca 10/08/20
     * 3.Following is a fun algorithm of three steps to check whether a given 8 digit number is acceptable.
     * step1: starting from the rightmost digit,form the sum of every other digit. 
     * - For example the number is 12345658, this sum is 8+6+4+2 = 20.
     * step2: double each of the digits that were not included in the preceding step; add all digits of the resulting number. 
     * - For the example number above,doubled digits would be 10,10,6,2. Adding those digits will yield (1+0+1+0+6+2)10.
     * step3: Add the sum of the numbers in step1 and step2. if the last digit of that number is 0, the number is acceptable, not otherwise.
     * Your program should read an 8 digit number and output whether it is acceptable or not.
     */
    //declare
    int sum1 = 0;
    int sum2 = 0;
    int sum3 = 0;
    int digits = 0;
        
    Scanner data = new Scanner(System.in);
    System.out.println("Enter a card number");
    digits = data.nextInt();
    //step 1 
    sum1 = digits%10;
    // step 2
    sum2 = digits%100;
    //step 3 
    sum3 = sum1 + sum2;
    if (sum3 = 0){
        System.out.println("Acceptable");
    }
    else 
    {
        System.out.println("Unacceptable");
        }

我看到了其他一些問題,看到位值(如 %10)應該給出數字,但我不知道如何根據問題更改它?

這里有一些建議。

  1. 要獲得最低有效數字,您可以使用余數運算符 (%) 和 10。
  2. 要獲得下一位數字,請將原始數字除以 10,然后將余數運算符應用於商。
  3. 對於每個其他數字,您需要在將數字乘以 2 后應用余數運算符和除法運算符。因此 14 將是 1 + 4,您應該能夠像上面那樣計算出來。
  4. 您可以繼續此操作,直到您的起始數字為 0。或者循環重復 4 次,每次循環獲取兩位數字並執行適當的求和操作。
  5. 完成后,您只需測試所需值的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM