【java入门】Java语言程序设计(基础篇)练习题 第三章

Java语言程序设计 练习题

第三章

3.4 随机月份

注意:(使用数组的时候要注意有无越界)

public class Exercise03_04 {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		java.util.Scanner input = new java.util.Scanner(System.in);
		String[] months = {
    
    "January","February","March","April","May","June","July","August","September","October","November","December"};
		System.out.println("Enter a month number(1 to 12):");
		int monthNumber = input.nextInt();
		System.out.println("The month is "+months[monthNumber-1]);
		
	}

}

运行效果:(eclipse)
在这里插入图片描述

3.9 商业:检查ISBN-10

在这里插入图片描述

以下是运行示例:

Enter the first 9 digits of an ISBN as integer: 013601267
The ISBN-10 number is 013012671

Enter the first 9 digits of an ISBN as integer:013031997 The ISBN-10
number is 013031997X

public class Exercise03_09 {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		java.util.Scanner input = new java.util.Scanner(System.in);
		System.out.println("Enter the first 9 digits of an ISBN as integer:");
		int number = input.nextInt();;
		int total = 0;
		int a = 0;
		int b = number;
		
		 
		
		for(int i=9;i>0;i--) {
    
    
			a = b % 10;
			total += a * i;
			b = b/10;
		}
		if((total%11)!=10) {
    
    
			System.out.print("The ISBN-10 number is " + number + total%11);}
		if((total%11)==10) {
    
    
			System.out.print("The ISBN-10 number is " + number + "X");}
		}
		
	}

3.15 游戏:彩票

在这里插入图片描述
补充:
在这里插入图片描述

import java.util.*;

public class Exercise03_15 {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		
		int lottery = (int)(Math.random()*1000);
		Scanner input = new Scanner(System.in);
		System.out.print("Enter your lottery pick(three digits): ");
		int guess = input.nextInt();
		int[] a = new int[3];
		int[] b = new int[3];
		int s = guess;
		int t = lottery;
		int win = 0;//中奖数
		
		for(int i=0;i<3;i++) {
    
    
			a[i] = s % 10;
			s /=10;
		}
		for(int i=0;i<3;i++) {
    
    
			b[i] = t % 10;
			t /=10;
		}
		
		for(int i=0;i<3;i++) {
    
    
			for(int j =0;j<3;j++) {
    
    
				if(a[i]==b[j]) {
    
    
					win++;
				}}
			
		}
		if(win==1) {
    
    
			System.out.println("Bouns is 1 000 dollars");
		}
		else if(win==3 && a[0]==b[0] && a[1]==b[1] && a[2]==b[2]) {
    
    
			System.out.println("Bouns is 10 000 dollars");
		}
		else if(win ==3 &&(a[0]!=b[0] || a[1]!=b[1] || a[2]!=b[2])) {
    
    
			System.out.println("Bouns is 3 000 dollars");
		}
		else {
    
    System.out.println("No bouns!");}
		
		System.out.print("The lottery is "+lottery);
		
		input.close();
	}
}

3.19(计算三角形的周长)

import java.util.*;

public class Exercise03_19 {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		
		Scanner input = new Scanner(System.in);
		System.out.print("Enter three numbers: ");
		int a = input.nextInt();
		int b = input.nextInt();
		int c = input.nextInt();
		int cir = 0;
		
		if(a+b>c && a+c>b && b+c>a) {
    
    
			cir = a+b+c;
			System.out.print("circumference is "+cir);
		}
		else {
    
    System.out.print("Can't form a triangle");}
		
		input.close();
	
	}
	}

3.21(科学:某天是星期几)

public class Exercise03_21 {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		
		Scanner input = new Scanner(System.in);
		
		System.out.print("Enter year: (e.g.,2012): ");
		int year = input.nextInt();
		
		System.out.print("Enter month: 1-12: ");
		int month = input.nextInt();
		if(month ==1) {
    
    month=13; year=year-1;}
		if(month ==2) {
    
    month=14; year=year-1;}
		
		System.out.print("Enter the day of the month: ");
		int day = input.nextInt();
		int century = year / 100;
        int yearOfCentury = year % 100;

		int h = (day + 26 * (month + 1) / 10 + yearOfCentury + yearOfCentury / 4 + century / 4 + 5 * century) % 7;;
		
		System.out.print("Day of the week is ");
		if(h==0) {
    
    System.out.print("Saturday");}
		else if(h==1) {
    
    System.out.print("Sunday");}
		else if(h==2) {
    
    System.out.print("Monday");}
		else if(h==3) {
    
    System.out.print("Tuesday");}
		else if(h==4) {
    
    System.out.print("Wednesday ");}
		else if(h==5) {
    
    System.out.print("Thursday");}
		else if(h==6) {
    
    System.out.print("Friday");}
	
		input.close();
	}
	}

3.22(几何:点是否在圆内?)

在这里插入图片描述
在这里插入图片描述

package firstprogram;
import java.util.*;

public class Exercise03_22 {
    
    
	public static void main(String[] args) {
    
    
	System.out.print("Enter a point with two coordinates: ");
	Scanner in = new Scanner(System.in);
	float x= in.nextInt();
	float y= in.nextInt();
	double d= Math.sqrt(x*x+y*y);
	if(d<=10) {
    
    
		System.out.print("Point ("+x+", "+y+" ) is in the circle");
	}
	else {
    
    
		System.out.print("Point ("+x+", "+y+" ) is not in the circle");
	}
	in.close();
}}

3.23 点是否在矩形内?

在这里插入图片描述

public class Exercise03_23 {
    
    
	public static void main(String[] args) {
    
    
	System.out.print("Enter a point with two coordinates: ");
	Scanner in = new Scanner(System.in);
	float x= in.nextInt();
	float y= in.nextInt();
	
	if(x<5&& x>(-5) && y<2.5 && y>(-2.5)) {
    
    
		System.out.print("Point ("+x+", "+y+" ) is in the rectangle");
	}
	else {
    
    
		System.out.print("Point ("+x+", "+y+" ) is not in the rectangle");
	}
	in.close();
}}

3.24游戏:挑一张牌

在这里插入图片描述

public class Exercise03_24 {
    
    
	public static void main(String[] args) {
    
    
	System.out.print("Enter a number: ");
	Scanner in = new Scanner(System.in);
	int x= in.nextInt();
	in.close();
	int d=x/13;
	int s=x%13;
	String[] suits= {
    
    "Clubs","Diamonds","Hearts","Spades"};
	String[] ranks= {
    
    "Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
	System.out.print("The card you picked is "+ranks[s]+" of "+suits[d-1]);
}}

3.28几何:两个矩形

在这里插入图片描述
在这里插入图片描述

public class Exercise03_28 {
    
    
	public static void main(String[] args) {
    
    
	Scanner in = new Scanner(System.in);
	System.out.print("Enter rl's center x-, y-coordinates, width , and height: ");
	double x1= in.nextDouble();
	double y1= in.nextDouble();
	double w1= in.nextDouble();
	double h1= in.nextDouble();
	System.out.print("Enter r2's center x-, y-coordinates, width , and height: ");
	double x2= in.nextDouble();
	double y2= in.nextDouble();
	double w2= in.nextDouble();
	double h2= in.nextDouble();
	in.close();
	if(x2-w2/2>=x1-w1/2 && x2+w2/2<=x1+w1/2 && y2-h2/2>=y1-h1/2 && y2+h2/2<=y1+h1/2) {
    
    
		System.out.print("r2 is inside rl");
	}
	else if(x2-w2/2<x1-w1/2 || x2+w2/2>x1+w1/2 || y2-h2/2<y1-h1/2 || y2+h2/2>y1+h1/2) {
    
    
		System.out.print("r2 does not overlap rl");
	}
	else {
    
    
		System.out.print("r2 overlaps rl");
	}
	
	
}}

3.29 几何:两个圆

在这里插入图片描述

public class Exercise03_29 {
    
    
	public static void main(String[] args) {
    
    
	Scanner in = new Scanner(System.in);
	System.out.print("Enter circlel's center x-, y-coordi nates, and radius: ");
	double x1= in.nextDouble();
	double y1= in.nextDouble();
	double r1= in.nextDouble();
	
	System.out.print("Enter circle2 's center x-, y-coordinates, and radius: ");
	double x2= in.nextDouble();
	double y2= in.nextDouble();
	double r2= in.nextDouble();
	
	in.close();
	
	double d=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
	double d2=Math.abs(r1-r2);
	
	if(d<=d2) {
    
    
		System.out.print("circle2 is inside circlel");
	}
	else if(d<=r1+r2) {
    
    
		System.out.print("circle2 overlaps circlel");
	}
	else {
    
    
		System.out.print("circle2 does not overlap circlel");
	}
	
	
}}

补充1.出现’input’ is never closed的警告

可以在代码最后加一句:

input.close();

おすすめ

転載: blog.csdn.net/qq_51669241/article/details/114030131