本周主要学习并练习了JAVA语言的if语句,switch语句和for语句,这三类语句作为JAVA语言的基础语法,牢固的掌握是十分必要的。

if语句:

public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
if(a>b) {
System.out.println("a:"+a);
}
else if(a<b) {
System.out.println("b:"+b);
}
else {
System.out.println("相等");
}
}

switch语句:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int main() {
printf("请输入今天是星期几(1-7): \n");
int day = 0;
scanf("%d", &day);
switch (day) { //switch括号里面必须放整数
case 1:
printf("星期一!\n");
break;//break表示跳出当前的switch语句
case 2:
printf("星期二!\n");
break;
case 3:
printf("星期三!\n");
break;
case 4:
printf("星期四!\n");
break;
case 5:
printf("星期五!\n");
break;
case 6:
printf("星期六!\n");
break;
case 7:
printf("星期日!\n");
break;
default:
printf("您的输入有误!\n");
}
system("pause");
return 0;
}

for语句:

# include <stdio.h>
int main(void)
{
int i;
int sum = 0; 
for (i=1; i<=100; ++i) 
{
sum = sum + i;
}
printf("sum = %d\n", sum);
return 0;
}

下一周将继续进行JAVA语言的深入算法与语句的学习

 

posted on 2022-07-16 16:09  杨申龙  阅读(26)  评论(0)    收藏  举报