一.三角形
(1)问题描述
输入三个整数a、b、c,分别作为三角形的三条边,通过程序判断这三条边是否能构成三角形?如果能构成三角形,则判断三角形的类型(等边三角形、等腰三角形、一般三角形)。
(2)介绍
编程工具:idea
编程语言:java
使用平台:Windows10
(3)代码
package 作业; import java.util.Scanner; public class sanjiaoxin1 { public static void sanjiaoxin(int a,int b,int c) { if (a > 0 && b > 0 && c > 0 && a + b > c && a + c > b && b + c > a){ if (a == b && a == c && b == c) { System.out.print("是一个等边三角形"); } else if (a == b || a == c || b == c) { System.out.print("是一个等腰三角形"); } else { System.out.print("是一个一般三角形"); } }else { System.out.print("不能构成三角形"); } } public static void main(String[] args){ int a; int b; int c; System.out.println("请输入三角形的三边: "); Scanner scanner=new Scanner(System.in); a=scanner.nextInt(); b=scanner.nextInt(); c=scanner.nextInt(); sanjiaoxin(a,b,c); } }
2.三角形问题结构化实现
(1)问题描述
输入三个整数a、b、c,分别作为三角形的三条边,通过程序判断这三条边是否能构成三角形?如果能构成三角形,则判断三角形的类型(等边三角形、等腰三角形、一般三角形)。要求输入三个整数a、b、c,必须满足以下条件:1≤a≤200;1≤b≤200;1≤c≤200。
(2)介绍
编程工具:idea
编程语言:Java
使用平台:Windows10
(3)代码
package 作业; import java.util.Scanner; public class sanjiaoxin1 { public static void sanjiaoxin(int a,int b,int c) { if(a<1||a>200){ System.out.println("请输入合法的a"); }else if(b<1 || b>200){ System.out.println("请输入合法的b"); }else if(c<1 || c>200){ System.out.println("请输入合法的c"); }else if (a > 0 && b > 0 && c > 0 && a + b > c && a + c > b && b + c > a){ if (a == b && a == c && b == c) { System.out.print("是一个等边三角形"); } else if (a == b || a == c || b == c) { System.out.print("是一个等腰三角形"); } else { System.out.print("是一个一般三角形"); } }else { System.out.print("不能构成三角形"); } } public static void main(String[] args){ int a; int b; int c; System.out.println("请输入三角形的三边: "); Scanner scanner=new Scanner(System.in); a=scanner.nextInt(); b=scanner.nextInt(); c=scanner.nextInt(); sanjiaoxin(a,b,c); } }
3.NextData函数实现
(1)问题描述
NextDate() 是整型变量 month, day 和 year 的函数,输入 1812-2012 年期间的某一日期的 month, day 和 year 的值,输出这一天的下一天的日期的 month, day 和 year 值。
(2)介绍
编程工具:idea
编程语言:java
使用平台:Windows10
(3)代码
package 作业; import java.util.Scanner; public class LaterDay { public static void NextDate(int year, int month, int day) { if (year % 4 == 0 && year % 100 != 0 && month == 2 && day == 29) {//当为润年的时候,二月有29天 month = month + 1; day = 1; System.out.printf("%d", year); System.out.printf("年"); System.out.printf("%d", month); System.out.printf("月"); System.out.printf("%d", day); System.out.printf("日"); } else if (month == 2 && day == 28) {//当该年为平年的时候,二月为28天,输出日期后一天,月份加1,将具体几号置为1 month = month + 1; day = 1; System.out.printf("%d", year); System.out.printf("年"); System.out.printf("%d", month); System.out.printf("月"); System.out.printf("%d", day); System.out.printf("日"); } else if (month == 12 && day == 31) {//当输入月份为12月31日此情况下要输出后一天则要将其年份加1,月份和日期号置为1 year = year + 1; month = 1; day = 1; System.out.printf("%d", year); System.out.printf("年"); System.out.printf("%d", month); System.out.printf("月"); System.out.printf("%d", day); System.out.printf("日"); } //当输入月大,日子为最大31号,则执行月份+1,日期号置为1 else if (month == 1 && day == 31 || month == 3 && day == 31 || month == 5 && day == 31 || month == 7 && day == 31 || month == 8 && day == 31 || month == 10 && day == 31) { month = month + 1; day = 1; System.out.printf("为什么输出"); System.out.printf("%d", year); System.out.printf("年"); System.out.printf("%d", month); System.out.printf("月"); System.out.printf("%d", day); System.out.printf("日"); } else if (month == 4 && day == 30 || month == 6 && day == 30 || month == 9 && day == 30 || month == 11 && day == 30) { month = month + 1; day = 1; System.out.printf("%d", year); System.out.printf("年"); System.out.printf("%d", month); System.out.printf("月"); System.out.printf("%d", day); System.out.printf("日"); } else { day = day + 1; System.out.printf("%d", year); System.out.printf("年"); System.out.printf("%d", month); System.out.printf("月"); System.out.printf("%d", day); System.out.printf("日"); } } public static void ErrorTip(int i) {//当输入年份月份日期号不正确的时候给出的提示 System.out.println("输入的整数不合法"); } public static void main(String[] args) {//主函数 Scanner scan = new Scanner(System.in); System.out.println("请输入年份"); int year = scan.nextInt(); if (year > 1998 && year < 2051) { System.out.println("请输入月份"); } else { ErrorTip(year); } int month = scan.nextInt(); if (month > 0 && month < 13) { System.out.println("请输入具体几号"); } else { ErrorTip(month); } int day = scan.nextInt();//输入天数 if (year % 4 == 0 && year % 100 != 0 && month == 2)//当输入的年份该年为润年的情况下 { if (day > 0 && day < 30) NextDate(year, month, day); else ErrorTip(day); } else if (month == 2) {//输入的该年为平年的情况如下 if (day > 0 && day < 29) NextDate(year, month, day); else ErrorTip(month); } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {//月大则一个月有31天 if (day > 0 && day < 32) NextDate(year, month, day); else ErrorTip(day); } else {//月小则一月有30天二月份除外 if (day > 0 && day < 31) NextDate(year, month, day); else ErrorTip(day); } scan.close(); } }
4.佣金问题实现
(1)问题描述
前亚利桑那洲境内的一位步枪销售商销售密苏里州制造商制造的步枪机(lock)、枪托(stock)和枪管(barrel)。枪机卖45美元,枪托卖30美元,枪管卖25美元。销售商每月至少要售出一支完整的步枪,且生产限额是大多数销售商在一个月内可销售70个枪机、80个枪托和90个枪管。
根据当月的销售情况,并计算销售商的佣金如下:
①不到(含)1000美元的部分为10%;
②1000(不含)~1800(含)美元的部分为15%;
③超过1800美元的部分为20%。
佣金程序生成月份销售报告,汇总售出的枪机、枪托和枪管总数,销售商的总销售额以及佣金。
(2)介绍
编程工具:idea
编程语言:java
使用平台:Windows10
(3)代码
package 作业; import java.util.Scanner; public class yongjin { public static void main(String[] args) { double lockprice = 45; double stockprice = 30; double barrelprice = 25; double totallocks = 0; double totalstocks = 0; double totalbarrels = 0; double commission; Scanner input = new Scanner(System.in); System.out.print("Input the locks,stocks and barrels:"); int locks = input.nextInt(); if (locks == -1) { System.out.print("当⽉销售活动结束!\n"); } if (locks != -1) { int stocks = input.nextInt(); int barrels = input.nextInt(); if (1 > locks || locks > 70) { System.out.println("对不起,枪机数不能为负数且⼚商限制⼀个⽉只能卖出少于70个!"); } if (1 >= stocks || locks >= 80) { System.out.println("对不起,枪托数不能为负数且⼚商限制⼀个⽉只能卖出少于80个!"); } if (1 >= barrels || barrels >= 90) { System.out.println("对不起,枪管数不能为负数且⼚商限制⼀个⽉只能卖出少于90个!"); } totallocks = totallocks + locks; totalstocks = totalstocks + stocks; totalbarrels = totalbarrels + barrels; System.out.print("Locks sold:" + totallocks + "\nStocks sold:" + totalstocks + "\nbarrels sold:" + totalbarrels + "\n"); } double locksales = lockprice * totallocks; double stocksales = stockprice * totalstocks; double barrelsales = barrelprice * totalbarrels; double sales = locksales + stocksales + barrelsales; System.out.print("total sales:" + sales); if (sales > 1800) { commission = 0.10 * 1000; commission = commission + 0.15 * 800; commission = commission + 0.20 * (sales - 1800); } else if (sales > 1000) { commission = 0.10 * 1000; commission = commission + 0.15 * (sales - 1000); } else commission = 0.10 * sales; System.out.print("commission is $:" + commission); } }