【问题标题】:Is there a way of reducing the number of "if" statements I use?有没有办法减少我使用的“if”语句的数量?
【发布时间】:2016-01-04 21:36:41
【问题描述】:

对于课程,我正在创建一个二进制到二进制转换器,但我似乎无法让它工作。我对ifelse if 语句的使用似乎使我的代码非常混乱。

有没有办法减少杂乱的数量?该任务表示调查:.toString.substring(int a, int b)java.lang.Math.pow(double a, double b) 会很有用。无论如何也要让它工作?所有的值,因为它们最初被定义为 0,所以最终都为 0。

import java.util.Scanner;


public static void main(String[] args) {

Scanner userInput = new Scanner(System.in);    
    String binary;
    int value0 = 0;
    int value1 = 0;
    int value2 = 0;
    int value3 = 0;
    int value4 = 0;
    int value5 = 0;
    int value6 = 0;
    int value7 = 0;

    System.out.println("Welcome to the Binary to Denary converter!");
    System.out.println("Please enter an 8-BIT Binary value:");

    binary = userInput.next();
    char result0 = binary.charAt(0);
    char result1 = binary.charAt(1);
    char result2 = binary.charAt(2);
    char result3 = binary.charAt(3);
    char result4 = binary.charAt(4);
    char result5 = binary.charAt(5);
    char result6 = binary.charAt(6);
    char result7 = binary.charAt(7);

    if (result0 == 1){
    value0 = 128;
    }
    else if (result0 == 0){
    value0=0;
}      
    if (result1 == 1){
    value1 = 64;    
    }
    else if (result1 == 0){
    value1=0;    
}
    if (result2 == 1){
    value2 = 32;        
    }
    else if (result2 == 0){
    value2=0;
}
    if (result3 == 1){
    value3 = 16;        
    }
    else if (result3 == 0){
    value3=0;
} 
    if (result4 == 1){
    value4 = 8;        
    }
    else if (result4 == 0){
    value4=0;
}
    if (result5 == 1){
    value5 = 4;        
    }
    else if (result5 == 0){
    value5=0;
}
    if (result6 == 1){
    value6 = 2;        
    }
    else if (result6 == 0){
    value6=0;
}
    if (result7 == 1){
    value7 = 1;        
    }
    else if (result7 == 0){
    value7=0;   
}
    int answer = value0+value1+value2+value3+value4+value5+value6+value7;

    System.out.println("Your Denary value is:"+value0+"+"+value1+"+"+value2+"+"+value3+"+"+value4+"+"+value5+"+"+value6+"+"+value7+"="+answer);
}
}

【问题讨论】:

  • 为什么不使用数组?它会大大缩短代码。
  • 使用 switch 语句。查看 oracles 文档docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html。这将是凝聚混乱的最简单方法
  • 不要为每个数字编写单独的代码,而是编写一个循环。要启用它,您需要使用数组来代替 valuen 变量。作为奖励,您现在不需要多个(标量)变量,您现在可以使用全部 resultn

标签: java if-statement binary converter


【解决方案1】:

正如其他人在 cmets 中所说,您使用了太多变量。

试试这个:

public static void main (String[] args) throws java.lang.Exception
{
    Scanner userInput = new Scanner(System.in);    
    String binary;
    int answer = 0;
    System.out.println("Welcome to the Binary to Denary converter!");
    System.out.println("Please enter an 8-BIT Binary value:");

    binary = userInput.next();

    for (int i = 0; i < binary.length(); i++) {
        if (binary.charAt(i) == '1') {
            answer += java.lang.Math.pow(2, binary.length() - i - 1);
        }   
    }

    System.out.println("Your Denary value is:"+answer);
}

如果您正确理解二进制转换,我们从右到左依次递增power of 2。所以不是检查每个值,而是比较字符串中的每个字符并检查它是否是1,如果是则递增。

Here 是上面代码的工作演示。

【讨论】:

  • 很好的答案!感谢您的帮助:3
猜你喜欢
  • 2020-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-21
  • 2016-09-09
  • 1970-01-01
相关资源
最近更新 更多