计数器 (江苏教育考试院)
http://202.102.112.54/statApp/CountApp
SimpleCount.java
package com.javaeye.lindows.test;
public class SimpleCount {
/**
* @param args
*/
private static final int n1 = 2, n2 = 3;
public void Method(int x, int y) {
System.out.println(x + y);
System.out.println(x - y);
System.out.println((double) x / y);
System.out.println(x * y);
}
public static void main(String[] args) {
SimpleCount sCount = new SimpleCount();
sCount.Method(n1, n2);
}
}
ForTest.java
package com.javaeye.lindows.util;
import java.util.Scanner;
/**
* @author Lindows
* @see http://www.java2000.net/p11512
* http://www.diybl.com/course/3_program/java/javashl/200857/114863.html
* http://topic.csdn.net/u/20071017/21/107d54c0-a850-4f0e-b92a-c01c8ba97000.html?seed=2058733001
*/
public class ForTest {
public ForTest() {
}
// 题目:打印1-100的和
public void forSum() {
int sum = 0;
for (int i = 0; i <= 100; i++) {
sum += i;// sum = sum + i;
}
System.out.println("for 循环:" + sum);
// ---------------------------
int count2 = 0;
int sum2 = 0;
while (count2 <= 100) {
sum2 += count2;
// System.out.println(count2);
count2++;
// System.out.println(sum2);
}
System.out.println("while 循环:" + sum2);
// ---------------------------
int count3 = 0;
int sum3 = 0;
do {
sum3 += count3;
// System.out.println(count3);
count3++;
// System.out.println(sum3);
} while (count3 <= 100);
System.out.println("do...while 循环:" + sum3);
}
// 题目:打印用数组求1-100的和
public void forArray() {
// Integer[] intArray = new Integer[100];
// Integer intArray[] = new Integer[100]; //不建议使用此方式
int[] intArray = null;
intArray = new int[100];
int sum4 = 0;
for (int i = 0; i < 100; i++) {
intArray[i] = i + 1;
}
for (int j = 0; j < 100; j++) {
sum4 += intArray[j];
}
System.out.println("int或Integer数组循环:" + sum4);
}
// 题目:打印1到100中的偶数,一行5个,并求和
public void forEven() {
int sum5 = 0;
for (int i = 1; i <= 100; i++) {
if ((i % 2 == 0)) {
System.out.printf("%9d", i);// 或%3d等
sum5 += i;
if (i % 5 == 0) {
System.out.println();
}
}
}
System.out.println("1到100中的偶数和" + sum5);
}
// 题目:打印1到100的各奇偶之和,及奇+偶的和
public void forInt() {
int sumOdd = 0; // 设定奇数和的变量
int sumEven = 0;// 设定偶数之和的变量
for (int i = 1; i <= 100; i++) {
while (i % 2 != 0) {
sumOdd += i;
break;
}
do {
if (i % 2 == 0) {
sumEven += i;
}
break;
} while (i > 100);
}
System.out.println("1到100中的奇数之和为" + sumOdd);
System.out.println("1到100中的偶数之和为" + sumEven);
System.out.println("1到100中的整数之和为" + (sumOdd + sumEven));
}
// 题目:打印几何形如:正直角,倒直角,等腰三角,九九乘法表,倒九九表
public void forGeometric() {
// 打印:直角三角形
for (int i = 0; i < 9; i++) {
for (int j = 0; j < i; j++) {
System.out.print("*+");
}
System.out.println("*");
}
System.out.println("---------------------------------");
// 打印:倒直角三角形
for (int i = 9; i >= 0; i--) {
for (int j = i; j >= 0; j--) {
System.out.print("*-");
}
System.out.println("");// 换行
}
System.out.println("---------------------------------");
// 打印:等腰三角形
final int line = 9;
for (int i = 0; i < line; i++) {
for (int j = -(line - 1); j <= i; j++) {
System.out.print(Math.abs(j) <= i ? "*" : " ");
}
System.out.println();
}
System.out.println("---------------------------------");
// 打印:九九乘法表
for (int m = 1; m <= 9; m++) {
// System.out.print(m);
for (int n = 1; n <= m; n++) {
// System.out.print(n);
System.out.print(m + "*" + n + "=" + m * n + " ");
}
System.out.println(" ");
}
System.out.println("---------------------------------");
// 打印:倒九九乘法表
for (int x = 9; x >= 1; x--) {
for (int y = x; y >= 1; y--) {
System.out.print(x + "*" + y + "=" + x * y + " ");
}
System.out.println("");
}
}
// 题目:求4位数的任意3位的排列组合数>>>场景:输入验证码时可能用到
public void forCombination() {
// 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
// 计数器变量
int count = 0;
// 利用三重循环组合数字的百、十、个位
for (int i = 1; i <= 4; i++) { // 生成百位
for (int j = 1; j <= 4; j++) { // 生成十位
for (int k = 1; k <= 4; k++) { // 生成个位
// 确保i、j、k三位互不相同
if (i != j && j != k && i != k) {
++count;// 计数器累加
// 控制台输出当前组合出的三位数
System.out.println("第" + count + "种组合:" + i + "" + j
+ "" + k);
}
}
}
}
System.out.println("共计" + count + "种三位数组合方式。");
}
// 题目:打印菱形
public void forDiamond() {
Scanner input = new Scanner(System.in);
// 循环标识
boolean con = true;
// 菱形的层数和转换计算方式后的层数
int border, num = 0;
do {
con = false;
System.out.print("请输入要打印的菱形层数[不小于3的奇(整)数]:");
// 菱形的层数
border = input.nextInt();
if (border % 2 == 0 || border < 3) {
System.out.println("只能输入不小于3的奇(整)数!请重试!");
con = true;
}
} while (con);
// 转换层数计算方式
num = border - (border - (border % 2)) / 2;
// 控制台输出提示
System.out.println(border + "层菱形图案如下: ");
// 打印上半部分图象
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= num - i; j++) {
// System.out.print(" "); //这样瘦长一点
System.out.print(" ");
}
for (int j = 1; j <= 2 * (i + 1) - 3; j++) {
// System.out.print("*"); //这样瘦长一点
System.out.print("* ");
}
System.out.println();
}
// 打印下半部分图象
for (int i = 1; i <= num - 1; i++) {
for (int j = 1; j <= i; j++) {
// System.out.print(" "); //这样瘦长一点
System.out.print(" ");
}
int k = num - 1 - i;
for (int j = 1; j <= 2 * k + 1; j++) {
// System.out.print("*"); //这样瘦长一点
System.out.print("* ");
}
System.out.println();
}
}
public static void main(String[] args) {
ForTest fTest = new ForTest();
// 题目:打印1-100的和
fTest.forSum();
System.out.println("---------------------------------");
// 题目:打印用数组求1-100的和
fTest.forArray();
System.out.println("---------------------------------");
// 题目:打印1到100中的偶数,一行5个,并求和
fTest.forEven();
System.out.println("---------------------------------");
// 题目:打印1到100的各奇偶之和,及奇+偶的和
fTest.forInt();
System.out.println("---------------------------------");
// 题目:打印几何形如:正直角,倒直角,等腰三角,九九乘法表,倒九九表
fTest.forGeometric();
System.out.println("---------------------------------");
// 题目:求4位数的任意3位的排列组合数>>>场景:输入验证码时可能用到
fTest.forCombination();
System.out.println("---------------------------------");
// 题目:打印菱形
fTest.forDiamond();
System.out.println("---------------------------------");
}
}
SwitchTest.java
package com.javaeye.lindows.test;
/**
*
* @author lindows
* @see 题目: 给出一值X,用switcht case语句判断其值(注意,不是用if,else):X<1,X=1,2,3,4,X>4
* 本题考查了switch的嵌套和三元运算符
*/
public class SwitchTest {
public void switchMethod1(int x) {
switch (x) {
case 1:
System.out.println("x=1");
break;
case 2:
System.out.println("x=2");
break;
case 3:
System.out.println("x=3");
break;
case 4:
System.out.println("x=4");
break;
default:
// 如果x>4运行x=5,
switch (x > 4 ? x = 5 : x) {
case 5:
System.out.println("x>4");
break;
default:
System.out.println("x<1");
break;
}
}
}
public String switchMethod2(int y) {
switch (y) {
case 1:
return "y=1";
case 2:
return "y=2";
case 3:
return "y=3";
case 4:
return "y=4";
default:
return y > 4 ? "y>4" : "y<1";
}
}
public static void main(String[] args) {
SwitchTest sTest = new SwitchTest();
// 输入数字测试
sTest.switchMethod1(-1);
sTest.switchMethod1(1);
sTest.switchMethod1(2);
sTest.switchMethod1(3);
sTest.switchMethod1(4);
sTest.switchMethod1(5);
System.out.println("--------------------");
// 输入数字测试
System.out.println(sTest.switchMethod2(1));
System.out.println(sTest.switchMethod2(2));
System.out.println(sTest.switchMethod2(3));
System.out.println(sTest.switchMethod2(4));
System.out.println(sTest.switchMethod2(5));
System.out.println(sTest.switchMethod2(0));
}
}
end