java语言程序设计基础篇第三章编程练习题

1
套公式

2
import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		int number1 = (int)(System.currentTimeMillis()%10);
		int number2 = (int)(System.currentTimeMillis()/7%10);
		int number3 = (int)(System.currentTimeMillis()/3%10);
		Scanner input = new Scanner(System.in);
		System.out.print("What is " + number1 + " + " + number2 + " + " + number3 + "? " );
		int answer = input.nextInt();
		System.out.println(number1 + " + " + number2 + " = " + answer + " is " + (number1+number2+number3 == answer));
	}
}

3
参考第一章13题

4
import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		int month;
		String[] months = {"January","February","March","April","May","June","July","August",
				"September","October","November","December"};
		for(int i = 0; i < 20; ++i){
			month = (int)(System.currentTimeMillis()%12);
			System.out.println(months[month]);
		}
	}
}

5
import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		String[] weeks = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
		Scanner input = new Scanner(System.in);
		int num1 = input.nextInt();
		int num2 = input.nextInt();
		int result = (num1+num2)%7;
		System.out.println(weeks[result]);
	}
}


6
略

7
程序清单2-10加个判断就可以了

8
import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		int num1 = input.nextInt();
		int num2 = input.nextInt();
		int num3 = input.nextInt();
		int temp = 0;
		if(num1 > num2){
			temp = num1;
			num1 = num2;
			num2 = temp;
		}
		
		if(num1 > num3){
			temp = num1;
			num1 = num3;
			num3 = temp;
		}
		
		if(num2 > num3){
			temp = num2;
			num2 = num3;
			num3 = temp;
		}
		
		System.out.println(num1+ " " + num2 + " " +num3);
	}
}

9
import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		String nums = input.next();
		int result = 0;
		for(int i = 0; i < 9; ++i){
			int num = (int)(nums.charAt(i) - '0');
			result += num*(i+1);
		}
		result %= 11;
		System.out.print(nums);
		if(result == 10)
			System.out.println("X");
		else
			System.out.println(result);
	}
}

10

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		int number1 = (int)(Math.random()*100);
		int number2 = (int)(Math.random()*100);
		System.out.println("What is " + number1 + " + " + number2 + "?");
		Scanner input = new Scanner(System.in);
		int answer = input.nextInt();
		if(number1 + number2 == answer)
			System.out.print("You are correct!");
		else
			System.out.println(number1 + " + " + number2 + " should be " + (number1 + number2));
		
	}
}

11

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		int[] months = {0,31,28,31,30,31,30,31,31,30,31,30,31};
		int year = 0;
		int month = 0;
		Scanner input = new Scanner(System.in);
		year = input.nextInt();
		month = input.nextInt();
		if(month == 2){
			if((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
				System.out.println(months[month]+1);
			else
				System.out.println(months[month]);
		}
		else{
			System.out.println(months[month]);
		}
	}
}

12

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("Enter a three-digit integer:");
		int number = input.nextInt();
		if(number/100 == number%10)
			System.out.println(number + " is a palindrome");
		else
			System.out.println(number + " is not a palindrome");
	}
}

13

。。。。。

14

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		int number = ((int)(Math.random()*100)) % 2;
		System.out.println("Please enter 0(contrary) or 1(front)");
		int answer = input.nextInt();
		if(number == answer)
			System.out.println("You are correct");
		else
			System.out.println("You are wrong!");
	}
}

15

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		int lottery = (int)(Math.random()*1000);
		System.out.println("Enter your lottery pick(three digit)");
		int guess = input.nextInt();
		int lottery1 = lottery/100;
		int lottery2 = lottery%10;
		int lottery3 = (lottery%100)/10;
		
		int guess1 = guess/100;
		int guess2 = guess%10;
		int guess3 = (guess%100)/10;
		
		if(guess == lottery)
			System.out.println("Exact macth:you win $10000");
		else if((lottery1 == guess2 && lottery2 == guess1 && lottery3 == guess3)||
				(lottery1 == guess3 && lottery3 == guess1 && lottery2 == guess2)||
				(lottery1 == guess1 && lottery2 == guess3 && lottery3 == guess2))
			System.out.println("Match all digits:you win $3000");
		else if(guess1 == lottery1 || guess1 == lottery2 || guess1 == lottery3 ||
				guess2 == lottery1 || guess2 == lottery2 || guess2 == lottery3 ||
				guess3 == lottery1 || guess3 == lottery2 || guess3 == lottery3 )
			System.out.println("Match one digit:you win $1000");
		else
			System.out.println("Sorry,you are wrong!");
	}
}

16

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		int height = (int)(Math.random()*101);
		int width = (int)(Math.random()*51);
		if(((int)(Math.random()))%2 == 0)
			height = -height;
		if(((int)(Math.random()))%2 != 0)
			width = -width;
		System.out.println("x:" + width + ",y:"+height);
	}
}

17

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("scissor(0),rock(1),paper(2)");
		int user = input.nextInt();
		int computer = (int)(System.currentTimeMillis())%3;
		if(user == 0 && computer == 1)
			System.out.println("You lost!");
		else if(user == 0 && computer == 2)
			System.out.println("You won!");
		else if(user == 1 && computer == 0)
			System.out.println("You won!");
		else if(user == 1 && computer == 2)
			System.out.println("You lost!");
		else if(user == 2 && computer == 0)
			System.out.println("You lost!");
		else if(user == 2 && computer == 1)
			System.out.println("You won!");
		else
			System.out.println("It is draw!");
	}
}

18

套公式

19

import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		double e1 = input.nextDouble();
		double e2 = input.nextDouble();
		double e3 = input.nextDouble();
		if(e1+e2 > e3 && e1+e3 > e2 && e2+e3 > e1){
			double p = (e1+e2+e3)/2;
			System.out.println(Math.sqrt(p*(p-e1)*(p-e2)*(p-e3)));
		}
		else{
			System.out.println("The length is illegal");
		}
	}
}

20

。。。。。。

21

/*书中给出的名字叫泽勒一致性,这个公式也叫蔡勒公式*/
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		String[] week = {"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};
		System.out.print("Enter year:");
		int year = input.nextInt();
		System.out.print("Enter month 1-12:");
		int month = input.nextInt();
		System.out.print("Enter the day of the month:");
		int day = input.nextInt();
		int m = 0;
		if(month == 1 || month == 2){
			m = month+12;
			--year;
		}
		else
			m = month;
		int k = year%100;
		int j = year/100;
		int h = (day + 26*(m+1)/10 + k + k/4 + j/4 + 5*j) % 7;
		System.out.println(week[h]);
	}

}


22

import java.util.Scanner;
public class Main {

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

}


23

import java.util.Scanner;
public class Main {
	
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		int x,y;
		System.out.print("Enter a point with two coordinates:");
		x = cin.nextInt();
		y = cin.nextInt();
		boolean flag = false;
		if(x <= 5 && (float)y <= 2.5f) flag = true;
		if(flag){
			System.out.println("Point" + "(" + x + "," + y + ")" + "is in the rectangular");
		}
		else{
			System.out.println("Point" + "(" + x + "," + y + ")" + "is not in the rectangular");
		}
	}
}

24

import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		String[] size = {"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
		String[] flowerColor = {"Clubs","Diamonds","Hearts","Spades"};
		System.out.println("The card you picked is " + size[(int)(Math.random()*13)] +
				" of " + flowerColor[(int)(Math.random()*4)]);
	}
}

25
套公式,没啥意义

26

import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter an integer:");
		int number = input.nextInt();
		System.out.println("Is 10 divisible by 5 and 6 ?" +
		((number%5==0) && (number%6==0)));
		System.out.println("Is 10 divisible by 5 or 6 ?" +
		((number%5==0) || (number%6==0)));
		System.out.println("Is 10 divisible by 5 or 6, but not both ?" +
		((number%5==0) ^ (number%6==0)));
	}
}

27

import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("Enter a point's x- and y-coordinates:");
		double x = input.nextDouble();
		double y = input.nextDouble();
		double k = -0.5;
		if(x < 0 || y < 0)
			System.out.println("The point is not in the triangle.");
		else{
			double cx = 200 - 2*y;
			if(x > cx)
				System.out.println("The point is not in the triangle.");
			else
				System.out.println("The point is in the triangle.");
		}
	}
}

28
import java.util.Scanner;
public class Main {
	
	public static void main(String[] args){
		Scanner cin = new Scanner(System.in);
		double r1x,r1y,r1width,r1height;
		double r2x,r2y,r2width,r2height;
		System.out.print("Enter r1's center x-,y-coordinates,width, and height:");
		r1x = cin.nextDouble();
		r1y = cin.nextDouble();
		r1width = cin.nextDouble();
		r1height = cin.nextDouble();
		System.out.print("Enter r2's center x-,y-coordinates,width, and height:");
		r2x = cin.nextDouble();
		r2y = cin.nextDouble();
		r2width = cin.nextDouble();
		r2height = cin.nextDouble();
		
		double distance = Math.sqrt((r1x - r2x)*(r1x - r2x) + (r1y - r2y)*(r1y - r2y));
		if((distance <= r1width - r2width/2) && (distance <= r1height - r2height/2)){
			System.out.println("r2 is inside r1");
		}
		else if((distance > r1width - r2width/2) && (distance > r1height - r2height/2)){
			System.out.println("r2 does not overlap r1");
		}
		else{
			System.out.println("r2 overlaps r1");
		}
	}
}


29

同上

30

参考编程练习题2.8

31

import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("Enter the exchange rate from dollars to RMB:");
		double rate = input.nextDouble();
		System.out.println("Enter 0 to convert dollars to RMB and 1 vice versa:");
		int tmp = input.nextInt();
		if(tmp == 0){
			System.out.println("Enter the dollar amount:");
			double dollar = input.nextDouble();
			System.out.println("$" + dollar + " is " + dollar*rate + " yuan.");
		}
		else{
			System.out.println("Enter the RMB amount:");
			double RMB = input.nextDouble();
			System.out.println(RMB + " yuan is $" + RMB/rate);
		}
	}
}


32

套公式

33

import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter weight and price for package 1:");
		double weight1 = input.nextDouble();
		double price1 = input.nextDouble();
		System.out.print("Enter weight and price for package 2:");
		double weight2 = input.nextDouble();
		double price2 = input.nextDouble();
		double ave1 = price1/weight1;
		double ave2 = price2/weight2;
		if(ave1 > ave2)
			System.out.println("Package 2 has a better price.");
		else if(ave1 < ave2)
			System.out.println("Package 1 has a better price.");
		else
			System.out.println("Two package have the same price.");
	}
}

34

参考编程练习题3.32



你可能感兴趣的:(Java学习)