new一个Scanner,()里面System.in代表输入,alt+enter
public class Stu {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("-使用next方式接受:");
if (scanner.hasNext()){
String str=scanner.next();
System.out.println("输出的内容为:"+str);
}
scanner.close();
}
}
?
?
//-使用next方式接受:
天红很棒
输出的内容为:天红很棒
?
//判断输入的是不是一个整数,其他类型同理
?
int i=0;
if (scanner.hasNextInt()){
i=scanner.nextInt();
System.out.println(i);
}
<!---->for循环的快捷键------例如*100.for*????( o=^?ェ?)o ┏━┓(ctrl+shift+b)
forEach
用outer标签跳到外层循环:
public class demo01 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >i ; j--) {
System.out.print("-");
}
for (int j = 1; j <=i; j++) {
System.out.print("+");
}
for (int j = 1; j < i; j++) {
System.out.print("=");
}
System.out.println();
}
}
}
//用debug分析
System.out.println();
类 对象 方法
java是值传递
(?′艸`?)
int[] arr=new int[5];
//数组定义
System.out.println( arr.length);
length属性求数组长度
?
数组三种初始化及内存分析
int[] arr=new int[5];
System.out.println( arr.length);
System.out.println( );
for (int i : arr) {
//arr.for回车
}
for (int i = 0; i < arr.length; i++) {
//arr.for选fori回车
}
//二维数组同理
按住ctrl单击,左下边框那里的(7:Structure)
?
由大到小排,如从小到大则if( < )
几行几列有几个不同值,每一个值的位置
?
public static void main(String[] args) {
// 创建一个11*11的二维数组 0:没有棋子,1黑棋,2白棋
int[][] array1 = new int[11][11];
array1[1][2]=1;
array1[2][3]=2;
System.out.println("输出原始的数组:");
for (int[] ints : array1) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println();
}
// 转换为稀疏数组保存
// 获取有效值的个数
int sum=0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (array1[i][j]!=0) {
sum++;
}
}
}
System.out.println("有效值的个数:"+sum);
?
// 创建一个稀疏数组的数组
int[][] array2 = new int[sum + 1][3];
array2[0][0]=11;
array2[0][1]=11;
array2[0][2]=sum;
?
// 遍历二维数组,将非零的值,存放稀疏数组中
int count=0;
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j <array1[i].length ; j++) {
if (array1[i][j]!=0){
count++;
array2[count][0]=i;
array2[count][1]=j;
array2[count][2]=array1[i][j];
?
}
}
}
?
// 输出稀疏数组
for (int i = 0; i <array2.length ; i++) {
System.out.println(array2[i][0]+"\t" +
array2[i][1]+"\t"+array2[i][2]+"\t");
}
?
// 稀疏数组的还原
// 1. 读取稀疏数组
int[][] array3 = new int[array2[0][0]][array2[0][1]];
?
// 2.给稀疏数组元素还原它的值
for (int i = 1; i < array2.length; i++) {
array3[array2[i][0]][array2[i][1]]=array2[i][2];
}
?
// 3.打印
System.out.println("输出还原的值的数组");
?
for (int[] ints : array3) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println();
}
?
}
?
原文:https://www.cnblogs.com/th11/p/14842715.html