• 第五次作业 第三章例题


    3-1
    import java.util.Scanner; public class Add { public static void main(String[] args) { int number1=(int)(System.currentTimeMillis()%10);//随机产生第一个整数 int number2=(int)(System.currentTimeMillis()/7%10);//随机产生第二个整数 Scanner input=new Scanner(System.in); System.out.println("What is" + number1 + "+" + number2 + "?"); int answer=input.nextInt();//得到答案 System.out.println(number1 + "+" + number2 + "=" + answer + "is" + (number1+number2==answer));//用布尔表达式给答案打分 } }

      

     3-2

    import java.util.Scanner;
    public class Simple {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner input =new Scanner(System.in);
    		System.out.println("Enter an integer: ");//输入一个整数
    		int number=input.nextInt();
    		if (number % 5 == 0)
    			System.out.println("HiFive");//如果能被5整除就显示HiFive
    		if (number % 2 == 0)
    			System.out.println("HiEven");//如果能被2整除就显示HiEven
    		
    	}
    
    }
    

      

    3-3

    import java.util.Scanner;
    public class Sub {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		int number1 = (int)(Math.random() * 10);
    		int number2 = (int)(Math.random() * 10);//随机产生两个一位数的整数
    		if(number1<number2) {
    			int temp=number1;
    			number1=number2;
    			number2=temp;//如果number1>number2,交换number1和number2
    		}
    		System.out.println("Whis is" + number1 + "-" + number2 + "?");
    		Scanner input = new Scanner(System.in);
    		int answer = input.nextInt();
    		if (number1 -number2 == answer)//检查输入的答案,并显示答案是否正确
    			System.out.println("You are correct.");
    		else {
    			System.out.println("Your answer is wrong.");
    			System.out.println(number1 + "-" + number2 +" should be " + (number1-number2));
    		}
    	}
    
    }
    

      

    3-4 

    import java.util.Scanner;
    public class Com {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner input = new Scanner(System.in);
    		System.out.print("Enter weight in kilograms:");//提示用户输入以千克为单位的体重
    		double weight = input.nextDouble();
    		System.out.print("Enter height in meters:");//提示用户输入以米为单位的身高
    		double height = input.nextDouble();
    		double bmi = weight / (height * height);//计算BMI
    		System.out.println("BMI is " + bmi);
    		if (bmi < 18.5)
    			System.out.println("Underweight");//体重过轻
    		else if (bmi < 25)
    			System.out.println("Normal");//标准
    		else if (bmi < 30)
    			System.out.println("Overweight");//过重
    		else
    			System.out.println("Obese");//肥胖
    	}
    
    }
    

      

    3-5

    import java.util.Scanner;
    public class ComputeTax {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner input = new Scanner(System.in);
            System.out.print("Enter the taxable income: ");//提示用户输入收入
            double income = input.nextDouble();
            double tax =0;
            if (income <= 8350)//用分支语句计算不同收入应征税
                tax = income * 0.10;
            else if (income <= 33950)
                tax = 8350 * 0.10 + (income - 8350) * 0.15;
            else if (income <= 82250)
                tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
            else if (income <= 171550)
                tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (income - 82250) * 0.28;
            else if (income <= 372950)
                tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (income - 171550) * 0.33;
            else
                tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (372950 - 171550) * 0.33 + (income - 372950) * 0.35;
            System.out.println("Tax is " + (int)(tax * 100)/100.0);
        }
    }

    3-6

    import java.util.Scanner;
    public class Test {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner input = new Scanner(System.in);
    		System.out.print("Enter an integer: ");
    		int number = input.nextInt();
    		if (number % 2 == 0 && number % 3 == 0)
    			System.out.println(number + " is divisible by 2 and 3.");//检验一个数是否能同时被2和3整除
    		if (number % 2 == 0 || number % 3 ==0)
    			System.out.println(number + " is divisible by 2 or 3.");//检验一个数是否能被2或3整除
    		if (number % 2 == 0 ^ number % 3 == 0)
    			System.out.println(number + " is divisible by 2 or 3,but not both.");//检验一个数是否能被2或3整除,但不能同时被这两个数整除
    	}
    
    }
    

      

    3-7

    import java.util.Scanner;
    public class Leap {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner input = new Scanner(System.in);
            System.out.println("Enter a year: ");
            int year = input.nextInt();
            boolean isLeapYear =(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);//如果某年可以被4整除而不能被100整除,或者可以被400整除,那么这一年就是闰年200
            System.out.println(year + " is a leap year? " + isLeapYear);
        }
    
    }

    3-8

    import java.util.Scanner;
    public class Lottery {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int lottery =(int)(Math.random() * 100);
            Scanner input = new Scanner(System.in);
            System.out.print("Enter your lottery pick (two digits): ");
            int guess = input.nextInt();//用户输入一个猜测的数值
            int lottery1 = lottery / 10;//得到lottery的十位数的数值
            int lottery2 = lottery % 10;//得到lottery的个位数的数值
            int guess1 = guess / 10;
            int guess2 = guess % 10;
            System.out.println("The lottery number is " + lottery);
            if (guess == lottery)
                System.out.println("Exact match: you win $10,000");
            else if (guess2 == lottery1 && guess1 == lottery2)
                System.out.println("Exact match: you win $3,000");
            else if (guess1 == lottery1 || guess1 == lottery2 || guess2 == lottery1 || guess2 == lottery2)
                System.out.println("Exact match: you win $1,000");
            else
                System.out.println("Sorry, no match.");
        }
    
    }

    3-9

    import java.util.Scanner;
    public class Zodia {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner input = new Scanner(System.in);
            System.out.print("Enter a year: ");
            int year = input.nextInt();//输入年份
            switch (year %12) {
            case 0:System.out.println("monkey");break;
            case 1:System.out.println("rooster");break;//
            case 2:System.out.println("dog");break;
            case 3:System.out.println("pig");break;
            case 4:System.out.println("rat");break;//
            case 5:System.out.println("ox");break;//
            case 6:System.out.println("tiger");break;
            case 7:System.out.println("rabbit");break;//
            case 8:System.out.println("dragon");break;
            case 9:System.out.println("snake");break;
            case 10:System.out.println("horse");break;//
            case 11:System.out.println("sheep");//
            }
        }
    
    }

  • 相关阅读:
    删除排序链表中的重复元素
    动态规划之爬楼梯问题
    外行小白的modbus惊心之旅
    SSM框架整合
    我与404的不解之缘系列(一)记录一个智障一般的错误
    day 06 作业
    day 06 文件查看查找与上传下载
    linux 防火墙查看firewall
    linux 变量详情
    day05作业练习题
  • 原文地址:https://www.cnblogs.com/xyayy/p/7706763.html
Copyright © 2020-2023  润新知