标签:string 产生 列表 cto appear first 调用 多重循环 got
多重:switch(表达式)
package Primary;
/**
* Java控制结构
* @version 15:21 2019-09-29
* @author xxwang1018
*/
class IfElseTest { //选择结构之if-else
public void i2() {
int a = 5;
if(a>1) {
System.out.println("aaaaaaa");
}
if(a>10) {
System.out.println("bbbbbbb");
} else {
System.out.println("ccccccc");
}
if(a>10) {
System.out.println("dddddd");
}
else if(a>=5) {
System.out.println("eeeeee");
}
else {
System.out.println("ffffff");
}
}
}
class SwitchTest { //选择结构之switch
public void s1() {
int a1 = 1;
int a2 = 2;
switch(a1+a2) {
case 1: System.out.println("aaaaaaa");
break;
case 2: System.out.println("bbbbbbb");
break;
case 3: System.out.println("ccccccc");
//break;
default:System.out.println("ddddddd");
}
String b = "abc";
switch(b) {
case "abc": System.out.println("eeeeeee");
break;
case "def": System.out.println("fffffff");
break;
case "hgi": System.out.println("ggggggg");
break;
default:System.out.println("hhhhhhh");
}
}
}
class WhileTest { //循环结构之while
public void w1() {
System.out.println("=============While Test==========");
int x = 10;
while (x < 20) {
System.out.print("value of x : " + x);
x++;
System.out.print("\n");
}
System.out.println("=============Do While Test==========");
x = 10;
do {
x++;
if(x%2 == 0) {
continue;
}
System.out.println("value of x : " + x);
} while (x < 20);
}
}
class ForTest { //循环结构之for
public void f2() {
for(int i=0;i<5;i++) {
for(int j=0;j<5;j++) {
if(j<=i) {
System.out.print("*");
} else {
break;
}
}
System.out.println();
}
}
}
public class ControlStructure {
public static void main(String[] args) {
new IfElseTest().i2();
new WhileTest().w1();
new ForTest().f2();
new SwitchTest().s1();
}
}
import java.util.*;
import java.math.*;
/**
* This program discribes special "break" statements
* @version 15:40 2019-03-27
* @auther xxwang1018
*/
public class SpecialBreak{
public static void main(String[] args) {
int[] score = new int[10];
int total=0;
for (int i = 0; i < 10; ++i){
score[i] = (int) (1 + Math.random() * 100); //随机产生学生分数
total+=score[i]; //计算总分
}
double average=1.0*total/10; //计算平均分
int counter=0;
/**
count 用来记录第一个低于平均分的学生的序号,且必须要初始化
*/
read_data:
while(average<total){
for(int i=0; i<10; ++i){
if(score[i]>average)
System.out.println("Congratulations! You are ok!"); //对超过平均分的学生鼓励
else{
System.out.println("\nThe first student who doesn't get an average score appears.");
counter=i;
break read_data;
}
System.out.println(score[i]);
}
}
System.out.println("\nThe average is "+average+"\nThis student's score is "+score[counter]);
}
}
/*
测试结果:
Congratulations! You are ok!
92
Congratulations! You are ok!
85
The first student who doesn't get an average score appears.
The average is 61.6
This student's score is 39
修饰词(public 或者 static) 返回值(int 或者void),函数名(形 参列表) {函数体}
package Primary;
/**
* Java函数
* @version 15:42 2019-09-29
* @author xxwang1018
*/
public class Function {
public static void main(String[] args) {
int a = 5;
int b = factorialCalculation(a);
System.out.println("The factorial of " + a + " is " + b);
}
public static int factorialCalculation(int m) {
if (m > 1) {
return m * factorialCalculation(m - 1);
} else {
return 1;
}
}
}
标签:string 产生 列表 cto appear first 调用 多重循环 got
原文地址:https://www.cnblogs.com/xxwang1018/p/11607032.html