mis*_*hsx 3 java arrays debugging java-8
我正在学习的基础知识,Java似乎有一个观察我无法理解。
下面是应该打印的代码的二进制执行一个的base10数量。没有错误或警告,但我正在寻找答案的逻辑异常。
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Main
{
private static final Scanner scanner = new Scanner(System.in);
public static void main(String args[]) {
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int remainder, quotient, i=0;
int[] binary=new int[n];
/* Binary division -Extracting the 1's and 0's out of the base10 number and storing it in binary[] */
while(n!=0){
remainder=n%2;
quotient=n/2;
binary[i]=remainder;
n=quotient;
i++;
}
int k=i; // For storing the length of array till I want my bits to be reversed since all other bits are initialized to 0.
/*Reversing the binary[] to get the coreect binary value.*/
for(int j=0; j<=i; j++){
int temp=binary[j];
binary[j]=binary[i];
binary[i]=temp;
i--;
}
/*Printing out all the bits of binary number. */
for(int l=1;l<=k;l++){
System.out.print(binary[l]);
}
System.out.println(binary); /*Prints a garbage looking value: For ex- for 25 i get: [I@33909752 */
scanner.close();
}
}
Run Code Online (Sandbox Code Playgroud)
对我的代码的关注:
1.当我尝试打印数组变量 - 时,我无法理解为什么我会看到随机的垃圾内容的可能推理binary。这是预料之中的事情吗?如果是这样,这是什么?
例如:
在我的代码中,当我提供打印变量时25得到的输入。我期待像[I@33909752binary[1,1,0,0,1,0,....,0]
2.我得到一个额外的0在我的二进制值的面前,如果我开始for-loop打印binary的0,而不是1
例如:
for(int l=1;l<=k;l++)
{
System.out.print(binary[l]);
}
Run Code Online (Sandbox Code Playgroud)
打印11001for25但如果我从 开始循环0,我会得到011001. 我通过放置 SOPLn 语句检查了代码的所有其他地方,但0数组的索引无处为 0。我想知道为什么?
不要随意输入你想到的东西。您不使用包java.io, java.math, java.security, java.text, java.util.concurrent, 或 中的任何内容java.util.regex。到目前为止,您的代码中唯一需要导入的是java.util.Scanner
你不需要像scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"). 如果您要读取另一个标记,这无论如何都不会在您的代码中发生,默认情况下,扫描仪将跳过空格,包括换行符。还有scanner.nextLine(),但你在这里不需要它。
不要关闭System.in。虽然Scanner围绕您自己分配的资源创建的应该关闭,但您不对System.in.
System.out.println(Object)打印调用toString()对象的结果。对于不提供实现的类,如数组,继承自java.lang.Objectproducts的方法getClass().getName() + "@" + Integer.toHexString(hashCode())。该int[]数组的类名[I,这就是为什么你看到[I@再加上一些十六进制数。
使用System.out.println(Arrays.toString(binary));来代替。但这将打印整个数组,包括未使用的元素。
您应该在循环中引入一个新的可变临时变量,而不是在循环中修改现有变量,而是在外部作用域中创建对新变量的需求。此外,您必须熟悉标准循环习惯用法,从索引零开始并排除结束索引(使用<而不是<=)。
当条件计算为 时,循环变量将包含循环后排除的结束索引false。此逻辑也适用于您的第一个while循环;之后,i将包含第一个未使用的索引或已使用的长度,这两种解释都是有效的。后续使用必须排除它, using <,正如所说,这是通常的模式。对于您的反转循环,这也意味着在使用结束索引之前必须递减。
修复循环逻辑将解决前导零的问题。
import java.util.Arrays;
import java.util.Scanner;
public class Main {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String args[]) {
int n = scanner.nextInt();
int remainder, quotient, i=0;
int[] binary=new int[n];
/* Binary division -Extracting the 1's and 0's
out of the base10 number and storing it in binary[] */
while(n!=0) {
remainder=n%2;
quotient=n/2;
binary[i]=remainder;
n=quotient;
i++;
}
/*Reversing the binary[] to get the correct binary value.*/
for(int j=0, k=i-1; j<k; j++,k--) {
int temp=binary[j];
binary[j]=binary[k];
binary[k]=temp;
}
/*Printing out all the bits of binary number. */
for(int l=0; l<i; l++) {
System.out.print(binary[l]);
}
System.out.println();
System.out.println(Arrays.toString(binary));
}
}
Run Code Online (Sandbox Code Playgroud)
import java.util.Arrays;
import java.util.Scanner;
public class Main {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String args[]) {
int n = scanner.nextInt();
int remainder, quotient, i=0;
int[] binary=new int[n];
/* Binary division -Extracting the 1's and 0's
out of the base10 number and storing it in binary[] */
while(n!=0) {
remainder=n%2;
quotient=n/2;
binary[i]=remainder;
n=quotient;
i++;
}
/*Reversing the binary[] to get the correct binary value.*/
for(int j=0, k=i-1; j<k; j++,k--) {
int temp=binary[j];
binary[j]=binary[k];
binary[k]=temp;
}
/*Printing out all the bits of binary number. */
for(int l=0; l<i; l++) {
System.out.print(binary[l]);
}
System.out.println();
System.out.println(Arrays.toString(binary));
}
}
Run Code Online (Sandbox Code Playgroud)