java 使用 System.out.write 打印到控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4144895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:00:27  来源:igfitidea点击:

Print to console using System.out.write

java

提问by ivorykoder

It is homework question.

是作业题。

Question:

问题:

"Use an array of integers with size 10 and print the content of the array using write( ) method.

Note: Please do not use System.out.println( ) method."

“使用大小为 10 的整数数组,并使用 write() 方法打印数组的内容。

注意:请不要使用 System.out.println() 方法。”

This is what I have tried so far, but it doesn't work.

这是我迄今为止尝试过的方法,但它不起作用。

int i = 0;
while (i != array.length) {
  byte[] temp = new byte[4];
  temp[0] = (byte) (array[i] & 256);
  array[i] = array[i] >> 8;
  temp[1] = (byte) (array[i] & 256);
  array[i] = array[i] >> 8;
  temp[2] = (byte) (array[i] & 256);
  array[i] = array[i] >> 8;
  temp[3] = (byte) (array[i] & 256);
  System.out.write(temp, 0, 4);
  i++;
}

I can't find a way to do this, please help me out.

我找不到办法做到这一点,请帮助我。

Thanks in advance. :)

提前致谢。:)

回答by deorst

void printInt(int x) {
  String s = String.valueOf(x);
  for(int i = 0; i < s.length(); i++) {
     System.out.write(s.charAt(i));
  }
  System.out.write('\n');
}

Edit: prettier:

编辑:更漂亮:

void printInt(int x) {
  String s = String.valueOf(x);
  System.out.write(s.getBytes());
  System.out.write('\n')
}

回答by Buhake Sindi

Have an integer array of size 10, i.e. int[] i = new int[10]and write each of them in System.out.write(int b)where the write()method accept an integer as parameter.

有大小为10,即一个整数数组int[] i = new int[10]并写入它们中的每在System.out.write(int b)其中write()方法接受一个整数作为参数。



This is really uglybut it worked....

这真的很丑,但它有效......

/**
 * 
 */
package testcases;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @author The Elite Gentleman
 *
 */
public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] array = {10, 20, 30, 40, 50};

        StringBuffer sb = new StringBuffer();
        for (int i: array) {
            sb.append(String.valueOf(i));
        }

        try {
            System.out.write(sb.toString().getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

回答by rapadura

Why are you making an array of bytes ? There is a write(int b) method.

你为什么要制作一个字节数组?有一个 write(int b) 方法。

Integer array = new Integer[10];
// populate array with some integers
for (Integer i : array) {
    System.out.write((int)i);
}

even the cast (int)i isnt necessary as autoboxing will take care of that.

甚至演员 (int)i 也不是必需的,因为自动装箱会解决这个问题。

回答by anirvan

The trick is to convert each digit of the int into a char. and then write the char using System.out.write.
So for example, if one of the int values is 91. You'll need to do
System.out.write('9');
System.out.write('1');
System.out.flush();

诀窍是将 int 的每个数字转换为 char。然后使用System.out.write.
例如,如果 int 值之一是 91。你需要做
System.out.write('9');
System.out.write('1');
System.out.flush();