本章目标是通过学习能够在 Java 的主方法中编写分支结构程序,涉及的 Java 编程基本概念包括:分支语句 if 、if-else、if-else if-else、switch 和 break 语句, 关系运算符、逻辑运算符和表达式,三目运算符 ? : 等。你准备好了吗?
public class Test1{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//声明整型变量 a , b ,c 获取控制台的输入。
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int max = 0;
max = a;
if(max<b)max=b;
if(max<c)max = c;
System.out.println("最大值为:" + max);
}
}
public class Test2{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 声明整型变量 x , y ,z 获取控制台的输入
int x = scanner.nextInt();
int y = scanner.nextInt();
int z = scanner.nextInt();
// 声明整型变量 temp ,用于变量值的临时存储
int temp = 0;
if(x>y){temp=x;x=y;y=temp;}
if(x>z){temp=x;x=z;z=temp;}
if(y>z){temp=y;y=z;z=temp;}
System.out.println("从小到大排列:" + x + " " + y + " " + z);
}
}
import java.util.Scanner;
public class ApplicationTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 声明整型变量 x , y 获取控制台的输入
int x = scanner.nextInt();
int y = scanner.nextInt();
if(x%2==0&&y%2==0){
System.out.println("两个数同为偶数!");
}else{
System.out.println("两个数至少有一个数为奇数!");
}
}
}
import java.util.Scanner;
public class ApplicationTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 声明 int 类型的变量 num ,用来获取控制台输入
int num = scanner.nextInt();
if(num%10==num/10000&&(num/10%10)==(num/1000%10)){
System.out.print("回文数");
}else{
System.out.print("不是回文数");
}
}
}
import java.util.Scanner;
public class ApplicationTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double score = scanner.nextDouble();
// 声明 int 类型变量 temp 用来保存学生成绩是否合格的结果(1 或者 0)
int temp;
temp = score<60 ? 0 : 1;
String result = temp == 1 ? "合格":"不合格";
System.out.println("该学生成绩判定为:" +result );
}
}
import java.util.Scanner;
public class ApplicationTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 声明 char 类型的变量 a 用来获取控制台输入
char a = scanner.next().charAt(0);
int b = a;
if(b>=65&&b<=90){
System.out.print("该字符是大写字母");
}else if(b>=97&&b<=122){
System.out.print("该字符是小写字母");
}else if(b>=48&&b<=57){
System.out.print("该字符是数字");
}else{
System.out.print("该字符是其他字符");
}
}
}
import java.util.Scanner;
public class ApplicationTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int year = scanner.nextInt();
int month = scanner.nextInt();
int day = 0;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)day=31;
if(month==4||month==6||month==9||month==11)day=30;
if(month==2){
if(year%400==0||(year%4==0&&year%100!=0))day=29;
else day=28;
}
System.out.print(year + "年" + month + "月有" + day + "天");
}
}
import java.util.Scanner;
public class ApplicationTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 声明 char 类型的变量 score 用来获取控制台输入的成绩
char score = scanner.next().charAt(0);
switch(score){
case 'A':System.out.println("百分制分数段为90分以上");break;
case 'B':System.out.println("百分制分数段为80-89分");break;
case 'C':System.out.println("百分制分数段为70-79分");break;
case 'D':System.out.println("百分制分数段为60-69分");break;
case 'E':System.out.println("百分制分数段为60分以下");break;
}
}
}
import java.util.Scanner;
public class ApplicationTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 声明 int 类型的变量 a 用来获取控制台输入
int a = scanner.nextInt();
if(a>=0){
if(a%2==0){
System.out.println("该数既是自然数又是偶数");
}else{
System.out.println("该数为自然数,但不是偶数");
}
}else{
System.out.println("该数不是自然数");
}
}
import java.util.Scanner;
public class Equation {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
double a = reader.nextDouble();
double b = reader.nextDouble();
double c = reader.nextDouble();
double p = b*b - 4*a*c;
if(p>0){
System.out.println("该方程有两个根");
double f1 = (-b+Math.sqrt(p))/(2.0*a);
double f2 = (-b-Math.sqrt(p))/(2.0*a);
System.out.print("x1 = " + String.format("%.2f", f1) + ",x2 = "+ String.format("%.2f", f2));
}else if(p==0) {
System.out.println("该方程只有一个根");
double x = (-b+Math.sqrt(p))/(2.0*a);
System.out.print("x = " + String.format("%.2f", x));
}else {
System.out.print("该方程无解");
}
}
}
import java.util.Scanner;
public class Logistics {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
double p = reader.nextDouble(); // 表示每公里每吨货物的基本运费
double w = reader.nextDouble(); // 表示货物重量
double s = reader.nextDouble(); // 运输距离
double exp=0;
if(s>0&&s<250) {
exp = p *w*s;
System.out.format("%.2f",exp);
}else if(s>=250&&s<500) {
exp = p *w*s *(1-0.02);
System.out.format("%.2f",exp);
}else if(s>=500&&s<1000) {
exp = p *w*s *(1-0.05);
System.out.format("%.2f",exp);
}else {
exp = p *w*s *(1-0.08);
System.out.format("%.2f",exp);
}
}
}
emmmm第二章就到这里了,不难的,新手福利。