Java基础之两个小算法
博客出自:http://blog.csdn.net/liuxian13183,转载注明出处! All Rights Reserved !
遍历二维数组
package com.blog.test;
public class TestLoop {
public static void main(String[] args) {
int cal = 0;
String[][] array = new String[7][6];
for (int i = 0; i < array.length; i++) {// 遍历纵向的数据
for (int j = 0; j < array[i].length; j++) {// 遍历横向的数据
System.out.print(array[i][j] + "a" + "\t");
++cal;
if (cal % 7 == 0) {
System.out.println();
}
}
}
}
}
输入出“请输入一个数字:”--》判断--》是数字的话,打出该数字;不是的话,再输出“请输入数字:”!package com.blog.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class TestCalculate {
static TestCalculate b = new TestCalculate();
public static void main(String[] args) throws Exception {
b.isNumber();
}
public String deal() throws Exception {
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("請輸入一列数字: ");
String a = buf.readLine();
return a;
}
public void isNumber() throws Exception {
String str = b.deal();
boolean isNumber = true;
char[] ch = str.toCharArray();
for (int k = 0; k < ch.length && isNumber == true; k++) {
isNumber = Character.isDigit(ch[k]);
if (isNumber) {
System.out.println("您输入的数字是:" + str);
int c = Integer.parseInt(str);
for (int i = c; i > 0; i--) {
for (int j = i; j > 0; j--) {
System.out.print(j + " ");
}
System.out.println();
}
break;
} else {
b.isNumber();
}
}
}
}
浙公网安备 33010602011771号