java基础作业1附答案

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

j a v a基础作业1附答案-标准化文件发布号:(9456-EUATWK-MWUB-WUNN-INNUL-DDQTY-KII
1. X (x<1)
Y= 2x-1 (1<x<10)
3x-11 (x>10) 要求输入x值后,输出y值。

package student.xiaoqiao.day01;
import java.util.Scanner;
public class Yi {
public static void main(String[] args) {
System.out.println("请输入一个数");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
if (x < 1) {
System.out.println("y=" + x);
}
if (x > 1 & x < 10) {
System.out.println("y=" + (2 * x - 1));
}
if (x > 10) {
System.out.println("y=" + (3 * x - 11));
}
}
}
2.输入一个年份判断year是否是闰年
package student.xiaoqiao.day01;
import java.util.Scanner;
public class Er {
public static void main(String[] args) {
System.out.println("请输入年份");
Scanner sc=new Scanner(System.in);
int year=sc.nextInt();
if((year%4==0&year%100!=0)|(year%400==0))
System.out.println("是闰年");
else
System.out.println("不是闰年");
}
}
3.任意输入a,b,c 3个数判断a,b,c大小
package student.xiaoqiao.day01;
import java.util.Scanner;
public class San {
public static void main(String[] args) {
System.out.println("请输入三个数(加空格或回车):");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
if (x > y & x > z) {
System.out.println(x);
} else if (x < y & z < y) {
System.out.println(y);
} else if (x < z & y < z) {
System.out.println(z);
}
}
}
4_1输出图形
*
***
*****
package student.xiaoqiao.day01;
public class Si_1 {
public static void main(String[] args) {
int temp = 3;
for (int i = 1; i <= temp; i++) {
for (int j = 1; j <= temp - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.print('\n');
}
}
}
4_2输出图形
*****
***
*
package student.xiaoqiao.day01;
public class Si_1 {
public static void main(String[] args) {
int temp = 3;
for (int i = 1; i <= temp; i++) {
for (int j = 1; j <= temp - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.print('\n');
}
}
}
4_3输出图形任意输入n
当输入n值为1时,输出 *
当输入n值为2时,输出
当输入n值为3时,输出.。

当输入n值为5时,输出
*
***
*****
*******
*********
*******
*****
***
*
package student.xiaoqiao.day01;
import java.util.Scanner;
public class Si_3 {
public static void main(String[] args) {
System.out.println("请输入一个数:");
Scanner sc=new Scanner(System.in);
int temp = sc.nextInt();
for (int i = 1; i <= temp; i++) {
for (int j = 1; j <= temp - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.print('\n');
}
for (int i = temp; i >= 1; i--) {
for (int j = i-1; j < temp; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 3; k++) {
System.out.print("*");
}
System.out.print('\n');
}
}
4_4 输出图形
***
*****
*******
package student.xiaoqiao.day01;
public class Si_4 {
public static void main(String[] args) {
int temp = 3;
for (int i = 1; i <= temp; i++) {
for (int j = 1; j <= temp - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i + 1; k++) {
System.out.print("*");
}
System.out.print('\n');
}
}
}
4_5 输出图形
package student.xiaoqiao.day01;
public class Si_5 {
public static void main(String[] args) {
int temp = 4;
for (int i = 1; i <= temp; i++) {
for (int k = 1; k <= 5; k++) {
System.out.print("*");
}
System.out.print('\n');
}
}
}
5.判断水仙花数输入一个3位数,判断个位,十位与百位的3次方的和是否等于其本身,相等就是水仙花数
package student.xiaoqiao.day01;
import java.util.Scanner;
public class Wu {
public static void main(String[] args) {
System.out.println("请输入一个三位数");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = a / 100;
int c = (a / 10) % 10;
int d = a % 10;
if (a == b * b * b + c * c * c + d * d * d) {
System.out.println("此数是水仙花");
} else {
System.out.println("此数不是水仙花");
}
}
}
6. 输入一个x和y,在输入一个字符x, 当输入的字符为‘+’,输出x与y的和,当输入的字符为‘-’,输出x与y的差,当输入的字符为‘*’,输出x与y的积,当输入的字符为‘/’,输出x与y的商.
package student.xiaoqiao.day01;
import java.util.Scanner;
public class Liu {
public static void main(String[] args) {
System.out.println("请输入一个加减乘除字符");
Scanner sc=new Scanner(System.in);
char c=sc.next().charAt(0);
int x=4;
int y=2;
switch (c) {
case '+':
System.out.println("x+y="+(x+y));
break;
case '-':
System.out.println("x-y="+(x-y));
break;
case '*':
System.out.println("x*y="+(x*y));
break;
case '/':
System.out.println("x/y="+(x/y));
break;
default:
System.out.println("输入的字符不合法!");
break;
}
}
}
7.求数列 0,1,2,32 ,…………….,n n-1 的和要求判断n是否在0到10之间,如果不在提示重新输入,for循环 while循环,do while 都要写一遍。

7.1package student.xiaoqiao.day01;
import java.util.Scanner;
public class Qi {
public static void main(String[] args) {
System.out.println("请输入一个0到10的数字");
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
if (a>=0&a<=10) {
int b=0;
for (int i = 1; i <=a; i++) {
b=(int) (b+Math.pow(i,i-1));
}
System.out.println(b);
}
else {
System.out.println("输入的数不在0到10之间,请重新输入!");
}
}
}
7.2package student.xiaoqiao.day01;
import java.util.Scanner;
public class Qi_dowhile {
public static void main(String[] args) {
System.out.println("请输入一个0到10的数字");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if (a>=0&a<=10) {
int b=0;
int i=1;
do{
b = (int) (b + Math.pow(i, i-1));
i++;
} while (i<=a);
System.out.println(b);
}
else {
System.out.println("输入的数不在0到10之间,请重新输入!");
}
}
}
7.3package student.xiaoqiao.day01;
import java.util.Scanner;
public class Qi_while {
public static void main(String[] args) {
System.out.println("请输入一个0到10的数字");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if (a>=0&a<=10) {
int b=0;
int i=1;
while (i<=a) {
b = (int) (b + Math.pow(i, i-1));
i++;
}
System.out.println(b);
}
else {
System.out.println("输入的数不在0到10之间,请重新输入!");
}
}
}
8输出9*9乘法表
package student.xiaoqiao.day01;
public class Ba {
public static void main(String[] args) {
for (int i = 1; i <=9; i++) {
for (int j = 1; j <=i; j++) {
System.out.print(i+"*"+j+"="+i*j+" ");
}
System.out.print('\n');
}
}
}
9. 输入x,y比较大小,使用三目运算
package student.xiaoqiao.day01;
import java.util.Scanner;
public class Jiu {
public static void main(String[] args) {
System.out.println("请输入两个值:");
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
int y=sc.nextInt();
System.out.println(x>y"
x大于y":"x小于y");
}
}
Day02作业
题目:编写程序实现一个猜数字游戏:系统随机生成一个100以内的整数,用户根据系统提示来进行猜测,如果猜对,则显示“恭喜”并提示“增加难度,需要继续吗”,如果用户选择继续则系统随机生成一个1000以内的整数,否则系统退出;如果用户猜错,则显示“错误,请重新输入!”,一旦用户连续5次没有猜中,则系统退出!
package student.xiaoqiao.Day02;
import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
public class Randomdemo {
public static void main(String[] args) {
int a=(int) (Math.random()*100)+1;//系统随机生成一个1到100的整数
System.out.println(a);
System.out.println("请输入一个1到100的整数");
Scanner sc1=new Scanner(System.in);//从键盘上输入一个数
int num1=sc1.nextInt();
while(num1!=a) {
if(num1>a){
System.out.println("您输入的数字过大,请重新输入!");
}
if(num1<a){
System.out.println("您输入的数字过小,请重新输入!");
}
num1=sc1.nextInt();
}
if(num1==a) {
System.out.println("小主好棒,您猜对啦!!"+'\r'+"请问还要升级难度吗(随机生成1到1000的整数),继续请输入‘是’,退出选择‘否’。

"+'\r');
char ch=sc1.next().charAt(0);
if (ch=='是') {
int b=(int) (Math.random()*1000)+1;//系统随机生成一个1到1000的整数
System.out.println(b);
System.out.println("请输入一个1到1000的整数");
int num2=sc1.nextInt();
int count=1;
while(num2!=b&count<5) {
System.out.println("错误,请重新输入!");
num2=sc1.nextInt();
count++;
}
if (num2==b) {
System.out.println("您已输入超过五次,游戏结束!");
}
}
if (ch=='否') {
System.out.println("游戏结束!");
}
}
}
}
4.歌手找打分:在歌唱比赛中,共有10位评委进行打分,在计算歌手得分时,去掉一个
最高分,去掉一个最低分,然后剩余的8位评委的分数进行平均,就是该选手的最终得分,输入每个评委的评分,求某选手的得分。

package student.xiaoqiao.Day02;
import java.awt.peer.SystemTrayPeer;
import java.util.Scanner;
public class Grade {
public static void main(String[] args) {
int[] qiao =new int[10];
System.out.println("请输入十位评委成绩(回车确认):");
Scanner scanner = new Scanner(System.in);
for(int i=0;i<10;i++){
qiao[i]=scanner.nextInt();
// if(i==9){
// System.out.println("停止输入!");
// }
}
for (int i = 0; i < qiao.length; i++) {
for (int j = i + 1; j < qiao.length; j++) {
if (qiao[i] < qiao[j]) {
int temp = qiao[i];
qiao[i] = qiao[j];
qiao[j] = temp;
}
}
// System.out.print(qiao[i]+" ");
}
int sum = 0;
for (int i = 1; i < 9; i++) {
sum = sum + qiao[i];
}
System.out.println(sum / 8.0);
}
}
-----------------------------------------------------
1. /** 字符串——比较空间的里值,
* 1、输入89.9543 四舍五入 (1)保留2位 (2) 取整四舍五入---math方法package student.xiaoqiao.Day02;
public class Yi_sishewuru {
public static void main(String[] args) {
double a=89.9543;
System.out.println(Math.round(a*100 )/100.0);
System.out.println(Math.round(a));
}
}
* 2、已经字符串“this is a test of java”
* (1)统计该字符串s出现的次数
* (2)取出字符串中“test”
* (3)将字符串中每个单词的第1个字符,变成大写输出。

* (4)用两种方式实现该字符串的倒叙输出(用StringBuffer和for实现) * (5)算出字符串中字母、数字、空格各多少
package student.xiaoqiao.Day02;
public class Er_Zifuchuan {
public static void main(String[] args) {
int count=0;
String str="this is a test of java";
for(int k=0;k<str.length();k++){
String a1 = str.substring(k,k+1);
if(a1.equals("s")){
count++;
}
}
System.out.println(count);
System.out.println(str.substring(10, 14));
String[] a2=str.split(" ");
StringBuffer a4=new StringBuffer();
for(int k=0;k<a2.length;k++){
a4.append(a2[k].substring(0,
1).toUpperCase()+a2[k].substring(1)+" ");
}
System.out.println(a4.toString());
StringBuffer a5 = new StringBuffer(str);
System.out.println(a5.reverse());
for(int k=0;k<str.length();k++){
System.out.print(str.substring(str.length()-1-k,str.length()-k)+"");
}
int englishCount=0;
int spaceCount=0;
int numCount=0;
char[] aa=str.toCharArray();
for (int i = 0; i < aa.length; i++) {
if (Character.isLetter(aa[i])) {
englishCount++;
}
else if (Character.isSpaceChar(aa[i])) {
spaceCount++;
}
else if (Character.isDigit(aa[i])) {
numCount++;
}
}
System.out.println();
System.out.println("字母的个数:"+englishCount);
System.out.println("空格的个数:"+spaceCount);
System.out.println("数字的个数:"+numCount);
}
}
* 3、输入某年某月某日,判断这一天是这一年的第几天(date)
package student.xiaoqiao.Day02;
//
//import java.text.SimpleDateFormat;
//import java.util.Date;
//public class San_Date {
// public static void main(String[] args) {
// Date d=new Date();
// SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// String ss=sim.format(d);
// System.out.println(ss);
// }
//}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class San_Date {
public static void main(String[] args) {
try {
Scanner sc = new Scanner(System.in);
System.out.print("输入时间(格式:yyyy-MM-dd)");
String str = sc.nextLine();
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
Date d = f.parse(str);
Calendar c = Calendar.getInstance();
c.setTime(d);
System.out.println(str + "是这一年的第" +
c.get(Calendar.DAY_OF_YEAR)
+ "天");
} catch (ParseException e) {
e.printStackTrace();
}
}
}
* 4、输入3个数由小到大的排序
package student.xiaoqiao.Day02
import java.util.Scanner;
public class Si_paixu {
public static void main(String[] args) {
System.out.println("请输入三个数:");
int[] qiao = new int[3];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
qiao[i] = scanner.nextInt();
}
for (int i = 0; i < qiao.length; i++) {
for (int j = i + 1; j < qiao.length; j++) {
if (qiao[i] > qiao[j]) {
int temp = qiao[i];
qiao[i] = qiao[j];
qiao[j] = temp;
}
}
System.out.print(qiao[i] + " ");
}
}
}
* 5、编写一个截取字符串的函数,输入为一个字符串和字节数输出为按字截取的字符串。

但是要保证汉字不被截半个,如:“我ABC”4,应该截为“我AB”,输入“我ABC 汉DEF”6,
* 应该输出为“我ABC”,而不是ABC+汉的半个。

(有点复杂可以先做别的)
*/
package com.day02;
public class Test05 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String ss = getString("s我是DW", 4);// 调用
ss=getString("我ABC汉DEF",6);
System.out.println(ss);
//拼接
// StringBuffer sss=new StringBuffer();
// sss.append("akd");
// sss.append(ss.charAt(0));
}
public static String getString(String s, int n) {
int index = 0;// 定义游标位置
StringBuffer ss = new StringBuffer();// 用来存放新的值
for (int i = 0; i <= n - 1; i++) {
if (s.charAt(index) < 255 && s.charAt(index) > 0|| Character.isDigit(s.charAt(index))) {// 如果当前字符是数字字符
ss.append(s.charAt(index));//append:参数将被追加到此序列。

此序列的长度将增加 1。

index++;
} else {
// 如果当前字符是汉字
if (index <= n - 2) { //
ss.append(s.charAt(index));//charAt(index)返回指定索引处的 char 值。

index++;
n--;// 当遇到汉字以后,因汉字占两个字节,n自动减1,例如"s我"总共占3个字节,"n"代表所取字节数,这时候已经占用了3个字节,当需要取2个
// 字节时,”s我“是点3个字节,"我"自动取消,当需要取4个字节时,”s我是“是占用5个字节,
}
}
}
return ss.toString();
}
}
6.将十进制数35转换为二进制数
package student.xiaoqiao.Day03;
public class Liu_shijinzhi {
public static void main(String[] args) {
System.out.println(Integer.toBinaryString(35));
}
}
7.将字符串数组按字典的顺序重新排列"Java","Basic ","C++"," Fortran","SmallTalk" package student.xiaoqiao.Day03;
import java.util.Arrays;
public class QI_paixu {
public static void main(String[] args) {
String[] s=new String[5];
s[0]="Java";
s[1]="Basic";
s[2]="C++";
s[3]="Fortran";
s[4]="SmallTalk";
Arrays.sort(s);
System.out.println(Arrays.toString(s));
}
}
8.输出杨辉三角效果图如下
package student.xiaoqiao.Day03;
import java.util.Scanner;
public class Ba_yanghui {
public static void main(String[] args) {
System.out.println("请输入一个整数:");
Scanner sc=new Scanner(System .in);
int b=sc.nextInt();
int[][] a = new int[b][b];
for (int i = 0; i < b; i++) {
a[i][i] = 1;
a[i][0] = 1;
}
for (int i = 2; i <b; i++) {
for (int j = 1; j < i; j++) {
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
for (int i = 0; i <b; i++) {
// for (int k = 0; k < 2 * (2*b - i) - 1; k++) {
// System.out.print(" ");
// }
for (int j = 0; j <=i; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
9.(附加题)输出N*N方阵
N输入为1时
N输入为2时。

N输入为5时
/**
* 2)、假设N=5,显示5*5的方阵的转圈结果
* 1 2 3 4 5
* 16 17 18 19 6
* 15 24 25 20 7
* 14 23 22 21 8
* 13 12 11 10 9
*
* 1、判断有多少个外循环,
* 2、四个for循环输出
*/
package com.day03Home;
import java.util.Scanner;
public class Test09 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int k=1;
Scanner sc=new Scanner(System.in);
System.out.print("请输入N*N方阵的数为:");
int N=sc.nextInt();
int[][] a=new int[N][N];
for(int i=0;i<=N/2;i++)
{
for(int j=i;j<N-i;j++) //0-4 1-3 2-2
{
a[i][j]=k++;
}
for(int j=i+1;j<N-i;j++) //1-4 2-3
{
a[j][N-i-1]=k++;
}
for(int j=N-i-2;j>=i;j--) //3-0 2-1
{
a[N-i-1][j]=k++;
}
for(int j=N-i-2;j>i;j--) //3-1
a[j][i]=k++;
}
for(int i=0;i<a.length;i++){
for(int j=0;j<a.length;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
}
}
10.五子棋游戏
11. 用递归来实现5!=5*4!=5*4*3!=5*4*3*2!=5*4*3*2*1输出5!的值
package student.xiaoqiao.Day03;
public class Shiyi_digui {
public static int fn(int n) {
if (n <= 1) {
return 1;
}
else {
return n * fn(n - 1);
}
}
public static void main(String[] args) {
System.out.println("5!="+fn(5));
}
}
12.0、1、1、2、3、5、8、13、21、34、……在数学上,斐波纳契数列以如下被以递归的方法定义:F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n≥2)
package student.xiaoqiao.Day03;
import java.util.Scanner;
public class Shier_Fn {
public static int fn(int n){
if (n==0) {
return 0;
}
else if (n==1) {
return 1;
}
else {
return fn(n-1)+fn(n-2);
}
}
public static void main(String[] args) {
System.out.println("请输入一个数:");
Scanner scanner=new Scanner(System.in);
int a=scanner.nextInt();
System.out.println(fn(a));
}
}
13.1二分法.
package student.xiaoqiao.Day04;
import java.util.Scanner;
public class Erfen {
public static void main(String[] args) {
int[] a = { 1, 21, 25, 44, 56 };
int min=0;
int max=a.length-1;
System.out.println("请输入一个数:");
Scanner scanner =new Scanner(System.in);
int b=scanner.nextInt();
int mid = (min + max) / 2;
// 比较a[mid]和value
while (a[mid]!=b) {
if (a[mid] > b) {
max=mid-1;
}
if (a[mid] < b) {
min=mid+1;
}
mid=(min + max) / 2;
}
if (a[mid] == b) {
System.out.println("这是第" + (mid + 1) + "个数");
}
}
}
13.2选择排序
for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
13.3冒泡排序
for (int i = 0; i < a.length-1; i++) {
for (int j = 0; j < a.length-i-1; j++) {
if (a[j]>a[j+1]) {
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
13.4插入排序
int[]a={4,10,2,15,6};
for (int i = 0; i < a.length; i++) {
int temp=a[i];
int j=i-1;
while(j!=-1&&temp<a[j]){
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
}
}
13.5快速排序
package test02;
import java.util.Arrays;
public class Kuaisupaixu {
public static void quickSort(int arr[], int low, int high) { int l = low;
int h = high;
int povit = arr[low];
while (l < h) {
while (l < h && arr[h] >= povit)
h--;
if (l < h) {
int temp = arr[h];
arr[h] = arr[l];
arr[l] = temp;
l++;
}
while (l < h && arr[l] <= povit)
l++;
if (l < h) {
int temp = arr[h];
arr[h] = arr[l];
arr[l] = temp;
h--;
}
}
if (l > low)
quickSort(arr, low, l - 1);
if (h < high)
quickSort(arr, l + 1, high);
}
public static void main(String[] args) {
int[] bb = new int[] {6,2,7,3,8,9};
quickSort(bb,0,5);
System.out.println(Arrays.toString(bb));
}
}
14.从控制台输入整数,将之转化为大写数字输出. 如输入103 ,输出壹佰零叁.。

相关文档
最新文档