1.理发题(10份)
(1)110元,洗剪吹31元,可以多少次洗剪吹?最后还剩多少?
(2)一次剪头发15元和一次洗头20元,平均每次消费多少钱?
public class Test04 {
public static void main(String[]args){
double money=110;
double costone=31;
int count=(int)(money/costone);
System.out.println("可以理发"+count+"次");
double lastmoney=money%costone;
System.out.println("剩余"+lastmoney+"元");
double avgmoney=(15d+20d)/2d;
System.out.println("理发15,洗头20,那么平均消费是:"+avgmoney);
2.打印九九乘法表(15分)
public class NineNine {
public static void main(String[] args) {
for (int i= 1; i <=9 ; i++) {
for (int j =1; j <=i ; j++) {
System.out.print(j+"*"+i+"="+i*j+"\t");
}
System.out.println();
}
}
}
3.编写万用表程序
1 电压挡
2 电流挡
3 电阻档
4 其他档位
5 退出
注意:使用scanner(system.in)时,使用完毕后,一定要关闭扫描器,因为system.in属于IO流,一旦打开,它一直在占用资源,因此使用完毕后切记要关闭。(15分)
public class MulTimter {
public static void main(String[] args) {
System.out.println("欢迎使用万用表:");
Scanner scanner = new Scanner(System.in);
System.out.println("请选择档位:1电压档 2电流档3电阻档 4其他档位 5退出 ");
System.out.println("请输入你的选择:");
String input = scanner.next();
// 过滤无效的选择
while (!input.equals("1") && !input.equals("2")&& !input.equals("3")
&& !input.equals("4") && !input.equals("5")) {
System.out.println("请输入有效的选择:");
input = scanner.next();// 获取用户输入的选择
}
// 判断选择
switch (input) {
case "1":
System.out.println("你选择了电压档");
break;
case "2":
System.out.println("你选择了电流档");
break;
case "3":
System.out.println("你选择了电阻档");
break;
case "4":
System.out.println("你选择了其他档");
break;
case "5":
System.out.println("谢谢您的使用!");
break;
}
scanner.close();
}
}
4.编写三个方法,分别得出一个数组的最大值,最小值,平均值。(15分)
public class array {
public static void main(String[] args) {
int[]array={1,2,3,4,5,6};
int max=numMax(array);
System.out.println("最大的数是:" + max);
int min=numMin(array);
System.out.println("最小的数是:" + min);
double avg =numAvg(array);
System.out.println("平均数是:" +avg);
}
private static double numAvg(int[] array) {
double sum=0;
double numAvg=0;
for (int i = 0; i <array.length ; i++) {
sum+=array[i];
}
numAvg=sum/array.length;
return numAvg;
}
private static int numMin(int[] array) {
int numMin=array[0]; //定义一个最小值
for (int i = 0; i <array.length ; i++) {
if (numMin>array[i]){
numMin=array[i];
}
}
return numMin;
}
private static int numMax(int[] array) {
int numMax=0;
for (int i = 0; i <array.length ; i++) {
if (numMax<array[i]){
numMax=array[i];
}
}
return numMax;
}
}
5.创建宠物类(属性:名字 ,体重 方法: 奔跑,捕食)在DEMO类实例化宠物,设置名字,调用奔跑,捕食方法(15分)
public class Pet {
private String name;
private double weight;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
void run(){
System.out.println("它在奔跑");
}
void catchFood(){
System.out.println("它在捕食");
}
}
class demo{
public static void main(String[] args) {
Pet pet = new Pet();
pet.setName("麒麟");
pet.setWeight(100);
System.out.println("我的宠物:" + pet.getName()+",体重:" + pet.getWeight()+"kg");
pet.run();
pet.catchFood();
}
}
6.接收用户输入的5门功课并且,计算平均分。(15分)
给用户评级60-80良,81-90好,91-100优秀。
public class Test05 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double array[]=new double[5];
System.out.println("请输入5门功课的成绩");
double sum = 0;// 定义分数总和值
double avg = 0;// 定义平均分值
for (int i = 0; i <5; i++) {
System.out.print("请输入" + (i + 1)+ "第门成绩:");
array[i] = scanner.nextDouble();
sum+=array[i];
}
avg = sum / array.length;// 求得平均分值
String rank = avg >=91 ? "优秀" : (avg >=81 ? "好" : (avg >=60) ? "良"
: "");
System.out.println("五门科目的平均分是:" + avg+"\t评级为:" + rank);
scanner.close();
}
}
7.创建一个面积类,可以计算长方形,圆形的面积。并在DEMO类测试,计算长方形面积,圆的面积。(15分)
public class Circele{
private double radius = 0;// 圆的半径
public Circele(double radius){// 通过构造方法,获得半径
this.radius = radius;
}
//获得圆形面积
double getArea() {
return Math.PI * radius * radius;
}
}
public class Rectangle {
private double height=0;
private double weigtht=0;
public Rectangle(double height,double weigtht){
this.height=height;
this.weigtht=weigtht;
}
double getarea(){
return height*weigtht;
}
}
public class Demo {
public static void main(String[] args) {
Circele circele = new Circele(10);
System.out.println("圆的面积是:"+circele.getArea());
Rectangle rectangle = new Rectangle(10, 4);
System.out.println("长方形面积是:"+rectangle.getarea());
}
}
8.判断200-300之间有多少个素数,并输出所有素数。
程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。
public class suShu {
public static void main(String[] args) {
int sum = 0;
for (int i = 200; i<= 300; i++)
{
boolean isFind = false;
for (int j= 2; j <i; j++)
{
if (i%j==0)
{
isFind = true;
break;
}
}
if (!isFind)
{
System.out.println(i);
sum += i;
}
}
System.out.println("200到300之间的质数之和是:"+sum);
}
}
9.输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
程序分析:利用while语句,条件为输入的字符不为‘\n‘.
public class CountAll {
public static void main(String[] args){
System.out.print("请输入一串字符:");
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();//将一行字符转化为字符串
scan.close();
count(str);
}
//统计输入的字符数
private static void count(String str){
String E1 = "[\u4e00-\u9fa5]";//汉字
String E2 = "[a-zA-Z]";
String E3 = "[0-9]";
String E4 = "\\s";//空格
int countChinese = 0;
int countLetter = 0;
int countNumber = 0;
int countSpace = 0;
int countOther = 0;
char[] array_Char = str.toCharArray();//将字符串转化为字符数组
String[] array_String = new String[array_Char.length];//汉字只能作为字符串处理
for(int i=0;i<array_Char.length;i++) {
array_String[i] = String.valueOf(array_Char[i]);
}
//遍历字符串数组中的元素
for(String s:array_String){
if(s.matches(E1)) {
countChinese++;
}else if(s.matches(E2)) {
countLetter++;
}else if(s.matches(E3)){
countNumber++;
}else if(s.matches(E4)) {
countSpace++;
}else
countOther++;
}
System.out.println("输入的汉字个数:"+countChinese);
System.out.println("输入的字母个数:"+countLetter);
System.out.println("输入的数字个数:"+countNumber);
System.out.println("输入的空格个数:"+countSpace);
System.out.println("输入的其它字符个数:"+countSpace);
}
}
10.求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
public class Sum {
public static void main(String[] args){
System.out.print("求s=a+aa+aaa+aaaa+...的值,请输入a的值:");
Scanner scan = new Scanner(System.in).useDelimiter("\\s*");//以空格作为分隔符
int a = scan.nextInt();
int n = scan.nextInt();
scan.close();//关闭扫描器
System.out.println(expressed(2,5)+add(2,5));
}
//求和表达式
private static String expressed(int a,int n){
StringBuffer sb = new StringBuffer();
StringBuffer subSB = new StringBuffer();
for(int i=1;i<n+1;i++){
subSB = subSB.append(a);
sb = sb.append(subSB);
if(i<n)
sb = sb.append("+");
}
sb.append("=");
return sb.toString();
}
//求和
private static long add(int a,int n){
long sum = 0;
long subSUM = 0;
for(int i=1;i<n+1;i++){
subSUM = subSUM*10+a;
sum = sum+subSUM;
}
return sum;
}
}
11.输入某年某月某日,判断这一天是这一年的第几天?
程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。
public class WhichDay {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in).useDelimiter("\\D");//匹配非数字
System.out.println("请输入当前日期(年-月-日:");
int year = scan.nextInt();
int month = scan.nextInt();
int date = scan.nextInt();
scan.close();
System.out.println("今天是"+year+"年的第"+analysis(year,month,date)+"天");
}
private static int analysis(int year, int month,int date){
int n = 0;
int[] month_date = new int[] {0,31,28,31,30,31,30,31,31,30,31,30};
if((year%400)==0 || ((year%4)==0)&&((year%100)!=0))
month_date[2] = 29;
for(int i=0;i<month;i++)
n += month_date[i];
return n+date;
}
}
12.输入三个整数x,y,z,请把这三个数由小到大输出
程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小
public class Compare {
public static void main(String[] args){
Scanner scan = new Scanner(System.in).useDelimiter("\\D");
System.out.print("请输入三个数:");
int x = scan.nextInt();
int y = scan.nextInt();
int z = scan.nextInt();
scan.close();
System.out.println("排序结果:"+sort(x,y,z));
}
//比较两个数的大小
private static String sort(int x,int y,int z){
String s = null;
if(x>y){
int t = x;
x = y;
y = t;
}
if(x>z){
int t = x;
x = z;
z = t;
}
if(y>z){
int t = z;
z = y;
y = t;
}
s = x+" "+y+" "+z;
return s;
}
}
13.求1+2!+3!+...+20!的和
程序分析:此程序只是把累加变成了累乘。
public class ForTest {
public static void main(String[] args){
int x=1;
int sum=0;
for (int i = 1; i <=20 ; i++) {
x=x*i;
sum+=x;
}
System.out.println(sum);
}
}
14.利用递归方法求5!
程序分析:递归公式:fn=fn_1*4!
public class Fn {
public static void main(String[] args) {
System.out.println(fact(10));
}
//递归阶乘公式
private static long fact(int n){
if(n==1){
return 1;
}else {
return fact(n-1)*n;
}
}
}
15.请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母。
程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。
public class Week {
public static void main(String[] args){
String str = new String();
BufferedReader bufIn = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入星期的英文单词前两至四个字母):");
try{
str = bufIn.readLine();
}catch(IOException e){
e.printStackTrace();
}finally{
try{
bufIn.close();
}catch(IOException e){
e.printStackTrace();
}
}
week(str);
}
private static void week(String str){
int n = -1;
if(str.trim().equalsIgnoreCase("Mo") || str.trim().equalsIgnoreCase("Mon") || str.trim().equalsIgnoreCase("Mond"))
n = 1;
if(str.trim().equalsIgnoreCase("Tu") || str.trim().equalsIgnoreCase("Tue") || str.trim().equalsIgnoreCase("Tues"))
n = 2;
if(str.trim().equalsIgnoreCase("We") || str.trim().equalsIgnoreCase("Wed") || str.trim().equalsIgnoreCase("Wedn"))
n = 3;
if(str.trim().equalsIgnoreCase("Th") || str.trim().equalsIgnoreCase("Thu") || str.trim().equalsIgnoreCase("Thur"))
n = 4;
if(str.trim().equalsIgnoreCase("Fr") || str.trim().equalsIgnoreCase("Fri") || str.trim().equalsIgnoreCase("Frid"))
n = 5;
if(str.trim().equalsIgnoreCase("Sa") || str.trim().equalsIgnoreCase("Sat") || str.trim().equalsIgnoreCase("Satu"))
n = 2;
if(str.trim().equalsIgnoreCase("Su") || str.trim().equalsIgnoreCase("Sun") || str.trim().equalsIgnoreCase("Sund"))
n = 0;
switch(n){
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 0:
System.out.println("星期日");
break;
default:
System.out.println("输入有误!");
break;
}
}
}
16.100之内的素数
程序分析:素数是不能被1或者它本身之外的其他数整除的整数
public class Sushu {
public static void main(String[] args) {
int i,j;
for ( i = 1; i <=100;i++) {
for ( j =2; j <i ; j++) {
if ((i%j)==0){
break;
}
}
if (i==j){
System.out.println(i);
}
}
}
}
17.输入3个数a,b,c,按大小顺序输出。
public class ThreeNumber {
public static void main(String[] args){
System.out.print("请输入3个数:");
Scanner scan = new Scanner(System.in).useDelimiter("\\s");
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
scan.close();
if(a<b){
int t = a;
a = b;
b = t;
}
if(a<c){
int t = a;
a = c;
c = t;
}
if(b<c){
int t = b;
b = c;
c = t;
}
System.out.println(a+" "+b+" "+c);
}
}
18.写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。
//注意:next()方法读取到空白符就结束了,nextLine()读取到回车结束也就是“\r”;
public class StringLenth {
public static void main(String[] args) {
System.out.print("请输入一行字符串:");
Scanner scanner = new Scanner(System.in).useDelimiter("\\n");
String str = scanner.nextLine();
scanner.close();
//将字符串转化为字符数组
char[]chars=str.toCharArray();
System.out.println(str+"共"+(chars.length-1)+"个字符");
}
}
19.对字符串的排序
public class letter {
public static void main(String[] args){
String[] str = {"abc","cad","m","fa","f"};
for(int i=str.length-1;i>=1;i--){
for(int j=0;j<=i-1;j++){
if(str[j].compareTo(str[j+1])<0){
String temp = str[j];
str[j] = str[j+1];
str[j+1] = temp;
}
}
}
for(String subStr:str)
System.out.print(subStr+" ");
}
}
20.两个字符串连接程序
public class Connection {
public static void main(String[] args) {
String str1="你好,";
String str2="世界欢迎你!";
String str=str1+str2;
System.out.println(str);
}
}
21.统计字符串中子串出现的次数
public class Character {
public static void main(String[] args){
String str = "I come from China";
char[] ch = str.toCharArray();
int count = 0;
for(int i=0;i<ch.length;i++){
if(ch[i]==‘ ‘)
count++;
}
count++;
System.out.println("共有"+count+"个字串");
}
}
22.有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,将原有的数据和计算出的平均分数存放在磁盘文件"stud"中。
public class Student {
//定义学生数组模型
String[] number = new String[5];
String[] name = new String[5];
float[][] grade = new float[5][3];
float[] sum = new float[5];
/**
* 输入学号、姓名、成绩
*/
void input() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//录入状态标识
boolean isRecord=true;
while (isRecord){
try {
for (int i = 0; i <5 ; i++) {
System.out.print("请输入学号:");
number[i]=br.readLine();
System.out.print("请输入姓名:");
name[i] = br.readLine();
for (int j = 0; j <3; j++) {
System.out.print("请输入第"+(j+1)+"门课成绩:");
grade[i][j] = Integer.parseInt(br.readLine());
}
System.out.println();
sum[i] = grade[i][0]+grade[i][1]+grade[i][2];
}
isRecord=false;
}catch (Exception e){
System.out.print("请输入数字!");
}
}
}
/**
* 输出文件内容
* @param
*/
void output() throws IOException {
FileWriter fw = new FileWriter("E:\\Me\\我的下载\\stud.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write("No. "+"Name "+"grade1 "+"grade2 "+"grade3 "+"average");
//换行
bw.newLine();
for (int i = 0; i <5; i++) {
bw.write(number[i]);
for (int j = 0; j <3 ; j++) {
bw.write(" "+grade[i][j]);
bw.write(" "+sum[j]/5);
bw.newLine();
}
}
bw.close();
}
public static void main(String[] args) throws IOException {
Student student = new Student();
student.input();
student.output();
}
}
23.从硬盘中复制一个文件内容到另一个文件中
public class FileCopy {
public static void main(String[] args) {
copyAToB();
}
public static void copyAToB(){
File file=new File("E:/Me/我的下载/stud.txt");
File file2=new File("E:/Me/我的下载/b.txt");
if(!file2.exists()){
try {
file2.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
InputStream in=null;
OutputStream out=null;
try {
in=new FileInputStream(file);//建立到a的流
out=new FileOutputStream(file2);//建立到b的流
int i;
while((i=in.read())!=-1){//从a读取字母
out.write(i);//将字母写到b里
}
System.out.println("复制成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
out.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
参考资料:https://blog.csdn.net/wenzhi20102321/article/details/52430616
https://blog.csdn.net/cao2219600/article/details/80973336
原文:https://www.cnblogs.com/ttg-123456/p/12236275.html