连接变量的Java错误

时间:2016-09-22 02:48:11

标签: java

我对java非常新,只是想更好地理解一些事情。几个小时前我想出了这个java。谢谢你的建议。 我需要弄清楚如何将int语句中的if值基本上取为{= 1}},如果它们= = 1,则另一种方法来计算总数

示例

  

1111111111111111将= 32768 + 16384 + 8192 + 4096 + 2048 + 1024 +   512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1

需要一个可以获取这些值和ADD的方法。 一直在乱搞一两个小时试图破解它。

只是搞乱代码,这就是

package binChange;

import java.util.Scanner;

public class binChange {

    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter in only 1 and 0 and click enter for each time");
        int bin1 = input.nextInt();
        int bin2 = input.nextInt();
        int bin3 = input.nextInt();
        int bin4 = input.nextInt();
        int bin5 = input.nextInt();
        int bin6 = input.nextInt();
        int bin7 = input.nextInt();
        int bin8 = input.nextInt();
        int bin9 = input.nextInt();
        int bin10 = input.nextInt();
        int bin11 = input.nextInt();
        int bin12 = input.nextInt();
        int bin13 = input.nextInt();
        int bin14 = input.nextInt();
        int bin15 = input.nextInt();
        int bin16 = input.nextInt();

        if (bin16 == 1) {
            System.out.println(32768);
            int abin = 32768;

        } else {
            System.out.println(0);
            int abino = 0;
        }
        if (bin15 == 1) {
            System.out.println(16384);
            int bbin = 16384;
        } else {
            System.out.println(0);
            int bbinO = 0;
        }
        if (bin14 == 1) {
            System.out.println(8192);
            int cbin = 8192;
        } else {
            System.out.println(0);
            int cbino = 0;
        }
        if (bin13 == 1) {
            System.out.println(4096);
            int dbin = 4096;
        } else {
            System.out.println(0);
            int dbino = 0;
        }
        if (bin12 == 1) {
            System.out.println(2048);
            int ebin = 2048;
        } else {
            System.out.println(0);
            int ebino = 0;
        }
        if (bin11 == 1) {
            System.out.println(1024);
            int fbin = 1024;
        } else {
            System.out.println(0);
            int fbino = 0;
        }
        if (bin10 == 1) {
            System.out.println(512);
            int gbin = 512;
        } else {
            System.out.println(0);
            int gbino = 0;
        }
        if (bin9 == 1) {
            System.out.println(256);
            int hbin = 256;
        } else {
            System.out.println(0);
            int hbino = 0;
        }
        if (bin8 == 1) {
            System.out.println(128);
            int ibin = 128;
        } else {
            System.out.println(0);
            int ibino = 0;
        }
        if (bin7 == 1) {
            System.out.println(64);
            int jbin = 64;
        } else {
            System.out.println(0);
            int jbino = 0;
        }
        if (bin6 == 1) {
            System.out.println(32);
            int kbin = 32;
        } else {
            System.out.println(0);
            int kbino = 0;
        }
        if (bin5 == 1) {
            System.out.println(16);
            int lbin = 16;
        } else {
            System.out.println(0);
            int lbino = 0;
        }
        if (bin4 == 1) {
            System.out.println(8);
            int mbin = 8;
        } else {
            System.out.println(0);
            int mbino = 0;
        }
        if (bin3 == 1) {
            System.out.println(4);
            int nbin = 4;
        } else {
            System.out.println(0);
            int nbino = 0;
        }
        if (bin2 == 1) {
            System.out.println(2);
            int obin = 2;
        } else {
            System.out.println(0);
            int obino = 0;
        }
        if (bin1 == 1) {
            System.out.println(1);
            int pbin = 1;
        } else {
            System.out.println(0);
            int pbino = 0;
        }

    }

}

2 个答案:

答案 0 :(得分:1)

您可以通过创建变量sum并保持运行总计来将值相加。

使用循环来重复代码:

import java.util.Scanner;

public class BinChange {
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter in only 1 and 0 and click enter for each time");

        int[] bits = new int[16];
        for (int i = bits.length - 1; i >= 0; i--) {
            bits[i] = input.nextInt();
        }

        int sum = 0;
        for (int i = bits.length - 1; i >= 0; i--) {
            int value = bits[i] << i;
            System.out.println(value);
            sum += value;
        }

        System.out.println("The value is: " + sum);
    }
}

Ideone Demo

答案 1 :(得分:-2)

创建一个整数变量并用零初始化它。只要满足条件,就添加值。

int sum = 0;//Declare before first `if` statement.

if语句中满足条件时,请使用以下代码执行添加。例如:

if (bin16 == 1){
    System.out.println(32768);
    //int abin = 32768;//you might not need this statement.  
    sum = sum + 32768;//This 

}

因此,在程序结束时,可以通过调用sum变量来检索总和。例如

System.out.println("Total sum = "+sum);