You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Java/Java入门(第四章)- 分支结构.txt

216 lines
4.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

1.Java分支结构之 if...else
package step2;
import java.util.Scanner;
public class HelloIfStep2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
/******start******/
int score;
System.out.println("请输入学员成绩:");
score=input.nextInt();
if(score>=85 && score<=100)
{
System.out.print("优,非常棒!");
}
else
{
System.out.print("良,下次加油!");
}
/******end******/
}
}
2.Java分支结构之Switch
package step4;
import java.util.Scanner;
public class HelloSwitch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入月份:");
int input = sc.nextInt(); //获取输入的月份
//通过输入的月份来判断当前季节并输出
/*****start*****/
int season;
switch(input)
{
case 1:
System.out.print("1月是冬天");
break;
case 2:
System.out.print("2月是冬天");
break;
case 3:
System.out.print("3月是春天");
break;
case 4:
System.out.print("4月是春天");break;
case 5:
System.out.print("5月是春天");
break;
case 6:
System.out.print("6月是夏天");
break;
case 7:
System.out.print("7月是夏天");
break;
case 8:
System.out.print("8月是夏天");
break;
case 9:
System.out.print("9月是秋天");
break;
case 10:
System.out.print("10月是秋天");
break;
case 11:
System.out.print("11月是秋天");
break;
case 12:
System.out.print("12月是冬天");
break;
}
/*****end*****/
}
}
3.Java分支结构之多重if
package step3;
import java.util.Scanner;
public class HelloStep3 {
public static void main(String[] args) {
System.out.println("星级成绩评定系统");
System.out.println("请输入成绩:");
Scanner sc = new Scanner(System.in);
/******start******/
int score;
score=sc.nextInt();
if(score>=90&&score<=100)
{
System.out.print("*****五星成绩");
}
else if(score<90&&score>=80)
{
System.out.print("****四星成绩");
}
else if(score<80&&score>=70)
{
System.out.print("***三星成绩");
}
else if(score<70&&score>=60)
{
System.out.print("**俩星成绩");
}
else if(score<60)
{
System.out.print("无星成绩");
}
/******end******/
}
}
4.来吧我是BOSS
package step5;
import java.util.Scanner;
public class Practice {
final static Scanner sc = new Scanner(System.in); //创建扫描仪
//第一题
public void first(){
System.out.println("请输入人数:");
int input = sc.nextInt(); //获取输入的数据
/*****start*****/
if (input<10)
{
System.out.println("打半场");
}
else
{
System.out.println("打全场");
}
/*****end*****/
}
//第二题
public void second(){
System.out.println("请输入今天星期几:");
int input = sc.nextInt(); //获取输入的数据
/*****start*****/
if (input==1)
{
System.out.println("今天吃米饭");
}
else if(input==2)
{
System.out.println("今天吃牛排");
}
else if(input==3)
{
System.out.println("今天吃鸡排");
}
else
{
System.out.println("今天吃红烧肉");
}
/*****end*****/
}
//第三题
public void third(){
System.out.println("请输入今天星期几:");
int input = sc.nextInt(); //获取输入的数据
/*****start*****/
switch (input)
{
case 1:
System.out.println("今天吃米饭");
break;
case 2:
System.out.println("今天吃牛排");
break;
case 3:
System.out.println("今天吃鸡排");
break;
default:
System.out.println("今天吃红烧肉");
break;
}
/*****end*****/
}
}