Java输出
java要调用到util这个包
利用scanner进行输出。
import java.util.Scanner;
public class TestScanner {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
System.out.println("Result is:"+a);
}
}
输入:5,则结果输出为:“Result is:5”
如是输入字符串则变成String a = scanner.next();
这样的字符串的输入只能一个字符,因为System.in。如果要得到一行字符,那么可以用BufferedReader。将System.in封装成InputStreamReader,可以获得更多功能,其中readLine()可以读取一行。
public class TestScanner {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s = in.readLine();
System.out.println(s);
int n = Integer.parseInt( s);
System.out.println(n);
}
}
对数组进行遍历
for(int Array:Arrays)。
int[] arrays = new int[5];
for(int array:arrays){
System.out .println(array);
}
另有一种:System.out.println(Array.toString(array));
int[] arrays = new int[5];
System.out.println(Array.toString(array));
这样输出的就是[0,0,0,0,0]。
对数组进行复制
system.arraycopy(source,0,det,0,5)对数组进行复制操作,就可以不用for循环进行了。
int[] heres = new int[]{1,5,9,56,22,15,85};
int[] theres = new int[5];
System.arraycopy(heres,0,theres,0,5);
for(int there:theres){
System.out.println(there);
}
其中: System.arraycopy(heres,0,theres,0,5);
里面heres是源数组;
0是从索引第几位开始复制;
theres是要复制过去的新数组空间;
0是复制到索引第几位;
5是要复制的长度。