码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA语言程序设计基础课后习题第三章

时间:2014-11-13 00:20:18      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   ar   java   sp   for   

//exercise 3.1
package thirdchapterexercise1;
import java.util.Scanner;
public class first {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in =new Scanner(System.in);
        System.out.print("Enter a,b,c:");
        double a=in.nextDouble();
        double b=in.nextDouble();
        double c=in.nextDouble();
        double judge=b*b-4*a*c;
        if (judge>0){
            double x1=(-b+(Math.pow(judge, 0.5)))/(2*a);
            double x2=(-b-(Math.pow(judge, 0.5)))/(2*a);
            System.out.println("The roots are "+x1+" and "+x2);
        }
        else if (judge==0){
            double x=(-b)/(2*a);
            System.out.println("The root is "+x);
        }
        else 
            System.out.println("The equation has no real roots");
    }

}

//exercise 3.2
package thirdchapterexercise1;
import java.util.Scanner;
public class second {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in =new Scanner(System.in);
        System.out.print("Enter an integer:");
        int integer=in.nextInt();
        System.out.println("Is "+integer+" an even number?"+(integer%2==0));
    }

}


//exercise 3.3
package thirdchapterexercise1;
import java.util.Scanner;
public class third {

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


//exercise 3.4
package thirdchapterexercise1;
import java.util.Scanner;
public class fourth {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int number1=(int)(Math.random()*100);
        int number2=(int)(Math.random()*100);
        Scanner in=new Scanner(System.in);
        System.out.print("What is "+number1+" + "+number2+"?");
        int input=in.nextInt();
        System.out.println("Your answer is "+(number1+number2==input));
    }

}


//exercise 3.5
package thirdchapterexercise1;
import java.util.Scanner;
public class fifth {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int number1=(int)(Math.random()*10);
        int number2=(int)(Math.random()*10);
        int number3=(int)(Math.random()*10);
        
        Scanner in=new Scanner(System.in);
        System.out.print("What is "+number1+" + "+number2+" + "+number3+"?");
        int input =in.nextInt();
        
        System.out.println("Your answer is "+(number1+number2+number3==input));
        
    }

}


//exercise 3.6
package thirdchapterexercise1;

import java.util.Scanner;

public class sixth {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in=new Scanner(System.in);
        
        //input pounds
        System.out.print("Enter weight in pounds:");
        double weight=in.nextDouble();
        
        //input feet
        System.out.print("Enter feet:");
        int feet =in.nextInt();
                
        //input inches
        System.out.print("Enter height in inches:");
        int height =in.nextInt();
        
        //The feet and inches to meter,The pounds into kilograms
        double kilograms=weight*0.45359237;
        double meter=height*0.0254+feet*0.3048;
        
        //get BMI
        double bmi=kilograms/Math.pow(meter, 2);
        
        System.out.println("Your BMI is "+bmi);
        
        //The weight classification
        if(bmi<16)
            System.out.println("You are seriously underweight");
        else if (bmi<18)
            System.out.println("You are underweight");
        else if (bmi<24)
            System.out.println("You are normal weight");
        else if (bmi<29)
            System.out.println("Yo are overweight");
        else if (bmi<35)
            System.out.println("You are seriously overweight");
        else 
            System.out.println("You are gravely overweight");
    }

}


//exercise 3.8
package thirdchapterexercise1;
import javax.swing.JOptionPane;
public class seventh {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String number1=JOptionPane.showInputDialog("input number1:");
        String number2=JOptionPane.showInputDialog("input number1:");
        String number3=JOptionPane.showInputDialog("input number1:");
        int num1=Integer.parseInt(number1);
        int num2=Integer.parseInt(number2);
        int num3=Integer.parseInt(number3);
        int temp;
        if (num1>num2){
            temp=num1;
            num1=num2;
            num2=temp;
        }
        if(num2>num3){
            temp=num2;
            num2=num3;
            num3=temp;
        }
        if(num1>num2){
            temp=num1;
            num1=num2;
            num2=temp;
        }
        System.out.println(num1+"\t"+num2+"\t"+num3);
    }

}


//exercise 3.9
package thirdchapterexercise1;
import java.util.Scanner;
public class eighth {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in=new Scanner(System.in);
        
        System.out.print("input number:");
        String number=in.next();
        
        //字符串转化为字符数组
        char a[]=number.toCharArray();
        
        
        int b[]=new int[number.length()],sum=0;
        for(int i =0;i<number.length();i++){
            
            //将字符数组转化为数字数组
            b[i]=((int)a[i]-48)*(i+1);
            
            //sum
            sum=sum+b[i];
        }
        int d10=sum%11;
        
        //将数字转化为字符,
        char d=(char)(d10+48);
        
        //连接字符串
        number=number+d;
        
        System.out.println("ISBN is "+number);
    }

}


//exercise 3.10
package thirdchapterexercise1;

import java.util.Scanner;

public class ninth {
    public static void main(String[] args){
        int number1=(int)(Math.random()*10);
        int number2=(int)(Math.random()*10);
        
        if(number1<number2){
            int temp=number1;
            number1=number2;
            number2=temp;
        }
        
        System.out.print("What is "+number1+"+"+number2+"? ");
        Scanner in=new Scanner(System.in);
        int answer=in.nextInt();
        
        if(number1+number2==answer)
            System.out.println("You are correct!");
        else
            System.out.println("Your answer is wrong\n"+number1+"+"+number2+" sho"
                    + "uld be "+(number1+number2));
        
    }

}


//exercise 3.11
package thirdchapterexercise1;
import java.util.Scanner;
public class tenth {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in=new Scanner(System.in);
        System.out.print("please input mouth and year:");
        int mouth=in.nextInt();
        int year=in.nextInt();
        switch(mouth){
        case 1:
            System.out.println("January "+year+" has 31 days");
            break;
        case 3:
            System.out.println("March "+year+" has 31 days");
            break;
        case 4:
            System.out.println("April"+year+" has 30 days");
            break;
        case 5:
            System.out.println("May "+year+" has 31 days");
            break;
        case 6:
            System.out.println("June "+year+" has 30 days");
            break;
        case 7:
            System.out.println("July "+year+" has 31 days");
            break;
        case 8:
            System.out.println("August "+year+" has 31 days");
            break;
        case 9:
            System.out.println("September "+year+" has 30 days");
            break;
        case 10:
            System.out.println("October "+year+" has 31 days");
            break;
        case 11:
            System.out.println("November "+year+" has 30 days");
            break;
        case 12:
            System.out.println("December "+year+" has 31 days");
            break;
        default:
            break;
        }
        if(mouth==2){
            if(year%4==0&&year%100!=0||year%400==0){
                System.out.println("February "+year+" has 29 days");
                }
            else
            {
                System.out.println("February "+year+" has 20 days");
                
            }    
        }
    }

}

 

//exercise 3.12
package thirdchapterexercise2;
import java.util.Scanner;
public class first {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in=new Scanner(System.in);
        
        System.out.print("please input a integer:");
        int input=in.nextInt();
        
        if(input%5==0&&input%6==0)
            System.out.println(input+" is divisible by both 5 and 6");
        else if (input %5==0^input%6==0)
            System.out.println(input+" is dibisible by 5 or 6,but not both");
        else 
            System.out.println(input+" is not divisible by either 5 or 6");
    }

}


//exercise 3.14
package thirdchapterexercise2;
import java.util.Scanner;
public class second {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int random=(int)(Math.random()*100)%2;
        String computer;
        
        //指定random
        if (random==0){
            computer="tails";
        }
        else
            computer="heads";
            
        Scanner in=new Scanner(System.in);
        System.out.println("(please input heads of tails)");
        String input =in.next();
        if((random==1&&input.equals("heads"))||(random==0&&input.equals("tails"))){
            System.out.println("computer is "+computer);
            System.out.println("You are right!");
        }
        else {
            System.out.println("computer is "+computer);
            System.out.println("You are wrong!");
        }
    }

}


//exercise 3.15
package thirdchapterexercise2;

import java.util.Scanner;

public class third {

    public static void main(String[] args) {
        // lottery number
        int lottery=(int)(Math.random()*1000);
        
        Scanner in=new Scanner(System.in);
        
        System.out.print("Enter your lottery pick (three digits):");
        int guess =in.nextInt();
        
        //separation lottery digit
        int lotteryDigit1=lottery/100;
        int lotteryDigit2=lottery/10%10;
        int lotteryDigit3=lottery%10;
        
        //separation guess digit
        int guessDigit1=guess/100;
        int guessDigit2=guess/10%10;
        int guessDigit3=guess%10;
        
        System.out.println("The lottery number is "+lottery);
        
        //judge
        if (guess == lottery)
            System.out.println("Exact match:you win $10000");
        else if((guessDigit2==lotteryDigit1
                ||guessDigit2==lotteryDigit3)
                &&(guessDigit1==lotteryDigit2
                ||guessDigit1==lotteryDigit3)
                &&(guessDigit3==lotteryDigit1
                ||guessDigit3==lotteryDigit2))
            System.out.println("Match all digits: you win $3000");
        else if(guessDigit1==lotteryDigit1
                ||guessDigit1==lotteryDigit2
                ||guessDigit1==lotteryDigit3
                ||guessDigit2==lotteryDigit1
                ||guessDigit2==lotteryDigit2
                ||guessDigit2==lotteryDigit3
                ||guessDigit3==lotteryDigit1
                ||guessDigit3==lotteryDigit2
                ||guessDigit3==lotteryDigit3)
            System.out.println("Match one digit:you win $ 1000");
        else
            System.out.println("Sorry,no match");
    }

}


//exercise 3.16
package thirdchapterexercise2;

public class fourth {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a=(int)(Math.random()*26)+‘A‘;
        char b=(char)a;
        System.out.println(b);
        
    }

}


//exercise 3.17
package thirdchapterexercise2;
import java.util.Scanner;
public class fifth {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in=new Scanner(System.in);
        System.out.print("scissor(0),rock(1),paper(2):");
        int input=in.nextInt();
        int computer=(int)(Math.random()*3);
        if(computer==0&&input==1)
            System.out.print("The computer is scissor.You are rock.You win");
        else if (computer==0&&input==2)
            System.out.print("The computer is scissor.You are paper.You 输了.");
        else if (computer==0&&input==0)
            System.out.print("The computer is scissor.You are scissor.It is a draw.");
        
        else if (computer==1&&input==0)
            System.out.print("The computer is rock.You are scissor.You 输了.");
        else if (computer==1&&input==1)
            System.out.print("The computer is rock.You are rock.It is a draw.");
        else if (computer==1&&input==2)
            System.out.print("The computer is rock.You are paper.You win.");
        
        else if (computer==2&&input==0)
            System.out.print("The computer is paper.You are scissor.You win.");
        else if (computer==2&&input==1)
            System.out.print("The computer is paper.You are rock.You 输了.");
        else if (computer==2&&input==2)
            System.out.print("The computer is paper.You are paper.It is a draw.");
        
    }

}


//exercise 3.18
package thirdchapterexercise2;
import javax.swing.JOptionPane;
public class sixth {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String year=JOptionPane.showInputDialog("Enter a year:");
        int year1=Integer.parseInt(year);
        boolean isLeapYear=(year1%4==0&&year1%100!=0)||(year1%400==0);
        JOptionPane.showMessageDialog(null,year1+"is a leap year?"+isLeapYear);
    }

}


//exercise 3.19
package thirdchapterexercise2;
import java.util.Scanner;
public class seventh {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in=new Scanner(System.in);
        System.out.print("Enter three edges:");
        double side1=in.nextDouble();
        double side2=in.nextDouble();
        double side3=in.nextDouble();
        double max=side1,sum=0;
        
        if(max<side2){
            max=side2;
            sum=sum+side1;
        }
        else 
            sum=sum+side2;
        
        if(max<side3){
            max=side3;
            sum=sum+side2;
        }
        else 
            sum=sum+side3;
        
        System.out.println("Can edges "+side1+","+side2+" and "+side3+" form a "
                + "triangle?"+(sum>max));
        
    }

}


//exercise 3.20
package thirdchapterexercise2;

import java.util.Scanner;

public class eighth {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
        
        System.out.println("please input -58F to 41F of temperature and better than 2 of "
                + "wind speed!!");
        
        //input Fahrenheit
        System.out.print("Enter the temperature in Fahrenheit:");
        double fahreheit=in.nextDouble();
        if(fahreheit<-58||fahreheit>41){
            System.out.println("input error!!");
            System.exit(0);
        }
        //input wind speed
        System.out.print("Enter the wind speed miles per hours:");
        double windspeed=in.nextDouble();
        if (windspeed<2){
            System.out.println("input error!!");
            System.exit(0);
        }
        
        double windchillindex=35.74+0.6215*fahreheit-35.75*Math.pow(windspeed,0.16)+
                0.4275*fahreheit*Math.pow(windspeed,0.16);
        System.out.print("The wind chill index is "+windchillindex);
    }

}


//exercise 3.21
package thirdchapterexercise2;
import java.util.Scanner;
public class ninth {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in=new Scanner(System.in);
        System.out.print("Enter year:(e.g.m 2008):");
        int year=in.nextInt();
        System.out.print("Enter month : 1-12:");
        int month=in.nextInt();
        System.out.print("Enter the day of the month: 1-31:");
        int day=in.nextInt();
        
        if (month==1||month==2){
            month=month+12;
            year--;
        }
        
        int q=(day+(26*(month+1)/10)+year%100+year%100/4+year/100/4+5*year/100)%7;
        
        switch(q){
        case 0:
            System.out.println("Day of the weel is Saterday");
            break;
        case 1:
            System.out.println("Day of the weel is Sunday");
            break;
        case 2:
            System.out.println("Day of the weel is Monday");
            break;
        case 3:
            System.out.println("Day of the weel is Tuesday");
            break;
        case 4:
            System.out.println("Day of the weel is Wednesday");
            break;
        case 5:
            System.out.println("Day of the weel is Thursday");
            break;
        case 6:
            System.out.println("Day of the weel is Friday");
            break;
        }
    }

}


//exercise 3.22
package thirdchapterexercise2;
import java.util.Scanner;
public class tenth {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in=new Scanner(System.in);
        System.out.print("Enter a point with two coordinates:");
        double x=in.nextDouble();
        double y=in.nextDouble();
        double distance=Math.pow((x*x+y*y),0.5);
        
        if (distance<10){
            System.out.print("Point("+x+","+y+") is in the circle ");
        }
        else 
            System.out.print("Point("+x+","+y+") is not in the circle ");
    }

}


//exercise 3.23
package thirdchapterexercise3;
import java.util.Scanner;
public class first {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in=new Scanner(System.in);
        System.out.print("Enter a point with two coordinates:");
        double x=in.nextDouble();
        double y=in.nextDouble();
        
        //horizontal is x
        double horizontal=x;
        
        //vertical is y
        double vertical=y;
        
        if (horizontal<5&&vertical<2.5)
            System.out.print("Point("+x+","+y+") is in the rectangle");
        else 
            System.out.print("Point("+x+","+y+") is not in the rectangle");
    }

}


//exercise 3.24
package thirdchapterexercise3;

public class second {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int number1=(int)(Math.random()*13);
        int color1=(int)(Math.random()*4);
        String number=null,color=null;
        switch(number1){
        case 0:
            number="Ace";
            break;
        case 1:
            number="2";
            break;
        case 2:
            number="3";
            break;
        case 3:
            number="4";
            break;
        case 4:
            number="5";
            break;
        case 5:
            number="6";
            break;
        case 6:
            number="7";
            break;
        case 7:
            number="8";
            break;
        case 8:
            number="9";
            break;
        case 9:
            number="10";
            break;
        case 10:
            number="Jack";
            break;
        case 11:
            number="Queen";
            break;
        case 12:
            number="King";
            break;
        default:
            break;
        }
        switch(color1){
        case 0:
            color="Clubs";
            break;
        case 1:
            color="Diamond";
            break;
        case 2:
            color="Heart";
            break;
        case 3:
            color="Spades";
            break;
        }
        System.out.println("The card you picked is "+number+" of "+color);
    }

}


//exercise 3.25
package thirdchapterexercise3;

import java.util.Scanner;

public class third {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in=new Scanner(System.in);
        System.out.print("Enter three edges:");
        double side1=in.nextDouble();
        double side2=in.nextDouble();
        double side3=in.nextDouble();
        double max=side1,sum=0;
        
        //find max and sum of two side
        //Judge whether existence of triangle
        if(max<side2){
            max=side2;
            sum=sum+side1;
        }
        else 
            sum=sum+side2;
        
        if(max<side3){
            max=side3;
            sum=sum+side2;
        }
        else 
            sum=sum+side3;
        
        //calculate the perimeter
        double perimeter=sum+max;
        if(sum<max||sum==max)
            System.out.println("input is wrong");
        else
            System.out.print("sum of three side is:"+perimeter);
    }

}

//exercise 3.27
package thirdchapterexercise3;
import java.util.Scanner;
public class fourth {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in=new Scanner(System.in);
        System.out.print("Enter a point‘s x- and y-coordinates:");
        double x=in.nextDouble();
        double y=in.nextDouble();
        double distance1=Math.pow((x*x+y*y), 0.5);
        double distance2=Math.pow((x-200)*(x-200)+(y-100)*(y-100),0.5);
        if(distance1>distance2||x>200||x<0||y>100||y<0)
            System.out.print("The point is not in the triangle");
        else 
            System.out.print("The point is in the triangle");
    }

}


//exercise 3.28
package thirdchapterexercise3;
import java.util.Scanner;
public class fifth {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in =new Scanner(System.in);
        System.out.println("Enter r1‘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.println("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();
        
        double insidelimitwidth=Math.abs(w1-w2);
        double insidelimitheight=Math.abs(h1-h2);
        
        double insidejudgewidth=Math.abs(x2-x1);
        double insidejudgeheight=Math.abs(y2-y1);
        
        
        double overlapslimitwidth=w1+w2;
        double overlapslimitheight=h1+h2;
        
        double overlapsjudgewidth=Math.abs(x2-x1);
        double overlapsjudgeheight=Math.abs(y2-y1);
        
        if (insidejudgewidth<insidelimitwidth&&insidejudgeheight<insidelimitheight)
            System.out.println("r2 is inside r1");
        else if (overlapsjudgewidth<overlapslimitwidth&&overlapsjudgeheight<overlapslimitheight)
            System.out.println("r2 overlaps r1");
        else 
            System.out.println("r2 does not overlap r1");
        
    }

}


//exercise 3.29
package thirdchapterexercise3;
import java.util.Scanner;
public class sixth {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in=new Scanner(System.in);
        System.out.println("Enter circle1‘s center x-,y-coordinates,and radius:");
        double x1=in.nextDouble();
        double y1=in.nextDouble();
        double r1=in.nextDouble();
        System.out.println("Enter circle2‘s center x-,y-coordinates,and radius:");
        double x2=in.nextDouble();
        double y2=in.nextDouble();
        double r2=in.nextDouble();
        
        double distance=Math.pow((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2),0.5);
        
        double insidelimit=Math.abs(r1-r2);
        double overlapslimit=r1+r2;
        
        if(distance<insidelimit)
            System.out.println("circle2 is inside circle1");
        else if (distance<overlapslimit)
            System.out.println("circle2 overlaps circle1");
        else 
            System.out.println("circle2 does not overlap circle1");
        
        
    }

}

 

JAVA语言程序设计基础课后习题第三章

标签:des   style   blog   io   color   ar   java   sp   for   

原文地址:http://www.cnblogs.com/chenqy253/p/4093758.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!