參考鏈接: Java從控制臺讀取輸入的方法
package chap02;
?
import java.util.Scanner;
?
/**
?*?
?* @author admin
?* @date 2020-4-8
?* description:
?* 題目內容:
編寫程序, 從控制臺讀取下面的信息, 每月按22天工作日計算,結果保留兩位小數,輸出月薪。
員工姓名(如: zhangmin )
每天的工作小時數(如: 8 )
每小時的酬金(如: 50 )
個人所得稅率(如 0.052 )
?
輸入格式:
name = scanner.next();
time = scanner.nextDouble();
remuneration = scanner.nextDouble();
tax = scanner.nextDouble();
?
輸出格式:
System.out.printf( "%s salary is %10.2f" ,name, salary);
?
輸入樣例:
zhangmin
8
50
0.052
?
輸出樣例:
zhangmin salary is? ? 8342.40
?*/
?
public class Salary {
? ? public static void main(String[] args){
? ? ? ? //0.定義變量
? ? ? ? String name;
? ? ? ? double time,remuneration,tax;
? ? ? ? double salary;
? ??????
? ? ? ? Scanner scanner = new Scanner (System.in);
? ??????
? ? ? ? //1.獲取輸入數據
? ? ? ? name = scanner.next();
? ? ? ? time = scanner.nextDouble();
? ? ? ? remuneration = scanner.nextDouble();
? ? ? ? tax = scanner.nextDouble();
? ??????
? ? ? ? //2.計算月薪
? ? ? ? salary = time * remuneration *? 22 * (1 - tax);
? ??????
? ? ? ? //3.輸出結果
? ? ? ? System.out.printf("%s salary is %10.2f",name,salary);
? ??????
? ? }
? ??
?
}
?
package chap03;
?
/**
?*?
?* @author admin
?* @date 2020-4-8
?* description:
?* 題目內容:
輸入一個年份,判斷該年是不是閏年。(提示如果年份可以被 4 整除而不能被100整除, 或者可以被400 整除, 那么該年就是閏年)
輸入格式:
一個整數年份
輸出格式:
字符串
?
輸入樣例:
2000
或
2019
?
輸出樣例:
please input year:
2000 is leap year.
或
please input year:
2019 isn't leap year.
?*/
?
?
import java.util.Scanner;
?
public class LeafYear {
? ? public static void main(String[] args){
? ? ? ? int year;
? ? ? ? Scanner scanner = new Scanner(System.in);
? ??????
? ? ? ? System.out.print("please? input year:");
? ? ? ? year = scanner.nextInt();
? ? ? ? if((year % 4 == 0 && year != 100) || (year % 400 == 0)){
? ? ? ? ? ? System.out.println(year+"is leap year");
? ? ? ? }else{
? ? ? ? ? ? System.out.println(year + "isn't leap year.");
? ? ? ? }
? ? }
?
}
?
?
package chap03;
?
/**
?*?
?* @author admin
?* @date 2020-4-8
?* description:
?* 題目內容:
?
編寫程序:利用switch語句根據輸入的學生綜合成績等級,輸出學生的評語。學生的等級分為{'A','B','C','D','E'}(等級大小寫都需要支持),
分別對應的評語為{“perfect”,“excellent”,“good”,“qualified”,“failed”},如果輸入的等級不對,輸出“error”。
?
輸入格式:
?
A-E與a-e字符
?
?
?
輸出格式:
?
input grade:? X
?
評語字符串
?
輸入樣例:
?
A
?
?
?
輸出樣例:
?
input grade:
?
perfect!
?
?
?
輸入樣例:
?
a
?
?
?
輸出樣例:
?
input grade:perfect!
?*/
?
import java.util.Scanner;
?
public class ScoreGrade {
? ? public static void main(String[] args){
? ? ? ? //0.定義變量
? ? ? ? char grade;
? ? ? ? Scanner scanner = new Scanner(System.in);
? ??????
? ? ? ? //1.獲取輸入
? ? ? ? System.out.print("input grade:");
? ? ? ? grade = scanner.next().charAt(0);
? ??????
? ? ? ? //2.處理
? ? ? ? switch (grade){
? ? ? ? case 'A':
? ? ? ? case 'a':
? ? ? ? ? ? System.out.println("perfect!");
? ? ? ? ? ? break;
? ? ? ? case 'B':
? ? ? ? case 'b':
? ? ? ? ? ? System.out.println("excellent");
? ? ? ? ? ? break;
? ? ? ? case 'C':
? ? ? ? case 'c':
? ? ? ? ? ? System.out.println("good");
? ? ? ? ? ? break;
? ? ? ? case 'D':
? ? ? ? case 'd':
? ? ? ? ? ? System.out.println("qualified");
? ? ? ? ? ? break;
? ? ? ? case 'E':
? ? ? ? case 'e':
? ? ? ? ? ? System.out.println("failed");
? ? ? ? ? ? break;
? ? ? ? default:
? ? ? ? ? ? System.out.println("input error!");
? ? ? ? }????
? ? }
}
?
?
package chap04;
?
/**
?*?
?* @author admin
?* @date 2020-4-8
?* description:
?* 題目內容:
?
編寫程序:在Geometry類中編寫求圓的面積、三角形的面積、矩形面積的方法。
?
輸入格式:
double r = scanner.nextDouble();
double a = scanner.nextDouble();
double h = scanner.nextDouble();
double x = scanner.nextDouble();
double y = scanner.nextDouble();
?
輸出格式:
System.out.println("Area of circle:"+circle(r));
System.out.println("Area of triangle:"+triangle(a, h));
System.out.print("Area of rectangle:"+rectangle(x, y));
?
輸入樣例:
2
2
2
2
2
?
輸出樣例:
Area of circle:12.56
Area of triangle:2.0
Area of rectangle:4.0
?*/
?
import java.util.Scanner;
?
public class Geometry {
? ? public static void main(String[] args){
? ??????
? ? ? ? //0.定義變量
? ? ? ? Scanner scanner = new Scanner(System.in);
? ??????
? ? ? ? //2.輸入
? ? ? ? double r = scanner.nextDouble();
? ? ? ? double a = scanner.nextDouble();
? ? ? ? double h = scanner.nextDouble();
? ? ? ? double x = scanner.nextDouble();
? ? ? ? double y = scanner.nextDouble();
? ??????
? ? ? ? //3.輸出
? ? ? ? System.out.println("Area of circle:"+circle(r));
? ? ? ? System.out.println("Area of triangle:"+triangle(a, h));
? ? ? ? System.out.print("Area of rectangle:"+rectangle(x, y));
? ? }
? ? public static double circle(double radius){
? ? ? ? double result;
? ? ? ? result = Math.PI * radius*radius;
? ? ? ? return result;
? ? }
? ??
? ? public static double triangle(double a,double h){
? ? ? ? return a * h * 2;
? ? }
? ??
? ? public static double rectangle(double width,double height){
? ? ? ? return width * height;
? ? }
}