2016-11-25 91 views
-1

这是我第一次在这里发表。我已经在这个项目上呆了好几个小时了。我浏览了网页,发现只有一小段代码给了我更多的见解,但我很困惑。它应该做二进制。使用“while”循环填充二进制数组时遇到的麻烦

该程序运行,但似乎只返回3个“反向”二进制整数与第四或更多始终为“0”指示数组字段为空。任何人都知道发生了什么事?

/** 
* A program that prints the binary digits of a positive integer 
* 
* @author Your Name 
* @version Today's Date 
*/ 
import java.util.Scanner; 

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

     final int MIN = 0; 
     final int MAX = (int) (Math.pow(2, 30) - 1); 
     int[] digits = new int[30]; // array to hold the digits 

     int number = readInput("Enter an integer from " + MIN + " to " + MAX, MIN, MAX); // number to divide 


     // Your code goes here. This code must do the following: 
     // Declare a "companion" variable to count how many digits stored 
     // Initialize that variable appropriately 
     // While number > 0 
     // Store the remainder (number % 2) in the array 
     // Update the companion variable 
     // Set number to be number/2 
     // Display the filled portion of the array in reverse order 
     // System.out.println(number); 
     int index = 0; // the counter 
     int q = number/2; // quotient 
     int r = number % 2; // remainder 
     int n = 1; // iteration 
     int qr = 0; // returning quotient 


     while (number > 0) { 

      number = (int) q; 
      // System.out.println(number); 
      // System.out.println("Quotient - "+q); 
      System.out.println("Remainder - " + r); 
      // System.out.println("Iteration - "+n); 

      qr = (int) q/2; // Quotient to be returned 
      r = (int) q % 2; // New Remainder to be returned 
      q = qr; // Resetting the number to be divided 
      // n++; // Incrementing the loop 

      digits[index] = r; 
      index++; 

     } 

     listBackwardsFrom(digits, index); 
    } 

    /** 
    * Print the contents of an array backwards, starting from index last 
    * 
    * @param array: 
    *   an array of integers 
    * @param last: 
    *   the index of the starting location 
    */ 
    public static void listBackwardsFrom(int[] array, int last) { 
     for (int i = 0; i < last; i++) { 
      //System.out.println(i); 
      System.out.println(array[i]); 
      //System.out.println(array[28]); 
      //System.out.println(array[27]); 
      //System.out.println(array[26]); 
     } 
    } 

    /** 
    * Read in an integer between two given bounds 
    * 
    * @param prompt: 
    *   the prompt to show the user 
    * @param min: 
    *   the smallest acceptable input value 
    * @param max: 
    *   the largest acceptable input value 
    */ 
    public static int readInput(String prompt, int min, int max) { 
     Scanner scan = new Scanner(System.in); 
     boolean check = true; 
     System.out.println(prompt); 
     String userinput = scan.next(); 
     while (check) { 

      try { 

       if (Integer.valueOf(userinput) <= max && Integer.valueOf(userinput) >= min) { 
        check = false; 
       } else { 
        check = true; 
        System.out.println("enter an integer in valid range"); 
        userinput = scan.next(); 

       } 
      } catch (NumberFormatException e) { 
       check = true; 
       System.out.println("enter a valid integer"); 
       userinput = scan.next(); 
      } 

     } 

     return Integer.parseInt(userinput); 

    } 

    /** 
    * Read in a positive integer and return its value 
    * 
    * @param the 
    *   prompt to be shown to the user 
    */ 
    public static int readInteger(String prompt) { 
     Scanner scan = new Scanner(System.in); 
     boolean check = true; 
     System.out.println(prompt); 
     String userinput = scan.next(); 
     while (check) { 

      try { 

       if (Integer.valueOf(userinput) > 0) { 
        check = false; 
       } else { 
        check = true; 
        System.out.println("enter a valid integer"); 
        userinput = scan.next(); 

       } 
      } catch (NumberFormatException e) { 
       check = true; 
       System.out.println("enter a valid integer"); 
       userinput = scan.next(); 
      } 

     } 

     return Integer.parseInt(userinput); 
    } 
} 
+4

调试您的代码 –

+2

对不起,“这是我的代码,什么是错误的”形式的问题是脱离主题。您应该已经在调试器中逐步了解代码,并能够确定哪些行为意外。请访问[帮助],尤其是阅读[问]。 –

回答

1

检查此项,在计算下一个周期前存储提醒。

 while (number > 0) { 
     number = (int) q; 
     System.out.println("Remainder - " + r); 
     digits[index] = r; 
     qr = (int) q/2; // Quotient to be returned 
     r = (int) q % 2; // New Remainder to be returned 
     q = qr; // Resetting the number to be divided 
     index++; 
    } 

并从最后遍历开始。

public static void listBackwardsFrom(int[] array, int last) { 
    for (int i = last-1; i >= 0; i--) { 
     System.out.print(array[i]); 
    } 
} 
+0

非常感谢你!这是因为我在计算周期后存储了提醒。它现在有效!我的循环有点尘土飞扬,但现在我明白了。再次感谢 –