7/25笔记(switch-case,for结构)

Switch-case结构转换

int a=3;
int x=100;
if(a== 1)
x+=5;
else if(a==2)

x+=10;
else if(a==3)
x+=16;
else ()
x+=34;

等效于class zhuanhuan
{
public static void main(String[] args)
{
int a=3;
int x=100;
switch (a)
{
case 1:
System.out.println(x+=5);
break;
case 2:
System.out.println(x+=10);
break;
case 3:
System.out.println(x+=16);
default:
System.out.println(x+=34);
break;
}
}
}

Scanner类型中获取char形变量

char形变量转换

Switch-case结构中的省略形式

题目:输入一个月份得到想要的季节
class jijie
{
public static void main(String[] args)
{
String number=“november”;
switch (number)
{
case “edc”: //当几个case都输出相同结果时可以省略输出结构
case “sde”:
case “wdsa”:
System.out.println(“春季”);
break;
case “dsad”:
case “wds”:
case “november”:
System.out.println(“夏季”);
break;
case “ghgn”:
case “yjyn”:
case “tgtb”:
System.out.println(“秋季”);
break;
}
}
}

Scanner-if else例题

import java.util.Scanner; //注意这个不要掉了
class scoretest
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in); //标准形式
System.out.println(“请输入学生成绩”);
int score=scan.nextInt();
if (score>=90)
{System.out.println(‘A’);
}
else if (score<90&&score>=70)
{System.out.println(‘B’);
}
else if(score<70&&score>=60){
System.out.println(‘C’);
}
else if(score<60){
System.out.println(‘D’);
}

For循环

for循环结构

题目:在这里插入图片描述
解答:class fortest {
public static void main(String[] args) {
for(int i=1;i<=150;i++){
System.out.print(i+" ");
if (i%3= =0){ //这里是由于博客不能打双等号,所以等号间空格了
System.out.print(“foo”);
}
if (i%5= =0){
System.out.print(“biz”);
}
if (i%7==0){
System.out.print(“baz”);
}
System.out.println();
}
}
}

注意:for循环要定义变量问题
例题:用for循环遍历1-100的奇数,并输出奇数和
class fortest1
{
public static void main(String[] args)
{
int sum=0;//注意这里要赋值,不然输出不了
for (int i=1;i<=100 ;i++ )
{
if (i % 2 !=0)
{
System.out.println(i);//区分加不加“”的区别
sum +=i;
}
}System.out.println(sum);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值