class For{
public static void main(String[] args) {
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("Hello World!");
System.out.println("我是分隔符~~~~~~~~~~~~~~~~~~~~~~~~");
for(int i = 0; i < 4; i++){
System.out.println("Hello World!");
}
}
}
class TestWhile {
public static void main(String[] args) {
//100以内的偶数的输出
int i = 1;
int sum = 0;
while(i <= 100){
if(i % 2 == 0){
System.out.println(i);
sum += i;
}
i++;
}
System.out.println(sum);
//System.out.println(i);
}
}
class DoWhile{
public static void main(String[] args) {
int i = 1;
do{
if(i % 2 == 0){
System.out.print(i + "\t");
}
i++;
}while(i <= 100);
}
}
/*
所有的循环结构都必须包含以下4部分:
1、初始化条件;
2、循环条件;
3、迭代条件;
4、循环体;
在这段代码中与格式的对应关系为:
1、初始化条件 = int i = 0;
2、循环条件 = i < 4;
3、迭代条件 = i++;
4、循环体 = System.out.println("Hello World!");
*/
class For{
public static void main(String[] args) {
for(int i = 0; i < 4; i++){
System.out.println("Hello World!");
}
}
}
/*
所有的循环结构都必须包含以下4部分:
1、初始化条件;
2、循环条件;
3、迭代条件;
4、循环体;
在这段代码中与格式的对应关系为:
1、初始化条件 = int i = 1;int sum = 0;;
2、循环条件 = i <= 100;
3、迭代条件 = i++;
4、循环体 = if语句;
*/
class TestWhile {
public static void main(String[] args) {
//100以内的偶数的输出
int i = 1;
int sum = 0;
while(i <= 100){
if(i % 2 == 0){
System.out.print(i +"\t");
sum += i;
}
i++;
}
System.out.print(sum);
}
}
/*
所有的循环结构都必须包含以下4部分:
1、初始化条件;
2、循环条件;
3、迭代条件;
4、循环体;
在这段代码中与格式的对应关系为:
1、初始化条件 = int i = 1;
2、循环条件 = i <= 100;
3、迭代条件 = i++;
4、循环体 = if语句;
*/
class TestDoWhile{
public static void main(String[] args) {
int i = 1;
do{
if(i % 2 == 0){
System.out.println(i);
}
i++;
}while(i <= 100);
int j = 10;
do{
System.out.println(j);
j++;
}while(j<10);
while(j < 10){
System.out.println(j);
j++;
}
}
}
class TestFor2 {
public static void main(String[] args) {
for(int j = 0;j < 4;j++){//外层循环控制行数
for(int i = 0;i < 5;i++){//内层循环控制列数
System.out.print("*");
}
System.out.println();
}
}
}
class TestJiuJiu {
public static void main(String[] args) {
for(int i = 1;i <= 9;i++){
for(int j = 1;j <= i;j++){
System.out.print(i + "*" + j + "=" + i*j + "\t");
}
System.out.println();
}
}
}
class TestPrimeNumber {
public static void main(String[] args) {
boolean flag = false;
long start = System.currentTimeMillis();
for(int i = 2;i <= 100000;i++){//实现100以内的自然数的遍历
//如何判断i是否为一个质数
for(int j = 2;j <= Math.sqrt(i);j++){
if(i % j == 0){
flag = true;
break;
}
}
if(!flag){
System.out.println(i);
}
flag = false;
}
long end = System.currentTimeMillis();
System.out.println("所花费的时间为:" + (end - start));
}
}
class TestPrimeNumber1 {
public static void main(String[] args) {
//boolean flag = false;
long start = System.currentTimeMillis();
l:for(int i = 2;i <= 100000;i++){//实现100以内的自然数的遍历
//如何判断i是否为一个质数
for(int j = 2;j <= Math.sqrt(i);j++){
if(i % j == 0){
//flag = true;
//break;
continue l;
}
}
//if(!flag){
//System.out.println(i);
//}
//flag = false;
}
long end = System.currentTimeMillis();
System.out.println("所花费的时间为:" + (end - start));
}
}
class TestPrimeNumber1 {
public static void main(String[] args) {
//boolean flag = false;
long start = System.currentTimeMillis();
l:for(int i = 2;i <= 100000;i++){//实现100以内的自然数的遍历
//如何判断i是否为一个质数
for(int j = 2;j <= Math.sqrt(i);j++){
if(i % j == 0){
//flag = true;
//break;
continue l;
}
}
//if(!flag){
//System.out.println(i);
//}
//flag = false;
}
long end = System.currentTimeMillis();
System.out.println("所花费的时间为:" + (end - start));
}
}
int i1 = 1; int i2 = 2; int i3 = 3;
public class TestArray {
public static void main(String[] args){
int i1;
i1 = 12;
boolean b = true;
//1.如何定义一个数组
//1.1数组的声明
String[] names;
int[] scores;
//1.2初始化
//第一种:静态初始化:初始化数组与给数组元素赋值同时进行。
names = new String[]{"张三","李四","王五"};
//第二种:动态初始化:初始化数组与给数组元素赋值是分开进行的;
scores = new int[4];
//2.如何调用相应的数组元素:通过数组元素的下角标的方式来调用。
//下角标从0开始,到n-1结束。其中N表示的是数组的长度。
scores[0] = 87;
scores[1] = 89;
scores[3] = 98;
//3。数组的长度:通过数组的length属性。
System.out.println(names.length);
System.out.println(scores.length);
//4.如何遍历数组元素
// System.out.println(names[0]);
// System.out.println(names[1]);
// System.out.println(names[2]);
for(int i = 0;i < names.length;i++){
System.out.println(names[i]);
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有