importjava.util.Scanner;
publicclassTest{
publicstaticString[]input(){
Scannersc=newScanner(System.in);
String[]a=newString[5];
System.out.println("輸入名字:");
for(inti=0;i<a.length;i++){
a[i]=sc.nextLine();
}
returna;
}
publicstaticvoidmain(String[]args){
String[]b=input();
intran=(int)(Math.random()*b.length);
System.out.println("隨機輸出一個名字:"+b[ran]);
}
}
Ⅱ 大家幫忙做下這幾道java基礎編程題(謝了)
//-_-!~
import java.util.*;
public class TestOne {
static Scanner san = new Scanner(System.in);
public static void main(String[] args) {
String [] ps = new String[3];
System.out.print("Input name: ");
ps[0] = san.next();
System.out.print("Input sex: ");
ps[1] = san.next();
System.out.print("Input age: ");
ps[2] = san.next();
for(String s: ps) {
System.out.print(s + " ");
}
System.out.println("\n------------------------------------");
System.out.print("Input int: ");
int i = san.nextInt();
System.out.print("Input float: ");
float f = san.nextFloat();
System.out.print("Bigger one: " + TestOne.numberCompare(i, f));
System.out.println("\n-------------------------------------");
Employee e = new Employee(25, "XP1254", "TIGER", true);
System.out.println(e);
}
//重載
public static String numberCompare (int i, float f){
if(f - i > 0) {
return f +"";
} else {
return i +"";
}
}
//重載
public static String numberCompare (float f, int i){
if(f - i > 0) {
return f +"";
} else {
return i +"";
}
}
}
class Employee{
private String id;//string型代表員工ID號;
private String name;//:string型代表姓名;
private int age;//:int型代表年齡;
private boolean sex;//:boolen型代表性別(其中 true代表男,false代表女)
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public Employee(){
this(0, null, null, false);
}
public Employee(int age, String id, String name, boolean sex) {
this.age = age;
this.id = id;
this.name = name;
this.sex = sex;
}
public String toString(){
return this.getName() + ": " + this.getAge();
}
}
Ⅲ 用JAVA編程 類與對象的基礎題
class Phone{
private String phonenumber;
public void setPhonenumber(String phonenumber){
this.phonenumber=phonenumber;
}
public String getPhonenumber(){
return phonenumber;
}
public void recCall(){
System.out.println("接到一個電話");
}
public void telCall(){
System.out.println("撥出一個電話");
}
}class Fixedphone extends Phone{
private String phonenumber;//號碼是私有,設置為private,不可繼承
public void recCall(){
System.out.println("以"+this.phonenumber+"呼出了一個電話"); //重載了父類的recCall
}
}class Cordlessphone extends Fixedphone{
private String phonenumber;
public void info(){
System.out.println("這是無繩電話的信息");
}
}interface Moveable{
public void moveinfo();
}class Mobilephone extends Phone implements Moveable{
private String phonenumber;
public void moveinfo(){
System.out.println("我實現了可移動性");
}
}public class PhoneTest{
public static void main(String a[]){
Phone[] p=new Phone[5];
Phone p1=new Phone();
p1.setPhonenumber("123456789");
p[0]=p1;
Phone p2=new Phone();
p2.setPhonenumber("987654321");
p[1]=p2;
Mobilephone mp=new Mobilephone();
mp.setPhonenumber("11111");
p[2]=mp;
Fixedphone fp=new Fixedphone();
fp.setPhonenumber("22222");
p[3]=fp;
Cordlessphone cp=new Cordlessphone();
cp.setPhonenumber("33333");
p[4]=cp;
for(int i=0;i<p.length;i++){
System.out.println(p[i].getPhonenumber());
} p[4]=p[1];
System.out.println(p[4].getPhonenumber());
}} 寫的不是很好,希望對你有幫助噶
Ⅳ 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基礎編程題~
public class student{
private String name;
private int age;
public student(String name,int age){ //student的帶有String,int帶參數的構造函數
this.name = name;
this.age = age; //把傳進來的參數賦值給創建出來的類的對象
}
public static void main(String[] args){
student s = new student("zhangsan",18); //調用student的構造函數創 造一個對象s
System.out.println("姓名:"+s.name+"年齡:"+s.age);//調用對象的屬性就用s.name s.age!
}
}
----------------------------------------------------------------------
public class Student {
static String name;//設定String類型的靜態變數name
static int age;//設定int類型的靜態變數age
public static void main(String[] args){//main為主方法,運行從main開始
name="zhangsan";//給name賦值zhangsan
age=18;//給age賦值18
System.out.println("該學生的名字為"+name+"年齡為:"+age);//輸出name和age
}
}
static 修飾的屬性和方法只屬於這個類本身,而不屬於這個類所創建出來的對象,所以可以直接寫name,age 而不用像上面那種要先創建個類的對象出來 !
Ⅵ 關於JAVA的基礎編程題
頂樓上,還有題目很爛,前後類型要一直,否則會丟精度,就拿工資來說吧
定義float型,最後計算返回int型,還有就是計算工資時,根本不需要傳參數,因為這個類已經有足夠的數據支持了,不知道getDecMoney(int day)方法中day的有什麼意義
public class Employee {
int id;
byte sex;
String name;
String ty;
float salary;
int holidays;
Employee(int id,byte sex,String name,String ty,float salary,int holidays)
{
this.id=id;
this.sex=sex;
this.name=name;
this.ty=ty;
this.salary=salary;
this.holidays=holidays;
}
public void dispaly()
{
System.out.println("員工姓名:"+this.name);
if(this.sex==(byte)0)
System.out.println("員工性別:男");
else
System.out.println("員工性別:女");
System.out.println("員工職務:"+this.ty);
}
public int getDecMoney(int day)
{
if(day<=3)
this.salary-=30*day;
else
this.salary-=50*day;
return (int)this.salary;
}
public static void main(String[] args)
{
Employee employee=new Employee(123,(byte) 0,"張三","科長",3000.0f,2);
employee.dispaly();
System.out.println("該員工本月工資:"+employee.getDecMoney(employee.holidays));
}
}
Ⅶ JAVA基礎編程題
package com.qiu.swing.layoutDemo;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.JTextField;
/**
*
* @author Qiu
*
*/
public class TextDemo extends JFrame{
final JButton button_show = new JButton("顯示");
final JButton button_clear = new JButton("顯示");
final JTextField text = new JTextField();
final Container con = this.getContentPane();
public TextDemo() {
this.setTitle("HelloWorld!");
this.setSize(300, 160);
// 居中
this.setLocationRelativeTo(null);
this.setUndecorated(true); // 去掉窗口的裝飾
this.setResizable(false);
this.getRootPane().setWindowDecorationStyle(
JRootPane.INFORMATION_DIALOG);// 採用指定的窗口裝飾風格
// 文字居中
text.setSize(100, 20);
Box vbox = Box.createVerticalBox();
Box xbox0 = Box.createHorizontalBox();
xbox0.add(text);
xbox0.add(button_show);
xbox0.add(button_clear);
vbox.add(xbox0);
vbox.add(Box.createVerticalStrut(100));
con.setLayout(new BoxLayout(con, BoxLayout.X_AXIS));
con.add(vbox);
button_show.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setText("HelloWorld");
}
});
button_clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setText("");
}
});
}
public static void main(String[] args) {
TextDemo home = new TextDemo();
home.setVisible(true);
}
}
Ⅷ 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 );
引入類:
Ⅸ 基礎JAVA編程題 請高手作解
3.
public class c4_16 extends Applet
{ public void init()
{ double a=2,b=3;
double z1=Math.pow(a,b); //引用Math類的pow方法求a的b次方
double z2=Math.sqrt(9); //引用Math類的sqrt方法求9的平方根
System.out.print("z1="+z1);
System.out.print("\tz2="+z2);
}
}
6.
public class BB
{
static int sum;
void Sum50(int a)
{
int i=a;
sum=sum+i;
if(i>0)
{
a--;
Sum50(a);
}
}
public static void main(String args[])
{
BB b=new BB();
b.Sum50(50);
System.out.println(sum);
}
}