gpt4 book ai didi

java - 二进制到十进制转换和 while 语句的算法

转载 作者:行者123 更新时间:2023-12-01 14:02:26 25 4
gpt4 key购买 nike

import java.util.Scanner;

public class Name {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

String A= "10", B = "11", C = "12", D = "13", E = "14" , F = "15";

System.out.println("Enter your nubmer/letter: ");
char number = input.next().charAt(0);

System.out.println("Enter your number system: ");
char system = input.next().charAt(0);

//while( system == 2) {
//if ( number % 2 != 0)
//}
//I want this to be an algorithm to convert binary
//into decimal, but I don't know how it can add
//the constant changes in the power of 2

if ( system == '10') {
System.out.println( "Your decimal number is" + number );
} else if (system == '16') {
System.out.println( "Your decimal number is" + number );
} else {
System.out.println(" Error ");
}
}
}

我已经尝试过,但它会自动进入错误。有人能告诉我我做错了什么吗?我正在尝试将二进制或十六进制数字转换为十进制。

最佳答案

我认为您的系统仅接受一个字符。您需要接受多个才能获得“10”或“16”:

System.out.println("Enter your number system: ");
String system = input.nextLine();

关于java - 二进制到十进制转换和 while 语句的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19254628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com