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形变量
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循环
题目:
解答: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);
}
}