Java基础编程题(java基础编程题及答案百度云)
5道简单的JAVA编程题(高分悬赏)
很详细的帮你写下,呵呵,所以要给分哦!
1、
(1)源程序如下:
public class One {
public static void main(String[] args) {
String name = "张三";
int age = 23;
char sex = '男';
String myclass = "某某专业2班";
System.out.println("姓名:" + name);
System.out.println("姓名:" + age);
System.out.println("姓名:" + sex);
System.out.println("姓名:" + myclass);
}
}
(2)
编写完程序的后缀名是.java,如本题,文件名就是One.java。
开始\运行\cmd,进入“命令提示符窗口”,然后用javac编译器编译.java文件,语句:javac One.java。
(3)
编译成功后,生成的文件名后缀是.class,叫做字节码文件。再用java解释器来运行改程序,语句:java One
2、编写程序,输出1到100间的所有偶数
(1)for语句
public class Two1 {
public static void main(String[] args) {
for(int i=2;i=100;i+=2)
System.out.println(i);
}
}
(2)while语句
public class Two2 {
public static void main(String[] args) {
int i = 2;
while (i = 100) {
System.out.println(i);
i += 2;
}
}
}
(3)do…while语句
public class Two3 {
public static void main(String[] args) {
int i = 2;
do {
System.out.println(i);
i += 2;
}while(i=100);
}
}
3、编写程序,从10个数当中找出最大值。
(1)for循环
import java.util.*;
public class Three1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
for (int i = 0; i 10; i++) {
System.out.print("输入第" + (i + 1) + "个数:");
number = input.nextInt();
if (max number)
max = number;
}
System.out.println("最大值:" + max);
}
}
(2)while语句
import java.util.*;
public class Three2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
while (i 10) {
System.out.print("输入第" + (i + 1) + "个数:");
number = input.nextInt();
if (max number)
max = number;
i++;
}
System.out.println("最大值:" + max);
}
}
(3)do…while语句
import java.util.*;
public class Three3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
do {
System.out.print("输入第" + (i + 1) + "个数:");
number = input.nextInt();
if (max number)
max = number;
i++;
}while(i10);
System.out.println("最大值:" + max);
}
}
4、编写程序,计算从1到100之间的奇数之和。
(1)for循环
public class Four1 {
public static void main(String[] args) {
int sum=0;
for(int i = 1;i=100;i+=2){
sum+=i;
}
System.out.println("1~100间奇数和:" + sum);
}
}
(2)while语句
public class Four2 {
public static void main(String[] args) {
int sum = 0;
int i = 1;
while (i = 100) {
sum += i;
i += 2;
}
System.out.println("1~100间奇数和:" + sum);
}
}
(3)do…while语句
public class Four3 {
public static void main(String[] args) {
int sum = 0;
int i = 1;
do {
sum += i;
i += 2;
} while (i = 100);
System.out.println("1~100间奇数和:" + sum);
}
}
5、
(1)什么是类的继承?什么是父类?什么是子类?举例说明。
继承:是面向对象软件技术当中的一个概念。如果一个类A继承自另一个类B,就把这个A称为"B的子类",而把B称为"A的父类"。继承可以使得子类具有父类的各种属性和方法,而不需要再次编写相同的代码。在令子类继承父类的同时,可以重新定义某些属性,并重写某些方法,即覆盖父类的原有属性和方法,使其获得与父类不同的功能。另外,为子类追加新的属性和方法也是常见的做法。继承需要关键字extends。举例:
class A{}
class B extends A{}
//成员我就不写了,本例中,A是父类,B是子类。
(2)编写一个继承的程序。
class Person {
public String name;
public int age;
public char sex;
public Person(String n, int a, char s) {
name = n;
age = a;
sex = s;
}
public void output1() {
System.out.println("姓名:" + name + "\n年龄:" + age + "\n性别:" + sex);
}
}
class StudentPerson extends Person {
String school, department, subject, myclass;
public StudentPerson(String sc, String d, String su, String m, String n,
int a, char s) {
super(n, a, s);
school = sc;
department = d;
subject = su;
myclass = m;
}
public void output2() {
super.output1();
System.out.println("学校:" + school + "\n系别:" + department + "\n专业:"
+ subject + "\n班级:" + myclass);
}
}
public class Five2 {
public static void main(String[] args) {
StudentPerson StudentPersonDemo = new StudentPerson("某某大学", "某某系别",
" 某专业", "某某班级", " 张三", 23, '男');
StudentPersonDemo.output2();
}
}
Java 基础编程题求解,不是很懂
第一种:方式借助于,while循环获取,提示输入内容获取输入值,然后判断如果余数为5结束循环。
int i = 0;
do{
System.out.println("请输入数据边界值:");
//获取输入数字
Scanner sc = new Scanner(System.in);
int s = sc.nextInt();
i = s%10;
if(i == 5){
System.out.println(s);
sc.close();
}
}while( i == 5 );
引入类:
第二种:用for循环实现可以控制一定的循环次数。
public ?static ?void testFor(){
for (int i = 0; i = 10 ; i++) {
System.out.println("请输入数据边界值:");
//获取输入数字
Scanner sc = new Scanner(System.in);
int s = sc.nextInt();
int b = 0;
b = s%10;
if(b == 5){
System.out.println(s);
sc.close();
break;
}
}
}
第三种:让用户输入一个字符串,数字按逗号分隔,然后判断其中有多少数字余数为5
//实现输入一个字符窜数字集合数字以逗号分隔
public ?static ?void testString(){
System.out.println("请输入数据边界值:");
//获取输入数字
Scanner sc = new Scanner(System.in);
String s = sc.next();
if(s != null s.length() 0){
String [] sNum = s.split(",");
for (int i = 0; i ?sNum.length ; i++) {
int sn = Integer.valueOf(sNum[i]);
int b = 0;
b = sn % 10;
if(b == 5){
System.out.println(sn);
}
}
}
//关闭输入流
sc.close();
}
java编程题 本人新手,求详解。
先看下最终的结果吧,是不是你想要的?
项目结构如下图所示:
其中,Student是父类,PostGraduate是子类,继承自父类Student,Main是主类,用于创建对象以及把这些对象的功能调用起来。
---------------------------Student代码如下:------------------------------
/**
* 学生类
* @author 逍遥
*
*/
public class Student {
//学号
private int sId;
//姓名
private String sName;
//数学成绩
private double mathScore;
//计算机成绩
private double computerScore;
/**
* 获取学号
* @return
*/
public int getsId() {
return sId;
}
/**
* 设置学号
* @param sId
*/
public void setsId(int sId) {
this.sId = sId;
}
/**
* 获取姓名
* @return
*/
public String getsName() {
return sName;
}
/**
* 设置姓名
* @param sName
*/
public void setsName(String sName) {
this.sName = sName;
}
/**
* 获取数学成绩
* @return
*/
public double getMathScore() {
return mathScore;
}
/**
* 设置数学成绩
* @param mathScore
*/
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
/**
* 获取计算机成绩
* @return
*/
public double getComputerScore() {
return computerScore;
}
/**
* 设置计算机成绩
* @param computerScore
*/
public void setComputerScore(double computerScore) {
this.computerScore = computerScore;
}
/**
* 输出成员变量(4个成员变量)的信息。
*/
public void print(){
System.out.println("学号:"+sId);
System.out.println("姓名:"+sName);
System.out.println("计算机成绩:"+mathScore);
System.out.println("数学成绩:"+computerScore);
}
}
---------------------------Student代码结束------------------------------
---------------------------PostGraduate代码如下:------------------------------
/**
* 研究生类
* @author 逍遥
*
*/
public class PostGraduate extends Student{
//导师姓名
private String tName;
//研究方向
private String ResearchDirection;
/**
* 获取导师姓名
* @return
*/
public String gettName() {
return tName;
}
/**
* 设置导师姓名
* @param tName
*/
public void settName(String tName) {
this.tName = tName;
}
/**
* 获取研究方向
* @return
*/
public String getResearchDirection() {
return ResearchDirection;
}
/**
* 设置研究方向
* @param researchDirection
*/
public void setResearchDirection(String researchDirection) {
ResearchDirection = researchDirection;
}
/**
* 研究生类重写父类的void print()方法,功能是输出成员变量(6个成员变量)的信息
*/
@Override
public void print() {
// TODO Auto-generated method stub
super.print();
System.out.println("导师姓名:"+tName);
System.out.println("研究方向:"+ResearchDirection);
}
}
---------------------------PostGraduate代码结束------------------------------
---------------------------Main代码如下:------------------------------
import java.util.Scanner;
/**
* 主类
* @author 逍遥
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//用于获取从键盘上输入的信息
Scanner input=new Scanner(System.in);
//创建一个Student类的对象
Student student=new Student();
//从键盘上输入其属性信息
System.out.print("请输入学生的学号:");
student.setsId(input.nextInt());
System.out.print("请输入学生的姓名:");
student.setsName(input.next());
System.out.print("请输入学生的数学成绩:");
student.setMathScore(input.nextDouble());
System.out.print("请输入学生的计算机成绩:");
student.setComputerScore(input.nextDouble());
//并且通过其print方法输出这些信息;
student.print();
//创建一个PostGraduate类的对象
PostGraduate postGraduate=new PostGraduate();
//从键盘上输入其属性信息
System.out.print("请输入研究生的学号:");
postGraduate.setsId(input.nextInt());
System.out.print("请输入研究生的姓名:");
postGraduate.setsName(input.next());
System.out.print("请输入研究生的数学成绩:");
postGraduate.setMathScore(input.nextDouble());
System.out.print("请输入研究生的计算机成绩:");
postGraduate.setComputerScore(input.nextDouble());
System.out.print("请输入研究生的导师姓名:");
postGraduate.settName(input.next());
System.out.print("请输入研究生的研究方向:");
postGraduate.setResearchDirection(input.next());
//并且通过其print方法输出这些信息。
postGraduate.print();
}
}
---------------------------Main代码结束------------------------------
=================知识点的简单总结=================
本题考察的知识点是面向对象的三大特性之一:继承。
Student为父类,包含了学号、姓名、数学成绩和计算机成绩4个属性,以及一个print()方法。
PostGraduate 继承父类的时候,继承了父类中的所有方法,因为方法我都是用的public,而属性继承不了,因为我在父类中用了封装,所有属性都用private修饰了,想访问属性的话,必须通过get、set方法,这里,我重写了父类中的print方法,通过super.print();调用了父类中的print()方法。
最后就是Main类,提供了main方法作为入口函数,用于按要求声明这些对象以及去调用对象中的方法。
有关java编程题目?
按照题目要求编写的圆,圆锥和测试类的Java程序如下
Test.java文件内容如下
class Circle{
private double r;
private String color;
public Circle(double r){
this.r=r;
}
public double area(){
return Math.PI*r*r;
}
public double perimeter(){
return Math.PI*2*r;
}
public double getR(){
return this.r;
}
public void setR(double r){
this.r=r;
}
public String getColor(){
return this.color;
}
public void setColor(String color){
this.color=color;
}
public String toString(){
return "圆的半径为"+r+",颜色为"+color;
}
}
class Cone{
private Circle c;
private double h;
private String color;
public Cone(Circle c,double h){
this.c=c;
this.h=h;
}
public double volume(){
return 1.0/3*c.area()*h;
}
public Circle getCircle(){
return this.c;
}
public void setCircle(Circle c){
this.c=c;
}
public double getH(){
return this.h;
}
public void setH(double h){
this.h=h;
}
public String getColor(){
return this.color;
}
public void setColor(String color){
this.color=color;
}
public String toString(){
return "圆锥的底面积为"+c.area()+",高为"+h+",颜色为"+color;
}
}
public class Test{
public static void main(String[] args){
Circle circle1=new Circle(2.5);
circle1.setColor("红色");
System.out.println(circle1.toString());
System.out.println("圆的面积为"+circle1.area());
System.out.println("圆的周长为"+circle1.perimeter());
Cone circlar1=new Cone(circle1,2.7);
circlar1.setColor("蓝色");
System.out.println(circlar1.toString());
System.out.println("圆锥的体积为"+circlar1.volume());
}
}

JAVA编程题求解?
这种作业,最好还是结合书上知识,理解清楚老师布置的目的、怎么实现的
public class Frog {
private String name;
private Integer distance = 0;
//跳跃方法
void jump() {
//随机10-20
int jumpDistance = (int) (10 + Math.random() * (20 - 10 + 1));
this.distance += jumpDistance;
}
//带名字构造方法
Frog(String name) {
this.name = name;
}
public static void main(String[] args) {
Frog a = new Frog("a");
Frog b = new Frog("b");
Frog c = new Frog("c");
Frog d = new Frog("d");
for (int i = 0; i 10; i++) {
a.jump();
b.jump();
c.jump();
d.jump();
}
System.out.println(a.name + "总距离=" + a.distance);
System.out.println(b.name + "总距离=" + b.distance);
System.out.println(c.name + "总距离=" + c.distance);
System.out.println(d.name + "总距离=" + d.distance);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getDistance() {
return distance;
}
public void setDistance(Integer distance) {
this.distance = distance;
}
}
java基础编程题求解,请写的基础一点不然根本看不懂啊
import?java.util.Scanner;
/*
?*?@author?ww
?*
?*/
public?class?Test?{
?public?static?void?main(String[]?args)?{
??Scanner?s?=?new?Scanner(System.in);
??System.out.println("输入一个正整数n(n=2)");
??int?n?=?s.nextInt();
??StringBuilder?sb?=?new?StringBuilder();
??boolean?has?=?false;
??
??for(int?i=1;in;i++){
???int?temp?=?i;
???sb.append(i).append("?");
???for(int?j=i+1;jn;j++){
????temp?+=?j;
????sb.append(j).append("?");
????if(temp==n){
?????System.out.println(sb.toString());
?????has?=?true;
????}
???}
???sb.delete(0,?sb.length());
??}
??if(!has){
???System.out.println("NONE");
??}
}
}