Java第三章练习题

练习题:

Java第三章练习题_第1张图片

代码如下:

import java.util.Scanner;

public class Day31 {

	public static void main(String[] args) {
		double a,b,c;
		System.out .print("Enter a,b,c :");
		Scanner in = new Scanner(System.in);
		a = in.nextDouble();
		b = in.nextDouble();
		c = in.nextDouble();
		double d = Math.pow(1.0*b*b-4*a*c, 0.5);
		double r1 = (-b+d)/2*a;
		double r2 = (-b-d)/2*a;
		
		if (d > 0) {
			double x = 1.0e6;
			r1 = (int)( r1*x)/x;
			r2 = (int)(r2*x)/x;
			System.out.print("The equation has two roots "+r1+"and"+r2);
		}
		else if (d == 0) {
			System.out.print("The equation has one roots "+(int)r1);
		}
		else {
			System.out.print("The equation has no real roots ");
		}
		
	}

}

Java第三章练习题_第2张图片

代码如下:

import java.util.Scanner;

public class Day32 {

	public static void main(String[] args) {
		
		double a,b,c,d,e,f;
		System.out .println("Enter a,b,c,d,e,f : ");
		Scanner in = new Scanner(System.in);
		a = in.nextDouble();
		b = in.nextDouble();
		c = in.nextDouble();
		d = in.nextDouble();
		e = in.nextDouble();
		f = in.nextDouble();
		double x = (e * d - b * f) / (a * d - b * c);
		double y = (a * f - e * c) / (a * d - b * c);
		if (a * d == b * c) {
			System.out.print("The equation has no solution");
		}else {
			System.out .print("x is " + x +" and y is" +y );
		}

	}

}

3.3

Java第三章练习题_第3张图片

代码如下:

import java.util.Scanner;
		public class Day33 { 	public static void main(String[] args) {
			Scanner in = new Scanner (System.in);
			System.out.print("今天是周几?0为周日...");
			int today = in.nextInt();
			System.out.print("输入未来几天?");
			int future = in.nextInt();	
			int futureday = (today+future)%7;
			String todaystr = "";//定义今天的星期,将数字转换为星期
			if (today == 0 ) todaystr = "周日";
			else if (today == 1)todaystr = "周一";
			else if (today == 2)todaystr = "周二";
			else if (today == 3)todaystr = "周三";
			else if (today == 4)todaystr = "周四";
			else if (today == 5)todaystr = "周五";
			else if (today == 6) todaystr = "周六";
			String futerstr = "";//定义未来的星期,将数字转换为星期
			if (futureday == 0 ) futerstr = "周日";	
			else if (futureday == 1)futerstr = "周一";
			else if (futureday == 2)futerstr = "周二";
			else if (futureday == 3)futerstr = "周三";
			else if (futureday == 4)futerstr = "周四";
			else if (futureday == 5)futerstr = "周五";
			else if (futureday == 6)futerstr = "周六";	
			System.out.print("今天是"+todaystr+" "+future+"天后是"+futureday);		
	}

}

Java第三章练习题_第4张图片

代码如下:

import java.util.Scanner;
class Demo03_04{
    public static void main(String[] args){
        //1.输入一个数字
        Scanner scanner=new Scanner( System.in);
        System.out.print("请输入一个数字:");
        int num=scanner.nextInt();
        int temp=num;
        //2.拼接出该数字的反序
        int sum=0;
        sum=sum*10+num%10;
        num/=10;
        sum=sum*10+num%10;
        num/=10;
        sum=sum*10+num%10;
        num/=10;
        sum=sum*10+num%10;
        num/=10;
        sum=sum*10+num%10;
        num/=10;
        sum=sum*10+num%10;
        num/=10;
        if(sum==temp){
            System.out.println("是回文");
        }else{
            System.out.println("不是回文");
        }
    }
}


Java第三章练习题_第5张图片

代码如下:

import java.util.Scanner;
	public class Day35 { 	public static void main(String[] args) {
		Scanner in = new Scanner (System.in);
		int num=(int)(Math.random()*100);//取一个随机数
		System.out.println("输入两位数字");//提示用户输入
		int c;
		int k = 0;
		c = in.nextInt();
		int e,f;
		if (c<0) //如果是10以内的第一位是0
			{	
			e = 0;			f = c;	
			}else {	
				f = c%10;
				c/=10;
				e = c;
				}//进行第一位和第二位数字的赋值
		int a,b;
		if (num<10)		{
			a = 0;
			b = num;
			}else {	
				b 	 = num%10;
				num/=10;
				a = num;
				}if (a==e&&b==f) 
					System.out.println("10000");//一等奖
				else if (a == f &&b==e)
					System.out.println("3000");//二等奖
				else if (a==e||a==f||b==e||b==f)
					System.out.println("1000");//三等奖
				else 			System.out.println("没中奖");	
			}
	}

Java第三章练习题_第6张图片

其代码为:

import java.util.*;
public class Day36 { 	public static void main(String[] args) {
	Random random=new Random();//定义随机数对象
	int a = random.nextInt(3);//产生2以内的随机数
	Scanner in  = new Scanner (System.in);	
	System.out.println("输入0/1/2分别代表石头剪刀布");//提示用户输入
	int b = in.nextInt();//读取数据	
	if (a==0)		//判断哪个人赢了		
		{			if (b==0)		
			System.out.println("我是石头你是石头平了");	
		if (b==1)			
			System.out.println("我是石头你是剪刀你输了");	
		if (b==2)		
			System.out.println("我是石头你是布你赢了");	
		}		if (a==1)		{	
			if (b==0)			
				System.out.println("我是剪刀你是石头你赢了");	
			if (b==1)		
				System.out.println("我是剪刀你是剪刀平了");	
			if (b==2)		
				System.out.println("我是剪刀你是布你输了");	
			}		if (a==2)		{		
				
				if (b==0)	
					System.out.println("我是布你是石头你输了");
				if (b==1)		
					System.out.println("我是布你是剪刀你赢了");	
				if (b==2)
					
					System.out.println("我是布你是布平了");	
				} 
			} 
}

Java第三章练习题_第7张图片

其代码为:

import java.util.Scanner;
	public class Day38 {	public static void main (String [] args) {
	Scanner in = new Scanner (System.in);	
	System.out.println("输入坐标点");//提示用户输入
	double x = in.nextDouble();//读取坐标	
	double y = in.nextDouble();		
	double xo = 0;//定义圆心	
	double yo = 0;	
	double radius = 10;//定义半径	
	double distance =Math.sqrt( Math.pow(x-xo, 2)+Math.pow(y-yo, 2));
	if (distance == radius) //距离相等
		System.out.println("在圆上");
	else if (distance > radius) //在圆外
		System.out.println("在圆外");	
	else //在圆内	
		System.out.println("在圆内");		
	}

}

 

你可能感兴趣的:(Java第三章练习题)