本文目录一览:
java入门编程题:某班有十位同学,请顺序输入十位同学的学号,保存在数组中,并输出所有同学的学号?
import java.util.Scanner;
public class Students {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] students=new String[10];
String No=null;
for (int i = 0; i 10 ; i++) {
System.out.println(“请输入学号:”);
No=in.next();
students[i]=No;
}
System.out.println(“学号是:”);
for (String a:students) {
System.out.print(a+” “);
}
}
}
java编程题目
这不都说的很清楚了么。。。。。。。。
自己写吧,也没啥难度。
是完全不知道这个题目再说什么么?
package spring5.source;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class D extends JFrame {
public static void main(String[] args) {
D d = new D();
d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
d.setSize(500, 500);
d.setLayout(new FlowLayout());
TextField t1 = new TextField();
TextField t2 = new TextField();
Button b = new Button(“OK”);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String v1 = t1.getText();
try {
int n = Integer.parseInt(v1);
Double d = Math.pow(n, 2);
t2.setText(String.valueOf(d.intValue()));
} catch (Exception e2) {
e2.printStackTrace();
}
}
});
t1.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_ENTER) {
String v1 = t1.getText();
try {
int n = Integer.parseInt(v1);
Double d = Math.pow(n, 2);
t2.setText(String.valueOf(d.intValue()));
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
});
// KeyListener key_Listener = ;
d.add(t1);
d.add(t2);
d.add(b);
d.setVisible(true);
}
}
少了一个 d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 关闭窗口的
有关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编程题
//圆类Circle
import java.util.Scanner;
public class Circle {
private double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
//无参构造函数
public Circle(){
this.radius=0;
}
//带参构造函数
public Circle(double r ){
this.radius=r;
}
//获取面积
public double getArea(){
double r=this.radius;
double area=r*r*3.14;
return area;
}
//获取周长
public double getPerimeter(){
double perimeter=this.radius*2*3.14;
return perimeter;
}
//打印圆的信息
public void show(){
System.out.println(“请输入圆的半径”);
Scanner sc=new Scanner(System.in);
this.setRadius(sc.nextInt());
System.out.println(“圆的半径”+this.getRadius());
System.out.println(“圆的面积”+this.getArea());
System.out.println(“圆的周长”+this.getPerimeter());
}
}
//圆柱类
public class Cylinder extends Circle {
//圆柱高
private double height;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
//构造方法
public Cylinder (double r, double h){
super();
this.height=h;
this.setRadius(r);
}
public double getVolume( ) {
double volume=this.getArea()*this.height;
return volume;
}
//求体积
public void showVolume(){
System.out.println(“圆柱的体积”+this.getVolume());
}
}
//主程序入口
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Circle circle=new Circle();
circle.show();
Cylinder cylinder=new Cylinder(2.0,8.5);
cylinder.showVolume();
}
}
PS:注释写的很详尽了,求采纳
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/154968.html