Es ist mein erstes Mal hier posten. Ich bin seit vielen Stunden auf dieses Programm gestoßen. Ich habe das Internet durchforstet und fand nur kleine Teile des Codes, die mir mehr Einblick gaben, aber ich bin verwirrt. Es soll binär sein.Problem mit der "While" -Schleife zum Füllen Array mit Binär
Das Programm läuft, aber scheint nur 3 ganze Zahlen in der "umgekehrten" Binärdatei zurückzugeben, wobei die vierte oder mehr immer "0" ist, was anzeigt, dass das Feld leer ist. Jeder weiß was los ist?
/**
* 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);
}
}
Debuggen Sie Ihren Code –
Sorry, Fragen des Formulars "Hier ist mein Code, was ist falsch" sind Off-Topic. Sie sollten den Code im Debugger bereits durchgegangen sein und feststellen können, was sich unerwartet verhält. Bitte besuchen Sie die [Hilfe] und lesen Sie [fragen]. –