选择语句:
(1):单if语句
...
if(条件){
条件为真时,执行此代码块
}
...
(2):if-else语句
...
if(条件){
条件为真,执行此语句
}else{
条件为假时,执行此语句
}
if-else语句有时候可以改写为三目运算符,当局部代码块中为具体数字时:
if(a>b){
c=200;
}else{
c=100;
}
int c = a>b?200:100;
(3) if-else嵌套
if(条件1){
条件1为真时执行此代码块
if(条件2){
条件1为真并且条件2为真时执行此代码块
}else{
条件1为真并且条件2为假时执行此代码块
}
}else{
条件1为假时执行此代码块
}
(4)多if-else分支
if(条件1){
条件1为真执行此代码块
}else if(条件2){
条件1为假并且条件2位真执行此代码块
}else{
条件1和条件2为假执行此代码块
}
(5)switch语句
switch(变量){
case 选项1:
变量 == 选项1时,执行此语句
case 选项2:
变量 == 选项2时,执行此语句
...
default:
前面选项中没有符合条件的,执行此语句
}
if 和 switch 区别:
if可以对区间值进行比较;
switch只能对固定值进行比较。
三目运算符:
布尔表达式(条件表达式)(三目运算符)
数据类型 变量名 = 布尔表达式?常量值1:常量值2
编程练习题:
package day03;
import java.util.Scanner;
public class Demo3_1{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
// 获取系数a、b、c
System.out.println("Enter a,b,c : ");
double a = scanner.nextDouble();
double b = scanner.nextDouble();
double c = scanner.nextDouble();
// 根据b^2-4*a*c的值进行判断
int result = (int)(Math.pow(b, 2)-4*a*c);
if(result>0){
double real1 = (-b+Math.pow(result, 0.5))/(2*a);
double real2 = (-b-Math.pow(result, 0.5))/(2*a);
System.out.println("The equation has two roots "+real1+" and "+real2);
}else if(result==0){
double real3 = (-b)/2*a;
System.out.println("The equation has one root "+real3);
}else{
System.out.println("The equation has no real roots ");
}
}
}
package day03;
import java.util.Scanner;
public class Demo3_2 {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a,b,c,d,e,f:");
// 系数和常数项
double a = scanner.nextDouble();
double b = scanner.nextDouble();
double c = scanner.nextDouble();
double d = scanner.nextDouble();
double e = scanner.nextDouble();
double f = scanner.nextDouble();
// 判断该方程是否有解
double result = a*d-b*c;
if(result==0){
System.out.println("The equation has no solution ");
}else{
double x = (e*d-b*f)/result;
double y = (a*f-e*c)/result;
System.out.println("x is "+x+" and y is "+y);
}
}
}
package day03;
import java.util.Scanner;
public class Demo3_3 {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter today's day:");
// 获取今天是周几
int today = scanner.nextInt();
System.out.println("Enter the number of days elapsed since today:");
// 获取几天后的天数
int days = scanner.nextInt();
int result = (today+days)%7;
String a=" ";
String b=" ";
// 输出今天是周几
switch(today%7){
case 0: a = "Sunday";break;
case 1: a = "Monday";break;
case 2: a= "Tuesday";break;
case 3: a = "Wednesday";break;
case 4: a = "Thursday";break;
case 5: a = "Friday";break;
case 6: a = "Saturday";break;
}
// 输出几天后是周几
switch(result%7){
case 0: b = "Sunday";break;
case 1: b = "Monday";break;
case 2: b = "Tuesday";break;
case 3: b = "Wednesday";break;
case 4: b = "Thursday";break;
case 5: b = "Friday";break;
case 6: b = "Saturday";break;
}
System.out.println("Today is "+ a +" and the future day is "+b);
}
}
3.4
import java.util.Scanner;
public class Demo3_4 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
//1.获取用户输入的前九位编号
System.out.print("Enter the first 9 digits of an ISBN as integer:");
int digit=scanner.nextInt();
int digitOri=digit;
//2.将九位编号分别取出
System.out.println(digit);
int d9=digit%10;
digit/=10;
int d8=digit%10;
digit/=10;
int d7=digit%10;
digit/=10;
int d6=digit%10;
digit/=10;
int d5=digit%10;
digit/=10;
int d4=digit%10;
digit/=10;
int d3=digit%10;
digit/=10;
int d2=digit%10;
digit/=10;
int d1=digit%10;
//3.根据取出的前九位数字 计算第十位
int d10=(d1*1+d2*2+d3*3+d4*4+d5*5+d6*6+d7*7+d8*8+d9*9)%11;
System.out.println(d10);
//4.拼接ISBN 输出
String res="";
if(d1==0){
res+=0;//"0"
}
if(d10==10){
res=res+digitOri+"X";
// System.out.println(digitOri+"X");//+ 字符串连接符
}else{
res=res+digitOri+d10;
}
System.out.println(res);
}
}
//或者:数字输入之间需要空格,不能像题目中无空格输入
package day03;
import java.util.Scanner;
public class Demo3_4 {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the first 9 digits of an ISBN :");
int n1 = scanner.nextInt();
int n2 = scanner.nextInt();
int n3 = scanner.nextInt();
int n4 = scanner.nextInt();
int n5 = scanner.nextInt();
int n6 = scanner.nextInt();
int n7 = scanner.nextInt();
int n8 = scanner.nextInt();
int n9 = scanner.nextInt();
int num = (n1*1+n2*2+n3*3+n4*4+n5*5+n6*6+n7*7+n8*8+n9*9)%11;
if(num==10){
System.out.println("The ISBN-10 number is "+n1+n2+n3+n4+n5+n6+n7+n8+n9+'X');
}else{
System.out.println("The ISBN-10 number is "+n1+n2+n3+n4+n5+n6+n7+n8+n9+num);
}
}
}
package day03;
import java.util.Scanner;
public class Demo3_5 {
public static void main(String[] args){
// 输入一个三位数
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a three-digit integer:");
int number = scanner.nextInt();
// 将三位数进行拆分,对比个位和百位
int a = number%10;
int b = number/10/10;
if(a==b){
System.out.println(number+" is a palindrome");
}else{
System.out.println(number+" is not a palindrome");
}
}
}
//一一列举几种情况:
package day03;
import java.util.*;
public class Demo3_6 {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("scissor(0),rock(1),paper(2):");
int num = scanner.nextInt();
Random random = new Random();
int result = random.nextInt(3);
System.out.println(result);
if(result==0&&num==1){
System.out.println("The Computer is scissor.you are rock.You won");
}else if(result==0&&num==2){
System.out.println("The Computer is scissor.you are paper. computer won");
}else if(result==0&&num==0){
System.out.println("The computer is scissor,you are scissor. It is a draw");
}
if(result==1&&num==2){
System.out.println("The Computer is rock.you are paper.You won");
}else if(result==1&&num==0){
System.out.println("The Computer is rock.you are scissor. computer won");
}else if(result==1&&num==1){
System.out.println("The computer is rock,you are rock. It is a draw");
}
if(result==2&&num==2){
System.out.println("The computer is paper,you are rock. It is a draw");
}else if(result==2&&num==0){
System.out.println("The Computer is paper.you are scissor. You won");
}else if(result==2&&num==1){
System.out.println("The computer is paper,you are rock. computer won");
}
}
}
//找出出拳数字的关系
import java.util.Scanner;
public class Demo3_6 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
//1.输入玩家的选择
System.out.print("Enter :");
int p=scanner.nextInt();
//2.随机生成选择
int c=(int) (Math.random()*3);//[0,1)*3 [0,3)
System.out.println(c);
int result=-1;
String cStr="";//出拳信息
String pStr="";//出拳信息
if(p==0){
result=(p+c+3)%3;
}
if(p==1){
result=(p+c+1)%3;
}
if(p==2){
result=(p+c+2)%3;
}
if(p==0){
pStr="scissor";
}else if(p==1){
pStr="rock";
}else{
pStr="paper";
}
if(c==0){
cStr="scissor";
}else if(c==1){
cStr="rock";
}else{
cStr="paper";
}
System.out.print("The computer is "+cStr+". You are "+pStr+".");
switch (result) {
case 0:
System.out.println(" too! It is draw.");
break;
case 1:
System.out.println("You lose.");
break;
case 2:
System.out.println("You won.");
break;
}
}
}
/*
* 0 1 2
* 0 1 2
*
* P C
* 0 0 平0 0+3%3
* 0 1 输1 1+3%3
* 0 2 赢2 2+3%3
*
* 1 0 赢2 1+1%3
* 1 1 平0 2+1%3
* 1 2 输1 3+1%3
*
* 2 0 输1 2+2%3
* 2 1 赢2 3+2%3
* 2 2 平0 4+2%3
* */
package day03;
import java.util.Scanner;
public class Demo3_7 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入三个边长:");
double x1 = scanner.nextDouble();
double x2 = scanner.nextDouble();
double x3 = scanner.nextDouble();
//两边之和大于第三边(判断是否能构成三角形)
if(x1+x2>x3&&x1+x3>x2&&x2+x3>x1){
double grith = x1+x2+x3;
System.out.println("the length of triangle is "+grith);
}else{
System.out.println("the side is illegal");
}
}
}
3.8
package day03;
import java.util.Scanner;
public class Demo3_8 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter year:(e.g., 2012):");
int year = scanner.nextInt();
System.out.println("Enter month:1-12:");
int month = scanner.nextInt();
System.out.println("Enter the day of the month:1-31:");
int day = scanner.nextInt();
if(month==1||month==2){
month+=12;
year--;
}
int k=year%100;
int j=year/100;
int h = (day+(26*(month+1))/10+k+k/4+j/4+5*j)%7;
String a="";
switch(h){
case 0:
a = "Saturday";
break;
case 1:
a = "Sunday";
break;
case 2:
a = "Monday";
break;
case 3:
a = "Tuesday";
break;
case 4:
a = "Wednesday";
break;
case 5:
a = "Thursday";
break;
case 6:
a = "Friday";
break;
}
System.out.println("Day of the week is "+a);
}
}
package day03;
import java.util.Scanner;
public class Demo3_9 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a point with two coordinates:");
//x、y
double x = scanner.nextDouble();
double y = scanner.nextDouble();
double result = Math.sqrt(x*x+y*y);
if(result<=10){
System.out.println("Point( "+x+" , "+y+" )"+" is in the circle");
}else{
System.out.println("Point( "+x+" , "+y+" )"+" is not in the circle");
}
}
}
package day03;
import java.util.Scanner;
public class Demo3_10 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a point with two coordinates:");
int x = scanner.nextInt();
int y = scanner.nextInt();
// 点到(0,0)的距离
if(x<5&&y<2.5){
System.out.println("Point ( "+x+" , "+y+" ) is in the rectangle");
}else{
System.out.println("Point ( "+x+" , "+y+" ) is not in the rectangle");
}
}
}
package day03;
import java.util.*;
public class Demo3_11 {
public static void main(String[] args) {
Random random = new Random();
int num = random.nextInt(13);
String a = "";
//给0-12个数字分别赋予13张牌
switch(num){
case 0:
a = "Ace";
break;
case 1:
a = "2";
break;
case 2:
a = "3";
break;
case 3:
a = "4";
break;
case 4:
a = "5";
break;
case 5:
a = "6";
break;
case 6:
a = "7";
break;
case 7:
a = "8";
break;
case 8:
a = "9";
break;
case 9:
a = "10";
break;
case 10:
a = "Jack";
break;
case 11:
a = "Queen";
break;
case 12:
a = "King";
break;
}
int number = (int)(Math.random()*4);
String b = "";
//分别赋予四种花色
switch(number%4){
case 0:
b = "Clubs";
break;
case 1:
b = "Diamonds";
break;
case 2:
b = "Hearts";
break;
case 3:
b = "Spades";
break;
}
//拼接随机产生的花色和牌
System.out.println("The card you picked is "+a+" of "+b);
}
}
import java.util.Scanner{
public class Demo3_12 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
//1.输入四个坐标点
System.out.print("Enter 4 points:");
double x1=scanner.nextDouble();
double y1=scanner.nextDouble();
double x2=scanner.nextDouble();
double y2=scanner.nextDouble();
double x3=scanner.nextDouble();
double y3=scanner.nextDouble();
double x4=scanner.nextDouble();
double y4=scanner.nextDouble();
//2.结合方程组 计算方程组中的系数和常数项
double a=y1-y2;
double b=-(x1-x2);
double c=y3-y4;
double d=-(x3-x4);
double e=(y1-y2)*x1-(x1-x2)*y1;
double f=(y3-y4)*x3-(x3-x4)*y3;
//3.判断方程组是否有解
double delt=a*d-b*c;
if(delt==0){
System.out.println("The two line are parallel");
}else{
double x=(e*d-b*f)/delt;
double y=(a*f-e*c)/delt;
System.out.println("x="+x+",y="+y);
}
}
}
3.13
//方法一:按照斜线方程
package day03;
import java.util.Scanner;
public class Demo3_13{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a point's x- and y-coordinates:");
double x1 = scanner.nextDouble();
double y1 = scanner.nextDouble();
if((x1<=200&&y1<=-0.5*x1+100)||(y1<=100&&x1<=200-2*y1)){
System.out.println("The point is in the trigle");
}else{
System.out.println("The point is not in the triangle ");
}
}
}
//按照x轴和y轴的比例
package day03;
import java.util.Scanner;
public class Demo3_13{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a point's x- and y-coordinates:");
double x = scanner.nextDouble();
double y = scanner.nextDouble();
if(x>=0&&x<=200){
if((200-x)/y>=2){
System.out.println("in");
return;
}
}
System.out.println("out");
}
}
package day03;
import java.util.Scanner;
public class Demo3_14 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter r1's center x-,y-coordinates,width,and height:");
double x1 = scanner.nextDouble();
double y1 = scanner.nextDouble();
double w1 = scanner.nextDouble();
double h1 = scanner.nextDouble();
System.out.println("Enter r2's center x-,y-coordinates,width,and height:");
double x2 = scanner.nextDouble();
double y2 = scanner.nextDouble();
double w2 = scanner.nextDouble();
double h2 = scanner.nextDouble();
if(x2<=w1/2-w2/2+x1&&x2>=(x1-w1/2+w2/2)&&y2<=h1/2-h2/2+y1&&y2>=y2-h1/2+h2/2){
System.out.println("inside");
}else if(x2>=w1/2+w2/2+x1||x2<=(x1-w1/2-w2/2)||(y2>=h1/2+h2/2+y1)||(y2<=y2-h1/2-h2/2)){
System.out.println("out");
}else{
System.out.println("重叠");
}
}
}
3.15
package day03;
import java.util.Scanner;
import java.lang.Math;
public class Demo3_15 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter circle1's center x-,y-coordinates,and radius:");
double xo = scanner.nextDouble();
double yo = scanner.nextDouble();
double ro = scanner.nextDouble();
System.out.println("Enter circle2's center x-,y-coordinates,and radius:");
double x = scanner.nextDouble();
double y = scanner.nextDouble();
double r = scanner.nextDouble();
if(Math.abs(ro-r)>=(Math.sqrt((xo-x)*(xo-x)+(yo-y)*(yo-y)))){
System.out.println("inside");
}else if((Math.sqrt((xo-x)*(xo-x)+(yo-y)*(yo-y)))<=ro+r){
System.out.println("overlaps(重叠)");
}else{
System.out.println("out");
}
}
}
package day03;
import java.util.Scanner;
public class Demo3_16 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter three points for p0,p1 and p2:");
double x0 = scanner.nextDouble();
double y0 = scanner.nextDouble();
double x1 = scanner.nextDouble();
double y1 = scanner.nextDouble();
double x2 = scanner.nextDouble();
double y2 = scanner.nextDouble();
double result = (x1-x0)*(y2-y0)-(x2-x0)*(y1-y0);
if(result>0){
System.out.println("( "+x2+" , "+y2+" ) is on the left side of the line from ( "+x0+" , "+y0+" ) to ( "+x1+" , "+y1+" )");
}else if(result<0){
System.out.println("( "+x2+" , "+y2+" ) is on the right side of the line from ( "+x0+" , "+y0+" ) to ( "+x1+" , "+y1+" )");
}else{
System.out.println("( "+x2+" , "+y2+" ) is on the line from ( "+x0+" , "+y0+" ) to ( "+x1+" , "+y1+" )");
}
}
}