1.在日常的工作开发中,我们经常需要给一个对象中的属性赋值,现在需要您创建一个自定义注解,来帮助我们完成属性赋值。请您将 MyAnnotation
类修改成一个自定义注解,并添加属性 name(String)
和 age(int)
。您可以查看 Main
类中的执行流程,来观察程序的运行机制。
import java.lang.annotation.*;
@SuppressWarnings("all")
//注释加入文档,会被javadoc命令识别
@Target({ElementType.FIELD})
//注释保留到什么阶段
@Retention(RetentionPolicy.RUNTIME)
//指定注解用在哪些目标上,相当于作用域,比如方法,类
public @interface MyAnnotation {
String name();
int age();
}
2.输出换行的Hello World!,输出不换行的Hello World!
public class HelloWorld {
/* Your first java code
* print Hello World to console
*/
public static void main(String[] args) {
// write your code here
// use System.out.println to print Hello World to console.
//输出换行的Hello World!
System.out.println("Hello World!");
//输出不换行的Hello World!
System.out.print("Hello World!");
}
}
3.用拼接的方式打印字符串 , 当 name
取值为 “Jack” 并且 phone
取值为 “15570729587” 的时候,你的代码应该输出: Jack: 15570729587
public class Solution {
public String splice(String name, String phone) {
// write your code here
//方法一,最直观,效率最低
// return name+": "+phone;
//方法二,数据量大,多线程的情况
// StringBuffer sb = new StringBuffer();
// return(sb.append(name).append(": ").append(phone).toString());
//方法三,数据量大,单线程的情况
StringBuilder sb = new StringBuilder();
return(sb.append(name).append(": ").append(phone).toString());
}
}
4. 计算 1+2+3+…+n 的值
public class Solution {
public int add(int number) {
int i;
int sum = 0;
// write your code here
if(number>0){
for(i=1;i<=number;i++){
sum+=i;
}
return sum;
}else{
sum=0;
return sum;
}
}
}
5. 打印三角九九乘法表
public class Solution {
public static void main(String[] args) {
// write your code here
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();
}
}
}
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
6.四舍五入,当数组为 101 55 4 -88 777 555
的时候,你的代码应该返回:[100, 60, 0, -88, 780, 560]
public class Solution {
public int[] rounding(int[] array) {
// write your code here
for(int i=0;i<array.length;i++){
if(array[i]<0){
continue;
}
if((array[i]%10)>=5){
array[i] = ((array[i]+10)-(array[i]%10));
}else{
array[i] = (array[i]-(array[i]%10));
}
}
return array;
}
}