简体   繁体   English

output 不正确(交换数字的第一位和最后一位)

[英]output is not correct(Swapping the first and last digit of a number)

the output is almost correct except the first digit is not being removed. output 几乎是正确的,除了第一个数字没有被删除。 the code should swap the first and last digit of the number for example - if the input is 756 it should give 657 as output right now the code is showing 7657 the first digit is not being removed.例如,代码应该交换数字的第一个和最后一个数字 - 如果输入是 756,它应该给出 657 作为 output 现在代码显示 7657 第一个数字没有被删除。

package questionsOnLoops;
import java.lang.Math; 
import java.util.Scanner;

public class OSJIDS {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        Scanner srv = new Scanner(System.in);
        
        System.out.println("Enter any number: ");
        int n = srv.nextInt();
        
        int temp = n; //input number
        int c = 0;
        int f =n; //first digit
        int l; //last digit
        int result;
        
        while(temp>0) {
            temp = temp/10;
            c = c+1;
        }
        while(f>=10) {
            f = f/10;
        }
        g = n%10;
        result = (n/10)*10+f;
        result = (int) ((result%Math.pow(10,c))+(g*Math.pow(10,c)));
        System.out.println(result);
    }

}

Your code makes it quite hard to see what is exectly happening.您的代码很难看到到底发生了什么。

There are a few problems I see in your code (next to readability).我在您的代码中看到了一些问题(在可读性旁边)。 You were able to detect the first and the last number, but getting to the result seems to contain some mistakes您能够检测到第一个和最后一个数字,但获得结果似乎包含一些错误

  • result = (input/10)*10+f; here you take the input (506), you divide it by 10, multiply it by 10 to abuse the the int so that your fractions are removed and restored to the original number.在这里,您获取输入(506),将其除以 10,将其乘以 10 以滥用int 以便您的分数被删除并恢复为原始数字。 Then you append the first number again.然后你 append 再次第一个数字。

  • With the calculation you did above, you removed the last number, and added the first number as the last number.通过上面的计算,您删除了最后一个数字,并将第一个数字添加为最后一个数字。 You are still missing the step to add the first number.您仍然缺少添加第一个数字的步骤。 It would be easier and better to just create a new number based on your calculations根据您的计算创建一个新数字会更容易更好

Calculating the new number计算新数字

You already have most of the ingredients to calculate the new number.您已经拥有了计算新数字的大部分成分。 You already have the firstDigit and the last digit calculated.您已经计算了第一个数字和最后一个数字。 To get to the result we take the last number, and multiply it with 10 until it's in the correct position to be first.为了得到结果,我们取最后一个数字,然后乘以 10,直到它在正确的 position 中成为第一。 Next we append the first number, so that it's last in our new number finally, we add the inbetween numbers again.接下来我们 append 是第一个数字,所以它最后在我们的新数字中,我们再次添加中间数字。 (you can calculate the inbetween numbers by removing the first and last number from the original input) (您可以通过从原始输入中删除第一个和最后一个数字来计算中间数字)

I've updated your code example with the above mentioned changes.我已经使用上述更改更新了您的代码示例。 I've also gave the variables a bit more clear names to indicate what they contain我还给变量一个更清晰的名称来表明它们包含的内容

public static void main(String[] args) {
    Scanner srv = new Scanner(System.in);

    System.out.println("Enter any number: ");
    int input = srv.nextInt();        
    int temp = input;
    int length = 0;
    int firstDigit =input;
    int lastDigit;
    int inbetweenNumbers;

    while(temp>0) {
        temp = temp/10;
        length = length+1;
    }
    while(firstDigit>=10) {
        firstDigit = firstDigit/10;
    }
    lastDigit = input%10;

    inbetweenNumbers = input - lastDigit;
    inbetweenNumbers -= firstDigit * (int)Math.pow(10,length-1);

    //calculating the result
    int result = 0;
    result += lastDigit * (int)Math.pow(10,length-1);
    //Add last number (first digit of the input)
    result += firstDigit;
    //add the other numbers back
    result+= inbetweenNumbers;
    
    System.out.println(result+" "+length);
}

Let's use your example input, 756.让我们使用您的示例输入 756。

This gets rid of the last digit, 6, and instead puts the first digit there:这去掉了最后一个数字 6,而是把第一个数字放在那里:

        result = (n/10)*10+f;

So now result is 757. Next you try to put the 6 where the first 7 is:所以现在result是 757。接下来你试着把 6 放在前 7 的位置:

        k = (int) ((result%Math.pow(10,c))+(g*Math.pow(10,c)));

This isn't quite correct.这不太正确。 c is 3, the number of digits. c为 3,位数。 So Math.pow(10,c) equals 1000.0.所以Math.pow(10,c)等于 1000.0。 But you needed to use 100 in those two places, first to get rid of the 7 and then to add the 6 there.但是你需要在这两个地方使用 100,首先去掉7 ,然后在那里添加6 Try subtracting 1 from c in the expression.尝试从表达式中的c中减去 1。

My suggestions for more readable code我对更具可读性的代码的建议

  • Don't declare a variable until you use it;在使用之前不要声明变量; inirialize each in the declaration.在声明中初始化每个。
  • Use better variable names.使用更好的变量名。 For example:例如:
    • input rather than n input而不是n
    • length or numberOfDigits instead of c lengthnumberOfDigits而不是c
    • firstDigit instead of f firstDigit而不是f
    • lastDigit instead of g lastDigit而不是g
    • lastDigitReplaced instead of result lastDigitReplaced而不是result
    • finalResult instead of k finalResult而不是k

I do recommend making your variables a bit more descriptive, but that is an easy fix.我确实建议让您的变量更具描述性,但这是一个简单的解决方法。 I am guessing from your title that you want to swap the first and last digits?我从你的标题猜想你想交换第一个和最后一个数字? This method converts the numbers to strings, making appends and insertions relatively easier in this case.此方法将数字转换为字符串,在这种情况下使追加和插入相对容易。

import java.util.*;
import java.io.*;

class Main {
    public static void main(String[] args) {
        Scanner srv = new Scanner(System.in);
    
        System.out.println("Enter any number: ");
        int num = srv.nextInt();
        int lastDig = num;
        int firstDig = num%10;

        int len = 0;
        while(lastDig>9) {
            lastDig/=10;
            len++;
        }
    
        String ans = Integer.toString(num).substring(1, len);
        ans = firstDig+ans+lastDig;
        System.out.println(ans);
            
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM