本文目錄一覽:
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/zh-tw/n/154968.html