Java 简单语法(if switch 循环)

本文介绍了Java中if-else条件判断、switch语句用于不同分数等级的输出,以及while、do-while和for循环的基本应用实例。通过这些核心语法,读者将掌握基本的控制流程和循环结构在实际编程中的使用。
摘要由CSDN通过智能技术生成

if

package com.m.demo;

import java.util.Scanner;

public class IfSwitchWhile2 {

	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		System.out.print("输入成绩");
		double score=s.nextDouble();
		
//		if(score<60.0) {
//			System.out.println("不及格");
//		}else {System.out.println("及格");
//		
//		}
		
//		if(score>=90) {
//			System.out.println("优秀");
//		}else if(score>=75) {
//			System.out.println("良好");
//		}else {
//			System.out.println("一般");
//		}
		
			

	}

}

switch

package com.m.demo;

import java.util.Scanner;

public class IfSwitchWhile3 {

	public static void main(String[] args) {
		System.out.println("输入名次");
		Scanner s=new Scanner(System.in);
		int index=s.nextInt();
		switch(index) {
		case 1:System.out.println("1");break;
		case 2:System.out.println("2");break;
		case 3:System.out.println("3");break;
		default:System.out.println("0");break;
		}
		
			

	}

}

循环

while、dowhile、for

package com.m.demo;

import java.util.Scanner;

public class IfSwitchWhile {

	public static void main(String[] args) {
		int i=0,j=0;
		
//		//while循环
//		while(i<=100) {
//			System.out.printf("循环了%d次\r\n",(j+1));
//			j++;
//			i+=10;
//			
//		}
		do while
//		do {
//			System.out.printf("循环了%d次,i=%d\r\n",(j+1),i);
//			j++;
//			i+=10;
//		}while(i<=100);
//		for循环
		for(;i<100;j++,i+=10) {
			System.out.printf("循环了%d次,i=%d\r\n",(j+1),i);
		}
		
			
			
		
			

	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值