Java的基础语法,数据类型、类型转换、运算符等,
if条件控制语句
import java.util.Scanner;
public class ifscore{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的成绩:");
int score = sc.nextInt();
if(score >= 0 && score <= 100){
if(score >= 90 && score <= 100){
System.out.println("自行车一辆");
}else if (score >= 80 && score <= 89){
System.out.println("游乐场一次");
}else if (score >= 70 && score <= 79){
System.out.println("变形金刚一个");
}else if (score >= 60 && score <= 69){
System.out.println("没有奖励");
}else{
System.out.println("挨顿揍");
}
}else{
System.out.println("请输入正确成绩!");
}
}
}
switch语句
switch(week){
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("工作日");
case 6:
case 7:
System.out.println("休息日");
while和for的循环语句
for用于已知循环次数
while用于不知道循环次数
用for语句编写个位十位百位和水仙花数的程序
for(int i = 100;i <= 999;i++){
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100;
if((ge*ge*ge + shi*shi*shi + bai*bai*bai) == i){
System.out.print(i +" ");
count++;
if(count % 2 == 0){
System.out.println();
while的三种循环和死循环
random程序/需求:1-10随机出现数字
import java.util.Random;
public class Damo1Random{
public static void main(String[] args){
Random r = new Random();
for(int i = 1; i <= 10;i++){
int num = r.nextInt(10) + 1;//1-10
System.out.println(num);
}
}
}
跳转语句/需求:避开4楼
public class Damo1continue{
public static void main(String[] args){
for(int i = 1;i <=24;i++){
if(i == 4){
continue;
}
System.out.println(i + "层到了");
}
}
}
编程题/需求在0-100中随机出现一个数
mport java.util.Scanner;
import java.util.Random;
public class Test1{
public static void main(String[] args){
Random r = new Random();
Scanner sc = new Scanner(System.in);
int randomNum = r.nextInt(100) + 1;
System.out.println(randomNum);
while(true){
System.out.println("请输入您要猜的数据:");
int num = sc.nextInt();
if(num > randomNum){
System.out.println("猜大了");
}else if(num < randomNum){
System.out.println("猜小了");
}else{
System.out.println("恭喜猜中了");
break;
}
}
System.out.println("感谢您的使用");
}
}
注释:Scanner 键盘录入 Random 随机数