开发者

Reverse number after inputting an integer and doubling it

开发者 https://www.devze.com 2023-04-12 05:12 出处:网络
the directions are the following: input an integer, then double integer value, then call your static void reverse( long i ) method. The method void reverse( long i) displays the 开发者_如何学编程integ

the directions are the following: input an integer, then double integer value, then call your static void reverse( long i ) method. The method void reverse( long i) displays the 开发者_如何学编程integer in reverse terminated by a new line.

I need help in reversing the integer. I think I have the rest of the code correctly. But, correct me if I'm wrong.

    import java.util.Scanner;

public class Exercise5_3M {
    public static void reverse(String[] args) {
    Scanner input = new Scanner(System.in);

    //input an integer
    int num= input.nextInt();
    int result=0;

     //Double the intger
    while(num>0){
    result= num *2;

        }


System.out.print(result);
    }
}


at first of all you need void reverse( long i) instead void reverse(String[] args) at second

while(num>0){
    result= num *2;
}

never end, because num never change


Your method should be called "main" when you want to execute it on start. Second you created an infinity loop with your while when sb enters a positive number. A negative always returns 0. Third you have no method called reverse(long i). Fourth boundary check. Are only positive numbers allowed or negative also?

For a tip: You can reverse it in multiple ways. Convert your number to a string or char array and reverse it. Or you want to have a look at the mod operator "%".

0

精彩评论

暂无评论...
验证码 换一张
取 消