当前位置:首页 > java练习题(含答案)
1.
编写程序,用数组实现乘法小九九的存储和输出。【提示:采用多个一维数组。】 public class Multipation {
public static void main(String[] args) { // TODO Auto-generated method stub int x[][]=new int[9][9]; for(int i=0;i<9;i++){ for(int j=0;j<9;j++){ if(i>=j){ int m=i+1; }
}
}
int n=j+1;
x[i][j]=m*n;
System.out.print(m+\+n+\+x[i][j]);
}
}
System.out.println();
2. 定义一个类Student,属性为学号、姓名和成绩;方法为增加记录SetRecord和得到记录GetRecord。SetRecord给出学号、姓名和成绩的赋值,GetRecord通过学号得到考生的成绩。
public class Student { }
/**
* @param args */
private int ID; private String name; private float score;
public void SetRecord(int ID,String name,float score){ }
public float getRecord(int ID){ }
if(ID==this.ID) return this.score; else return -1; }
// TODO Auto-generated method stub Student s=new Student(); s.SetRecord(0,\,100); float Sco=s.getRecord(0); System.out.print(Sco); this.ID=ID; this.name=name; this.score=score;
public static void main(String[] args) {
3. 给出上题中设计类的构造函数,要求初始化一条记录(学号、姓名、成绩)。
4. public class Student { 5. 6. /**
7. * @param args 8. */
9. private int ID; 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. }
public static void main(String[] args) { }
// TODO Auto-generated method stub Student s=new Student(0,\,12); //s.SetRecord(0,\ float Sco=s.getRecord(0); System.out.print(Sco); private String name; private float score;
Student(int ID,String name,float score){ }
public void SetRecord(int ID,String name,float score){ }
public float getRecord(int ID){
if(ID==this.ID) return this.score; else return -1; }
this.ID=ID; this.name=name; this.score=score;
this.ID=0; this.name=\; this.score=65;
4. 编写程序,测试字符串“你好,欢迎来到Java世界”的长度,将字符串的长度转换成字符串进行输出,并对其中的“Java”四个字母进行截取,输出截取字母以及它在字符串中的位置。
public class StringTest { }
/**
* @param args */
public static void main(String[] args) { }
// TODO Auto-generated method stub String str=\你好,欢迎来到Java世界\; int length=str.length();
char [] stringArr = str.toCharArray(); //System.out.print(stringArr); for(int i=0;i //System.out.print(stringArr[i]); //System.out.print(stringArr[0]); if('J'==stringArr[i]){ } System.out.print(i); }
共分享92篇相关文档