(題目難易程度與題號順序無關哦)
目錄
1、多關鍵字排序
2、集合類的綜合應用問題
3、數組排序
4、球的相關計算問題
5、利用類對象計算日期
6、日期計算問題
7、星期日期的計算
8、計算坐標平面上兩點距離
9、異常處理設計問題
10、Java源文件和字節碼文件名稱的判斷問題
11、函數表達式的計算
12、成績的學分績點計算
1、多關鍵字排序
? ? ?學生考試成績的統計利用計算機軟件將變得十份簡單的事,由于你很勤奮,并且剛學習了面向對象程序設計Java,在Java中提供了面向對象程序設計方法,并提供了大量有用的工具類,解決該問題你將成為能手,我們知道只要利用容器 Collections.sort()方法,很方便解決對象的排序問題,如果一個年級的學生參加了有語文、數學和英語三科目的考試,需要計算出每個學生的總分和平均成績,并且要按照總分排序,如果總分相同,則依照語文、數學再英語的成績排序,即排序關鍵詞依次為總分、語文、數學、英語。請你設計一個程序解決該問題。
? ? 提示:利用輸入數據的每行創建一個Student對象,將所有學生對象存儲到一個List<Student> stulist = new ArrayList<Student>();線性表中,再利用Collections.sort(stulist,new MyComptor());就解決排序問題,然后將stulist中的每個學生輸出,就完成該排序問題了,但為了能排序,Collections必須知道對象排序規則,要定義一個規則,是通過設計一個比較器完成,比較器的主體如下:
? ? class MyComptor implements Comparator<Object> { ?
? ??
? ? ? ?public int compare(Object o1, Object o2) {
? ? ? ? Student stu1 =(Student)o1;
? ? ? ? Student stu2 =(Student)o2;
? ? ? ? if(...){//填寫比較規則
? ? ? ? ? ? return 1;
? ? ? ? }else{
? ? ? ? ? ? return 0;
? ? ? ? }
? ? ?}
? ?}
? ?下面是一個點對象排序的參考實例:
? ? ? ? Point point2 = new Point(2,2,2);
? ? ? ? Point point1 = new Point(1,1,1);
? ? ? ? Point point3 = new Point(3,1,2);
? ? ? ?
? ? ? ? List<Point> points = new ArrayList<Point>();
? ? ? ? points.add(point2);
? ? ? ? points.add(point1);
? ? ? ? points.add(point3); ? ? ??
? ? ??
? ? ? ? //根據point中的升序輸出
? ? ? ? Collections.sort(points, new SortByXdesc());
? ?SortByXdesc對象的定義如下:
? ?public class SortByXdesc implements Comparator<Object> {
? ? //根據point中的x降序輸出
? ? @Override
? ? public int compare(Object o1, Object o2) {
? ? ? ? Point point1 =(Point)o1;
? ? ? ? Point point2 =(Point)o2;
? ? ? ? if(point1.getX()>point2.getX()){
? ? ? ? ? ? return 1;
? ? ? ? }else{
? ? ? ? ? ? return 0;
? ? ? ? }
? ? }
}
標準輸入:
? ?第一行為一個正整數N,表示該年紀共有的學生數,接下來的N行,每行為一個學生的信息,依次為學號、班級、語文成績、數學成績和英語成績,其中學號為10個字符的字符串,班級和成績為正整數,他們之間由一個空格隔開。
標準輸出:
? ?輸出該年級學生的成績單,即根據總分和語文、數學、英語成績為次關鍵詞的排序后的成績單,每行輸出一個學生的成績,使用一個空格隔開,依次輸出如下數據:
學號 班級 語文 數學 英語 總分 平均
其中平均成績四舍五入保留2位小數。
測試用例輸入:
4
0806401001 ? ?1 ? ? 56 ? ?64 ? ?77
0806401002 ? ?1 ? ? 75 ? ?68 ? ?54
0806401003 ? ?1 ? ? 68 ? ?79 ? ?76
0806401004 ? ?1 ? ? 56 ? ?57 ? ?84
測試用例輸出:
0806401003 1 68 79 76 223 74.33
0806401002 1 75 68 54 197 65.67
0806401001 1 56 64 77 197 65.67
0806401004 1 56 57 84 197 65.67
本題代碼答案:
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;class AppForScoreSort {public static void main(String[] args) {Scanner sc=new Scanner(System.in);int n=sc.nextInt();Student[] stu=new Student[n+1];for(int i=1;i<=n;i++){String id=sc.next();int cs=sc.nextInt();int x1=sc.nextInt();int x2=sc.nextInt();int x3=sc.nextInt();int total=x1+x2+x3;double average=(double)total/3;//實例化學生對象stu[i]=new Student(id,cs,x1,x2,x3,total,average);}Comparator<Student> cmp=new MyComparator();Arrays.sort(stu,1,1+n,cmp);for(int i=1;i<=n;i++){System.out.println(stu[i].toString());}}
}class Student{//學號String id;//cs為所在班級int cs;//x1,x2,x3分別對應了語,數,英int x1,x2,x3;//總分int total;//平均分double average;//創建有參構造器public Student(String id, int cs, int x1, int x2, int x3, int total, double average) {this.id = id;this.cs = cs;this.x1 = x1;this.x2 = x2;this.x3 = x3;this.total = total;this.average = average;}//每個學生的輸出語句@Overridepublic String toString() {return id+" "+cs+" "+x1+" "+x2+" "+x3+" "+total+" "+String.format("%.2f",average);}
}//自定義比較方法
class MyComparator implements Comparator<Student>{@Overridepublic int compare(Student a, Student b) {if(b.total>a.total)return 1;else if(b.total<a.total)return -1;else if(b.x1>a.x1)return 1;else if(b.x1<a.x1)return -1;else if(b.x2>a.x2)return 1;else if(b.x2<a.x2)return -1;else if(b.x3>a.x3)return 1;else if(b.x3<a.x3)return -1;return 0;}
}
2、集合類的綜合應用問題
問題描述:
? ? ? 在實際項目開發中需要存儲各種類型的對象數據,一般使用數組存儲存在存儲個數不確定問題,通常使用集合類型來存儲。給你的問題是,在學生信息系統中一個院系有班級信息和學生信息,一個班級有多個學生,每個學生屬于一個班,在信息查詢中往往需要進行某學生在那個班,該同學有那些同班同學等相關查詢,如果使用集合合理存儲了班級和學生對象,進行相關查詢將變得容易。
? ? ? 本問題使用標準輸入班級信息和學生信息,然后查出給定某同學的班級,以及該班同學人數和學號最前、最后的學生學號。
輸入:
? ? ? ? 標準輸入,輸入的第一行為兩個正整數N、M,N表示接下來的N行為班級信息,每行由班級編號、班級名稱、入學年級,數據之間為一個逗號","分格;班級信息后M行為學生信息,學生信息的每行依次為學號、所在班級編號、姓名、性別、入學年構成,數據之間為一個逗號","分格。學生數據之后的所有行為學生學號數據,需要你完成查找的學生,每個學生查找信息使用一行輸出。
輸出:
? ? ? ?標準輸出,根據需要你查找的學生的學號使用一行輸出該學生所在的班級編號和該班學生個數,并輸出該班學號排序的第一個和最后一個學生的學號,中間使用一個空格隔開。如果該班只有一個學生,輸出第一個學生學號,如果該該學號沒有學生,輸出”NO Student“。
輸入樣列:
5 11
2006405,5,2020
2006408,8,2020
2106402,2,2021
2106405,5,2021
2106406,6,2021
2000130841,2006408,楊凱,男,2020
2000130842,2006408,楊石磊,男,2020
2000130849,2006408,劉坤艷,男,2020
2000130501,2006405,文吉鑫,男,2020
2000130502,2006405,方大衛,男,2020
2000130503,2006405,伍一帆,男,2020
2000130504,2006405,趙天祺,男,2020
2000130505,2006405,易佳豪,男,2020
2000130506,2006405,歐陽哲浩,男,2020
2000130510,2006405,陳吉濤,男,2020
2000130511,2006405,曹毅凡,男,2020
2000130506
2000130508
2000130842
輸出樣列:
2006405 8 2000130501 2000130511
NO Student
2006408 3 2000130841 2000130849
本題代碼答案:
import java.util.*;public class AppForset {public static void main(String[] args) {Scanner in=new Scanner(System.in);Map<String,Classes> claMap=new HashMap<String,Classes>();Map<String,Student>stuMap=new HashMap<String,Student>();int N,M;N=in.nextInt();M=in.nextInt();in.nextLine();for (int i=0;i<N;i++){String claline=in.nextLine();String[] datas=claline.split(",");Classes cla=new Classes(datas[0],datas[1],Integer.parseInt(datas[2]));claMap.put(datas[0],cla);}for(int i=0;i<M;i++){String stuline=in.nextLine();String[] datas=stuline.split(",");Classes cla=claMap.get(datas[1]);Student stu=new Student(datas[0],datas[2],datas[3],Integer.parseInt(datas[4]),cla);cla.stuSet.add(stu);stuMap.put(stu.stuid,stu);}while (in.hasNextLine()){String stuid=in.nextLine();Student stu=stuMap.get(stuid);if(stu==null)System.out.println("NO Student");else{Classes cla=stu.classes;Student[] stus=(Student[]) cla.stuSet.toArray(new Student[cla.stuSet.size()]);if(stus.length==1){System.out.println(cla.classId+" "+stus.length+" "+stu.stuid);}else{Arrays.sort(stus);System.out.println(cla.classId+" "+stus.length+" "+stus[0].stuid+" "+stus[stus.length-1].stuid);}}}}
}
class Classes{String classId;String classname;int grade;Set<Student> stuSet=null;public ?Classes(String classId,String Classname,int grade){this.classId=classId;this.classname=classname;this.grade=grade;stuSet=new HashSet<Student>();}
}
class Student implements Comparable{String stuid;String name;String sex;int grade;Classes classes;public ?Student(String stuid,String name,String sex,int grade,Classes classes){this.stuid=stuid;this.name=name;this.sex=sex;this.grade=grade;this.classes=classes;}@Overridepublic int compareTo(Object o) {Student stu=(Student)o;return this.stuid.compareTo(stu.stuid);}
}
3、數組排序
描述
? ? 給定一組n個正整數,根據各位數字之和從小到大進行排序。
輸入
? ? 輸入數據有多組,每組數據占一行,每行的第一個數正整數n,表示整數個數,后面接n個正整數。當n為0時,不作任何處理,輸入結束。
輸出
? ? 輸出每組要求下原始數據排序的結果。
樣例輸入
2 1 2
3 121 10 111
0
樣例輸出
1 2
10 111 121
本題代碼答案:
import java.util.Scanner;
public class ArraySort {public static void main(String[] args) {Scanner read=new Scanner(System.in);while(true){int n;n=read.nextInt();if(n==0)break;int[] a;int[] b;a=new int[n];b=new int[n];for(int i=0;i<n;i++){a[i]=read.nextInt();int x,sum=0;x=a[i];while(x!=0){sum+=x%10;x/=10;}b[i]=sum;}for(int i=0;i<n;i++){for(int j=i+1;j<n;j++){if(b[i]>b[j]){b[i]=b[i]^b[j];b[j]=b[i]^b[j];b[i]=b[i]^b[j];a[i]=a[i]^a[j];a[j]=a[i]^a[j];a[i]=a[i]^a[j];}}}for(int i=0;i<n;i++) System.out.print(a[i]+" ");System.out.println();}}
}
4、球的相關計算問題
????????球的相關計算涉及到球的體積和球的表面積以及過直徑的截面積等,我們知道,以上計算僅需要知道球的半徑就可以利用計算公式計算出來,計算中用到的圓周率使用3.14進行計算。
? ?輸入:
? ? ? ?標準輸入一個浮點數r(r>0),代表需要計算的球的半徑.
? ?輸出:
? ? ? ?標準輸出,根據輸入的球半徑r,依次輸出球表面積、圓球體積和直徑的側面積大小,數據間由一個空格隔開,結果保留2位有效數字。
? ?測試用例輸入:
? ?3
? ?測試用例輸出:
? ?113.04 113.04 28.26
本題代碼答案:
import java.util.Scanner;
public class Ball {public static void main(String args[]) {double pi=3.14;Scanner in = new Scanner(System.in);double r = in.nextDouble();double s = 4*pi * r * r;double v = 4/3.0*pi*r*r*r;double sc = pi * r * r;System.out.printf("%.2f ",s);System.out.printf("%.2f ",v);System.out.printf("%.2f",sc);}
}
5、利用類對象計算日期
????????在利用Java語言進行信息系統開發中,經常需要對日期進行計算和轉換,比如,設置了某活動的開始日期和結束日期,系統需要判斷當前是否是該活動時間,在Java開發的信息系統中,通常日期以字符串形式“yyyy-MM-dd hh:mm:ss”保存到數據庫中,在進行轉換中,通常需要將字符串表示的日期轉換為Date對象,Java API提供了一個工具SimpleDateFormat對象能將一個滿足“yyyy-MM-dd hh:mm:ss”形式的字符串轉換成一個Date對象。在日期轉換中,經常也利用到時間戳,什么是時間戳(timestamp)?時間戳(timestamp)是一種時間表示方式,定義為從格林威治時間1970年01月01日00時00分00秒起至現在的總秒數。通過日期對象的getTime()將獲得該日期的當前時間戳的毫米數。
? ? 給你的問題是,如何給出某活動的開始日期和結束日期,已經當前日期的時間戳,需要你編程判定當前日期所處的位置.
樣例輸入:
? ? 輸入的第一行為已個正整數N,表示需要你計算活動次數,接下來的N行,每行的第一個數為一個10位正整數,表示當前日期的時間戳秒數,接下來是兩個字符串,表示活動開始日期和結束日期。字符串滿足日期“yyyy-MM-dd hh:mm:ss”格式。
樣例輸出:
? ? 如果當前日期在活動開始之前,就輸出“Before",如果在活動之中輸出"NOW", 如果在活動之后,輸出"After".
測試輸入數據:
2
1389339888 "2013-10-11 12:12:12" "2013-10-12 12:12:12"
1389339888 "2014-01-09 12:12:12" "2014-01-12 12:12:12"
測試輸出數據:
After
NOW
本題代碼答案:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;public class DataQuestion {public static void main(String[] args) {Scanner in=new Scanner(System.in);int n=in.nextInt();for(int i=0;i<n;i++){long now=in.nextLong();String str = in.nextLine();MyDate mydate=new MyDate(now,str);System.out.println(mydate.getResult());}}
}
class MyDate{long now;//秒Date begindate,enddate;public MyDate(long now,String str) {this.now = now;//"2013-10-11 12:12:12" "2013-10-12 12:12:12"str=str.trim();String strd1=str.substring(1,20);String strd2=str.substring(23,str.length()-1);try {SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");begindate ?= df.parse(strd1);enddate ?= df.parse(strd2);} catch(Exception ex) {ex.printStackTrace();}//System.out.println(strd1+":"+strd2);}public String getResult(){if(now <begindate.getTime()/1000)return "Before";else if(now >enddate.getTime()/1000)return "After";else return "NOW";}
}
6、日期計算問題
????????在信息系統開發中,日期的計算成為常態,利用Java程序設計解決日期相關問題變得簡單,給你的問題是計算兩個日期(非時間上)相隔的天數。
輸入:
? ? 標準輸入,輸入的第一行為一個正整數N,表示需要你計算的問題數目,接下來的N行,每行有兩個表示日期和時間格式的數據,兩數據之間有一個空格,并且前一個日期在后一個日期的前面。
輸出:
? ? 標準輸出,輸出兩日期(非時間)之間相處的天數,每個計算結果使用一行輸出,并按照輸入順序依次輸出該問題的結果。
測試用例輸入:
4
2021-05-31 00:00 2021-06-01 00:01
2017-01-10 08:00 2017-01-10 09:00
2014-01-05 08:30 2014-01-14 08:00
2014-03-05 16:35 2014-07-03 08:00
測試用例輸出:
1
0
9
120
本題代碼答案:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class DateCalculation {public static void main(String[] args) throws ParseException {Scanner sc=new Scanner(System.in);SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");int n=sc.nextInt();while (n-- > 0) {String s1=sc.next()+" "+sc.next();String s2=sc.next()+" "+sc.next();Date date1=sdf.parse(s1);Date date2=sdf.parse(s2);long dd=(long)(date2.getTime()-date1.getTime())/24/60/60/1000;System.out.println(dd);}}
}
7、星期日期的計算
問題描述:
? ? ? ?在平時日常生活中通常需要回答當月的某日期為星期幾的問題,常常通過提供你當月某天是星期幾,然后需要計算當月的某日期為星期幾。提供當月日期和星期的數(用阿拉伯數表示,0為Sunday,1為Monday,2為Tuesday,3為Wednesday,4為Thursday,5為Friday,6為Saturday),然后提供一個當月的日期號數,請計算該日期號數為星期幾,使用英文單詞輸出星期幾。
輸入:
? ? ? 標準輸入,輸入的第一行為計算用例數為整數n,接下來的n行,每行有3個由空格分開整數,其中第一個整數為當月的日期數,第二個整數為第一個整數表示的日期的星期幾,第三個整數是需要你回答當月該日期是星期幾,如該行輸入為:1 3 ?10,表示當月的1號為星期三,請問當月的10號為星期幾。
輸出:
? ? ?標準輸出:每一個計算的結果使用一行輸出,輸出使用英文單詞輸出星期幾。
輸入樣例:
2
1 3 10
10 0 5
輸出樣例:
Friday
Tuesday
本題代碼答案:
import java.util.Scanner;public class DateWeek {public static void main(String[] args) {Scanner sc = new Scanner(System.in);String[] week = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};int n = sc.nextInt();while (n-- > 0) {int d1 = sc.nextInt();int w1 = sc.nextInt();int d2 = sc.nextInt();int w2 = (d2 - d1 + w1) % 7;w2 = w2 < 0? w2 + 7:w2;System.out.println(week[w2]);}}
}
8、計算坐標平面上兩點距離
題目描述
? ? 在平面直角坐標系中求兩點的距離。
輸入
? ? 輸入有若干行,每行四個數,分別代表兩個坐標點(a,b)和(c,d),當每行四個數為0時,結束輸入,本用例不用計算。
輸出
? ? 對應輸出這兩點之間的距離。結果保留兩位小數。請注意行尾輸出換行。
樣例輸入
0 0 4 3
1.1 -2.3 0 1
0 0 0 0
樣例輸出
5.00
3.48
本題代碼答案:
import java.util.Scanner;
import java.lang.Math;public class DistQuestion {private static double a, b, c, d;//這種方法Scanner也要放外面并設置成staticstatic Scanner sc = new Scanner(System.in);public static void main(String[] args) {while ( sin() ) {double s = Math.sqrt((a-c)*(a-c) + (b-d)*(b-d));System.out.printf("%.2f%n", s);}}private static boolean sin() {a = sc.nextDouble();b = sc.nextDouble();c = sc.nextDouble();d = sc.nextDouble();return a != 0 || b != 0 || c != 0 || d != 0;}
}
9、異常處理設計問題
????????我們能夠通過海倫公式計算已知一個三角形三邊長度的三角形面積,如果三角形的三邊長為a,b,c,三角形的面積計算公式描述如下:
????????要求你完成MyMath對象中的trianglearea法的實現,如果參數a,b,c的值不能構成一個三角形,使用異常進行處理,產生無效三角形異常,如果a,b,c中有負數,產生參數無效異常,根據題目要求請你完成異常類型和MyMath類型的設計。MyMath設計代碼如下:
import java.util.*;
public class MyMath
{
?public static double trianglearea(double a,double b,double c)
? throws NotTriAngleException,NegativeArgementsException{
? ? ? ? //判定a,b,c是否全為正數
? //判定a,b,c是否能構成三角形
? //計算三角形面積
? double s;
?}
?public static void main(String[] args)
?{
? double a,b,c;
? int N;
? Scanner sc= new Scanner(System.in);
? N = sc.nextInt();
? for(int i=0;i<N;i++){
? ?a=sc.nextDouble();
? ?b=sc.nextDouble();
? ?c=sc.nextDouble();
? ?System.out.printf("%.2f\n",MyMath.trianglearea(a,b,c));
? }
?}? ?
}
class NotTriAngleException extends Exception
{????????}
class NegativeArgementsException extends Exception
{????????}
? 輸入:
? ? ? 輸入的第一行為一個正整數N(N>0),表示以下有N行數據,每行為一個三角形邊的輸入信息,原則上是由空格隔開的三個浮點數,但也可能由于輸入錯誤產生非正常情況。
? 輸出:
? ? ? 如果某行三角形邊數據能讀入三個浮點數,輸出方法trianglearea計算結果,保留2位小數,如果發生異常直接輸出異常信息;每個三角形計算信息使用一行輸出;如果某行三角形邊數據讀入出現問題,直接退出程序的運行。
? 測試1輸入:
? ? ?3
? 3 4 5.0
? 1 1 2
? ? ?-2 4.0 3
? 測試1輸出:
? ? ?6.00
? ? Not TriAngle
? ? ?Negative Argements
? 測試2輸入: ? ?
? 3
? 3 4 5.0
? 1a 1 2
? ?-2 4.0 3
? 測試2輸出:
? ? ?6.00
本題代碼答案:
import java.util.*;
public class Ex_Handing {public static double trianglearea(double a, double b, double c) throws NotTriAngleException, NegativeArgementsException {// 判定a,b,c是否全為 正數if (a < 0 || b < 0 || c < 0){ throw new NegativeArgementsException("Negative Argements"); }//判定a,b,c是否能構成三角形if (a + b <= c || a + c <= b || c + b <= a){ throw new NotTriAngleException("Not TriAngle"); }// 計算三角形面積double p = (a + b + c) / 2.0;double s = Math.sqrt(p*(p -a)*(p -b)*(p -c));return s; }public static void main(String[] args){ double a = 0, b = 0, c = 0;int N;Scanner sc = new Scanner(System.in);N = sc.nextInt();for (int i = 0; i < N; i++){ try{ a = sc.nextDouble(); b = sc.nextDouble(); c = sc.nextDouble();System.out.printf("%.2f\n", Ex_Handing.trianglearea(a, b, c)); }catch (NegativeArgementsException e){ System.out.println(e.getMessage()); }catch (NotTriAngleException e){ System.out.println (e.getMessage()); }catch (Exception e) { }}}
}
class NotTriAngleException extends Exception
{NotTriAngleException(String message) { super(message); } }
class NegativeArgementsException extends Exception
{NegativeArgementsException(String message) { super(message); }
}
10、Java源文件和字節碼文件名稱的判斷問題
????????Java源文件名必須滿足以“.java”為后綴名,他編譯的字節碼文件以“.class”為后綴名,文件的命名(Java類型命名)必須滿足標識符規則,即文件名必須由英文大小寫字母、數字、下劃線和美元符組成,并且第一個字母不能為數字字符。
? ? ? ?給你的問題是請你編寫程序判斷提供的字符串是Java源文件還是字節碼文件。
輸入:
? ? 標準輸入,標準輸入多行,每行字符串為提供的一個文件名稱。
輸出:
? ? 標準輸出,根據輸入一行的字符串進行判斷,如果是合法的源文件,使用一行輸出“Java Source File",如果是合法的字節碼文件,使用一行輸出”Java Byte File",如果文件既不是源文件,也不是字節碼文件,使用一行輸出“Not Java File"。
輸入樣列:
Test.java
Test1.class
3Demo.class
輸出樣列:
Java Source File
Java Byte File
Not Java File
本題代碼答案:
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class File {public static void main(String[] args) {String s = null;Scanner in = new Scanner(System.in);while (in.hasNextLine()) {s = in.nextLine();FileTool1 ss = new FileTool1(s);ss.getSource();}}
}
class FileTool1 {String s;public FileTool1(String s) { this.s = s;}public ?void getSource () {int ?n = s.indexOf(".");Pattern name ?= Pattern.compile("^[a-zA-Z_$]+[a-zA-Z\\d_$]+$");String file = s.substring(0,n);Matcher f = name.matcher(file.trim());if (f.matches() && s.endsWith(".java"))System.out.println("Java Source File");else if (f.matches() && s.endsWith(".class"))System.out.println("Java Byte File");elseSystem.out.println("Not Java File");}
}
11、函數表達式的計算
????????請你設計一個程序,根據輸入的x值計算出函數值y,函數表達式為:
? ? | ?x,x<1
y=| 2x-1, ?1<=x<10
? ? |3x-11, ?x>=10
? ? 輸入數據的x值使用標準輸入,x的輸入占一行,你的計算結果也使用一行輸出,輸出結果保留6位小數。
輸入測試用例:
12.54
用例輸出結果:
26.620000
本題代碼答案:
import java.util.Scanner;
public class FunctionExpression{public static void main(String[] args){Scanner in=new Scanner(System.in);double y,x=in.nextDouble();if(x<1) y=x;else if(x<10) y=2*x-1;else y=3*x-11;System.out.printf("%.6f",y);}
}
12、成績的學分績點計算
題目描述
? ? 某校學生評比采用績點制,規則是:90分以上(含90分,下同)算5點,80分以上算4點,70分以上算3點,60分以上算2點,不及格算0點,請根據某個學生的成績及學分計算該生該門課程所獲得的績點。
輸入
? ? 標準輸入,輸入的第一行為需要計算用例個數N,接下來的N行,每行有兩個浮點數,第一個數為某課程的考試成績,第二個數為該課程的學分值。
輸出
? ? 標準輸出,每一行輸出一個學生的某課程計算得到的績點數(結果保留一位小數),請注意行尾輸出換行。
樣例輸入
3
85 1.5
90 3
50 4
樣例輸出
6.0
15.0
0.0
本題代碼答案:
?import java.util.Scanner;public class GradePoint{public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n = sc.nextInt();while ( n-- > 0 ) {double score = sc.nextDouble();double weight = sc.nextDouble();double point = 0.0;if ( score >= 90.0 ) point = 5.0;else if ( score >= 80.0 ) point = 4.0;else if ( score >= 70.0 ) point = 3.0;else if ( score >= 60.0 ) point = 2.0;System.out.printf("%.1f\n", weight * point);}}
}