在java中十进制转换为二进制具有倒车订单的相关问题 [英] Converting decimal to binary in java having trouble with reversing order

查看:139
本文介绍了在java中十进制转换为二进制具有倒车订单的相关问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哎,所以我把我的第一个CS当然永远在我的大学其在java中任教。我在遇到麻烦十进制转换为二进制。我似乎能够得到正确的输出,但其以相反的顺序,我不知道如何把它放在正确的顺序,这里就是我codeD到目前为止,

 进口的java.util。*;
公共类{六硼化镧
公共静态无效的主要(字串[] args){
扫描仪输入=新的扫描仪(System.in);
   System.out.print(输入一个十进制数转换为二进制:);
   INT X = input.nextInt();
   诠释Ÿ;
   串Y1 =;
   而(X!= 0){
       Y = X%2;
       X = X / 2;
       Y1 = Integer.toString(Y);
       System.out.print(Y1 +);
     }
}
}


解决方案

看来你是显示仅在正确的顺序输出。但是如果你想扭转目前的输出,你可以使用下面code:

 扫描仪输入=新的扫描仪(System.in);
        System.out.print(输入一个十进制数转换为二进制:);
        INT X = input.nextInt();
        诠释Ÿ;
        串Y1 =;
        串反向=;
        而(X!= 0){
           Y = X%2;
           X = X / 2;
           Y1 = Integer.toString(Y);
         // System.out.print(Y1 +);
           反向= Y1 ++逆转;
         }
        的System.out.println(反向顺序:+反向);

Hey so I'm taking my first cs course ever at my university its taught in java. I'm having trouble converting decimal to binary. I seem to be able to get the correct output but its in reverse order, and I have no idea how to put it in the correct order, here is what I've coded so far,

import java.util.*;
public class lab6 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
   System.out.print("Enter a decimal number to convert to binary: ");
   int x = input.nextInt();
   int y;
   String y1="";
   while(x!=0){
       y=x%2;
       x=x/2;
       y1 = Integer.toString(y);
       System.out.print(y1+" ");
     }    
}
}

解决方案

It seems you are showing the output in correct order only. But if you want to reverse the current output you can use below code:

        Scanner input = new Scanner(System.in);
        System.out.print("Enter a decimal number to convert to binary: ");
        int x = input.nextInt();
        int y;
        String y1="";
        String reverse = "";
        while(x!=0){
           y=x%2;
           x=x/2;
           y1 = Integer.toString(y);
         //  System.out.print(y1+" ");
           reverse = y1+" "+reverse;
         }          
        System.out.println("Reverse Order :"+reverse);

这篇关于在java中十进制转换为二进制具有倒车订单的相关问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆