这里写目录标题
- 2.5.2 程序流程控制:if-else结构p14
- 2.5.2 程序流程控制:if-else结构p14
- 2.5.2 程序流程控制:if-else结构p15
- 2.5.2 程序流程控制:if-else结构p18
- 2.5.2 程序流程控制:if-else结构p19
- 2.5.3 程序流程控制:switch-case结构P24
- 2.5.3 程序流程控制:switch-case结构P25
- 2.5.3 程序流程控制:switch-case结构P25
- 2.5.3 程序流程控制:switch-case结构P26
- 2.5.4 程序流程控制:循环结构之for循环p37
- 2.5.4 程序流程控制:循环结构之for循环p38
2.5.2 程序流程控制:if-else结构p14
import java.util.Scanner;
class ScannerTest{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name = scan.next();
System.out.println(name);
System.out.println("请输入你的芳龄:");
int age = scan.nextInt();
System.out.println(age);
System.out.println("请输入你的体重:");
double weight = scan.nextDouble();
System.out.println(weight);
System.out.println("你是否相中我了呢?(true/false)");
boolean isLove = scan.nextBoolean();
System.out.println(isLove);
System.out.println("请输入你的性别:(男/女)");
String gender = scan.next();
char genderChar = gender.charAt(0);
System.out.println(genderChar);
}
}
2.5.2 程序流程控制:if-else结构p14
import java.util.Scanner;
class IfTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入岳小鹏期末成绩:(0-100)");
int score = scan.nextInt();
if(score == 100){
System.out.println("奖励一辆BMW");
}else if(score > 80 && score <= 99){
System.out.println("奖励一台iphone xs max");
}else if(score >= 60 && score <= 80){
System.out.println("奖励一个 iPad");
}else{
System.out.println("什么奖励也没有");
}
}
}
2.5.2 程序流程控制:if-else结构p15
import java.util.Scanner;
class IfTest2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个整数:");
int num1 = scanner.nextInt();
System.out.println("请输入第二个整数:");
int num2 = scanner.nextInt();
System.out.println("请输入第三个整数:");
int num3 = scanner.nextInt();
if(num1 >= num2){
if(num3 >= num1)
System.out.println(num2 + "," + num1 + "," + num3);
else if(num3 <= num2)
System.out.println(num3 + "," + num2 + "," + num1);
else
System.out.println(num2 + "," + num3 + "," + num1);
}else{
if(num3 >= num2)
System.out.println(num1 + "," + num2 + "," + num3);
else if(num3 <= num1)
System.out.println(num3 + "," + num1 + "," + num2);
else
System.out.println(num1 + "," + num3 + "," + num2);
}
}
}
2.5.2 程序流程控制:if-else结构p18
class IfExer {
public static void main(String[] args) {
int x = 4;
int y = 1;
if (x > 2)
if (y > 2)
System.out.println(x + y);
else
System.out.println("x is " + x);
int dogAge = 6;
if(dogAge >= 0 && dogAge <= 2){
System.out.println("相当于人的年龄:" + dogAge * 10.5);
}else if( dogAge > 2){
System.out.println("相当于人的年龄:" + (2 * 10.5 + (dogAge - 2) * 4));
}else{
System.out.println("狗狗还没出生呢!");
}
int value = (int)(Math.random() * 90 + 10);
System.out.println(value);
}
}
2.5.2 程序流程控制:if-else结构p19
import java.util.Scanner;
class IfExer1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入你的身高:(cm)");
int height = scan.nextInt();
System.out.println("请输入你的财富:(千万)");
double wealth = scan.nextDouble();
System.out.println("请输入你是否帅:(是/否)");
String isHandsome = scan.next();
if(height >= 180 && wealth >= 1 && isHandsome.equals("是")){
System.out.println("我一定要嫁给他!!!");
}else if(height >= 180 || wealth >= 1 || isHandsome.equals("是")){
System.out.println("嫁吧,比上不足,比下有余。");
}else{
System.out.println("不嫁!");
}
}
}
2.5.3 程序流程控制:switch-case结构P24
class SwitchCaseTest {
public static void main(String[] args) {
int number = 5;
switch(number){
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
case 3:
System.out.println("three");
break;
default:
System.out.println("other");
}
String season = "summer";
switch (season) {
case "spring":
System.out.println("春暖花开");
break;
case "summer":
System.out.println("夏日炎炎");
break;
case "autumn":
System.out.println("秋高气爽");
break;
case "winter":
System.out.println("冬雪皑皑");
break;
default:
System.out.println("季节输入有误");
break;
}
}
}
2.5.3 程序流程控制:switch-case结构P25
class SwitchCaseTest1 {
public static void main(String[] args) {
int score = 78;
switch(score / 10){
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("不及格");
break;
case 6:
case 7:
case 8:
case 9:
case 10:
System.out.println("及格");
break;
}
switch(score / 60){
case 0:
System.out.println("不及格");
break;
case 1:
System.out.println("及格");
break;
}
}
}
2.5.3 程序流程控制:switch-case结构P25
import java.util.Scanner;
class SwitchCaseTest2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入2019年的month:");
int month = scan.nextInt();
System.out.println("请输入2019年的day:");
int day = scan.nextInt();
int sumDays = 0;
switch(month){
case 12:
sumDays += 30;
case 11:
sumDays += 31;
case 10:
sumDays += 30;
case 9:
sumDays += 31;
case 8:
sumDays += 31;
case 7:
sumDays += 30;
case 6:
sumDays += 31;
case 5:
sumDays += 30;
case 4:
sumDays += 31;
case 3:
sumDays += 28;
case 2:
sumDays += 31;
case 1:
sumDays += day;
}
System.out.println("2019年" + month + "月" + day + "日是当年的第" + sumDays + "天");
}
}
2.5.3 程序流程控制:switch-case结构P26
import java.util.Scanner;
class SwitchCaseExer {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入year:");
int year = scan.nextInt();
System.out.println("请输入month:");
int month = scan.nextInt();
System.out.println("请输入day:");
int day = scan.nextInt();
int sumDays = 0;
switch(month){
case 12:
sumDays += 30;
case 11:
sumDays += 31;
case 10:
sumDays += 30;
case 9:
sumDays += 31;
case 8:
sumDays += 31;
case 7:
sumDays += 30;
case 6:
sumDays += 31;
case 5:
sumDays += 30;
case 4:
sumDays += 31;
case 3:
if((year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0){
sumDays += 29;
}else{
sumDays += 28;
}
case 2:
sumDays += 31;
case 1:
sumDays += day;
}
System.out.println(year + "年" + month + "月" + day + "日是当年的第" + sumDays + "天");
}
}
2.5.4 程序流程控制:循环结构之for循环p37
class ForTest {
public static void main(String[] args) {
for(int i = 1;i <= 5;i++){
System.out.println("Hello World!");
}
int num = 1;
for(System.out.print('a');num <= 3;System.out.print('c'),num++){
System.out.print('b');
}
System.out.println();
int sum = 0;
int count = 0;
for(int i = 1;i <= 100;i++){
if(i % 2 == 0){
System.out.println(i);
sum += i;
count++;
}
}
System.out.println("总和为:" + sum);
System.out.println("个数为:" + count);
}
}
2.5.4 程序流程控制:循环结构之for循环p38
class ForTest1 {
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();
}
}
}