當前位置:首頁 » 編程軟體 » java編程基礎答案

java編程基礎答案

發布時間: 2022-03-06 21:08:15

A. java編程,在線等答案

package mytest;
interface Number
{
int compare(Number n);
double getValue();
String toString();
}
class Fraction implements Number
{
int n1;//分子
int n2;//分母
public int getN1() {
return n1;
}
public void setN1(int n1) {
this.n1 = n1;
}
public int getN2() {
return n2;
}
public void setN2(int n2) {
this.n2 = n2;
}
public String toString()
{
System.out.println(this.getValue());
return "";
}
@Override
public int compare(Number n) {
if(this.getValue()>n.getValue())
return 1;
else if(this.getValue()<n.getValue())
return -1;
return 0;
}
@Override
public double getValue() {
double result = 0.0;
if(n2 != 0)
{
result = (double)n1 / n2;
}
return result;
}
// public String toString()
// {
//
// }
}
class Complex implements Number{
int n1;//實部
int n2;//虛部
public int getN1() {
return n1;
}
public void setN1(int n1) {
this.n1 = n1;
}
public int getN2() {
return n2;
}
public void setN2(int n2) {
this.n2 = n2;
}
@Override
public int compare(Number n) {
if(this.getValue()>n.getValue())
return 1;
else if(this.getValue()<n.getValue())
return -1;
return 0;
}

public String toString()
{
System.out.println(this.getValue());
return "";
}

@Override
public double getValue() {
return Math.pow((Math.pow(n1, 2)+Math.pow(n2, 2)), 0.5);
}
}
public class Jxxxx {
public static void main(String[] args)
{
//測試分數
Fraction f1 = new Fraction();
f1.setN1(3);
f1.setN2(5);
Fraction f2 = new Fraction();
f2.setN1(1);
f2.setN2(2);
double val1 = f1.getValue();
double val2 = f2.getValue();
f1.toString();
f2.toString();
int result = f1.compare(f2);
System.out.println(result);

//測試復數
Complex c1 = new Complex();
c1.setN1(3);
c1.setN2(4);
Complex c2 = new Complex();
c2.setN1(5);
c2.setN2(12);
double v1 = c1.getValue();
double v2 = c2.getValue();
c1.toString();
c2.toString();
int res = c1.compare(c2);
System.out.println(res);
System.out.println(c1.toString());
}
}

B. 《Java語言程序設計基礎篇》第六版的練習題和編程題答案


哥們我給你寫完了,耽誤了我半個小時的時間啊!你直接運行就可以了
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Constellation implements ActionListener{
private JFrame frame = null;
private JTextField year = null;
private JTextField month = null;
private JTextField day = null;
private JLabel label1 = null;
private JLabel label2 = null;
private JLabel label3 = null;
private JPanel panel1 = null;
private JPanel panel2 = null;
private JButton button = null;
private JTextField output = null;
public static final String[] zodiacArr = { "猴", "雞", "狗", "豬", "鼠", "牛", "虎", "兔", "龍", "蛇",
"馬", "羊" };

public static final String[] constellationArr = { "水瓶座", "雙魚座", "牡羊座", "金牛座", "雙子座", "巨蟹座",
"獅子座", "處女座", "天秤座", "天蠍座", "射手座", "魔羯座" };

public static final int[] constellationEdgeDay = { 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22,
22 };

/**
* * 根據日期獲取生肖 *
* @return 11.
*/
public static String date2Zodica(Calendar time) {
return zodiacArr[time.get(Calendar.YEAR) % 12];
}

/**
* * 根據日期獲取星座 *
* @param time *
* @return
*/
public static String date2Constellation(Calendar time) {
int month = time.get(Calendar.MONTH);
int day = time.get(Calendar.DAY_OF_MONTH);
if (day < constellationEdgeDay[month]) {
month = month - 1;
}
if (month >= 0) {
return constellationArr[month];
}
// default to return 魔羯
return constellationArr[11];
}

public Constellation(){
frame = new JFrame("計算生肖,星座");
year = new JTextField("",3);
month = new JTextField("",3);
day = new JTextField("",3);
label1 = new JLabel("請輸入年份:");
label2 = new JLabel(",請輸入月份:");
label3 = new JLabel(",請輸入日期:");
button = new JButton("查看結果");
button.addActionListener(this);
panel1 = new JPanel();
panel1.setLayout(new FlowLayout(FlowLayout.CENTER));
panel1.add(label1);
panel1.add(year);
panel1.add(label2);
panel1.add(month);
panel1.add(label3);
panel1.add(day);
panel1.add(button);
frame.setLayout(new BorderLayout());
frame.add(panel1,BorderLayout.NORTH);
panel2 = new JPanel();
output = new JTextField("",40);
panel2.add(output,JPanel.CENTER_ALIGNMENT);
frame.add(panel2,BorderLayout.CENTER);
frame.setSize(500, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
output.setText("");
int y = Integer.parseInt(year.getText());
int m = Integer.parseInt(month.getText());
int d = Integer.parseInt(day.getText());

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, y);
calendar.set(Calendar.MONTH, m);
calendar.set(Calendar.DAY_OF_MONTH, d);
String zodica = date2Zodica(calendar);
String constellation = date2Constellation(calendar);
String str = "您輸入的日期為:"+y+"年-"+m+"-月"+d+"日,得到的生肖:"+zodica+",星座:"+constellation;
output.setText(str);
}

//testcode
public static void main(String[] args) {
new Constellation();
}
}

C. 誰知道java編程基礎應用與實例課後習題答案在那下載

哥們,不妨到你們的學校圖書館看一下,我們當時上學的用的那個Java課本,我們學校圖書館有專門和之對應的課後習題解答的書。

D. 求一題簡單的java編程題答案

public class Test {

public static void main(String[] args) {
int sum = 0, j;
boolean isPrime = true;
for (int i = 2; i <= 20; i++) {
for (j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
sum += i;
} else {
isPrime = true;
}
}
try {
sum = sum / 0;
} catch (Exception e) {
System.out.println("和為:"+sum+",且除數不能為0");
}
}
}

E. java編程作業,求答案

import java.awt.*;
import java.awt.event.*;

public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("姓名:xxx");
frame.setLayout(new BorderLayout());
Panel panel1 = new Panel();
Panel panel2 = new Panel();
TextField tf = new TextField(20);
panel1.add(tf);
panel2.setLayout(new GridLayout(4,4));

panel2.add(new Button("0"));
panel2.add(new Button("1"));
panel2.add(new Button("2"));
panel2.add(new Button("3"));
panel2.add(new Button("4"));
panel2.add(new Button("5"));
panel2.add(new Button("6"));
panel2.add(new Button("7"));
panel2.add(new Button("8"));
panel2.add(new Button("9"));
panel2.add(new Button("A"));
panel2.add(new Button("B"));
panel2.add(new Button("C"));
panel2.add(new Button("D"));
panel2.add(new Button("E"));
panel2.add(new Button("F"));

frame.add(panel1, BorderLayout.NORTH);
frame.add(panel2, BorderLayout.SOUTH);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
}

F. 求Java編程的答案

importjava.util.Arrays;

classStudent{

privateStringname;
privateintage;
privateStringsex;

privatedoubleexamDesign;
privatedoubleexamDB;
privatedoubleexamXML;

publicStudent(){
}

publicStudent(Stringname,intage,booleansex){
}

publicStudent(Stringname,intage,Stringsex,doubleexamDesign,
doubleexamDB,doubleexamXML){
this.name=name;
this.age=age;
this.sex=sex;
this.examDesign=examDesign;
this.examDB=examDB;
this.examXML=examXML;
}

publicStringgetName(){
returnname;
}

publicvoidsetName(Stringname){
this.name=name;
}

publicStringgetSex(){
returnsex;
}

publicvoidsetSex(Stringsex){
this.sex=sex;
}

publicintgetAge(){
returnage;
}

publicvoidsetAge(intage){
this.age=age;
}

publicdoublegetExamDesign(){
returnexamDesign;
}

publicvoidsetExamDesign(doubleexamDesign){
this.examDesign=examDesign;
}

publicdoublegetExamDB(){
returnexamDB;
}

publicvoidsetExamDB(doubleexamDB){
this.examDB=examDB;
}

publicdoublegetExamXML(){
returnexamXML;
}

publicvoidsetExamXML(doubleexamXML){
this.examXML=examXML;
}

publicStringsubtotal(){
return"總成績:"+(examDesign+examDB+examXML);
}

publicStringavgScore(){
return"平均分:"+(examDesign+examDB+examXML)/3;
}

publicStringtopScore(){
double[]arr={examDesign,examDB,examXML};
Arrays.sort(arr);
return"最高分:"+arr[2];
}

publicStringbottomScore(){
double[]arr={examDesign,examDB,examXML};
Arrays.sort(arr);
return"最低分:"+arr[0];
}

@Override
publicStringtoString(){
returnname+"的年齡是"+age+",性別是"+sex+",程序設計成績是"+examDesign
+",資料庫成績是"+examDB+",XML成績是"+examXML+"]";
}
}

publicclassTestStudent{
publicstaticvoidmain(String[]args){
Students=newStudent("小明",22,"男",87,88,89);
System.out.println(s.avgScore());
System.out.println(s.bottomScore());
System.out.println(s.topScore());
System.out.println(s.subtotal());
System.out.println(s);
}
}

G. java編程解答

第一個:

publicclassApp1{

staticintfib(intn){
if(n==0||n==1){
return1;
}

returnfib(n-1)+fib(n-2);
}

publicstaticvoidmain(String[]args){

for(inti=20;i<=30;i++){
System.out.print(Integer.toString(fib(i))+"");
}

}
}

第二個:

publicclassApp2{

publicstaticvoidmain(String[]args){
for(inti=0;i<=20;i++){
for(intj=0;j<=33;j++){
for(intk=0;k<=100;k++){
if(i*5+j*3+k/3==100&&i+j+k==100&&k%3==0)
System.out.println("公雞:"+i+"母雞:"+j+"小雞:"+k);
}
}
}
}
}

第三個:

importjava.io.FileWriter;
importjava.io.IOException;
importjava.util.*;

classBook{

privateStringtitle;

privateStringauthor;

privateintpageCount;

publicBook(){
this("","",0);
}

publicBook(Stringtitle,Stringauthor,intpageCount){
this.title=title;
this.author=author;
this.pageCount=pageCount;
}

publicStringgetTitle(){
returntitle;
}

publicvoidsetTitle(Stringtitle){
this.title=title;
}

publicStringgetAuthor(){
returnauthor;
}

publicvoidsetAuthor(Stringauthor){
this.author=author;
}

publicintgetPageCount(){
returnpageCount;
}

publicvoidsetPageCount(intpageCount){
this.pageCount=pageCount;
}

publicvoidprint(){
System.out.println(title+" "+author+" "+pageCount);
}

publicStringgetBookInfo(){
return"書名:"+title+",作者:"+author+",頁數:"+pageCount;
}
}

{

privatestaticScannerscanner=newScanner(System.in);

privatestaticList<Book>books=newArrayList<Book>();

privatestaticBookinput(){

Bookbook=newBook();

System.out.print("請輸入書名:");
book.setTitle(scanner.next());

System.out.print("請輸入作者:");
book.setAuthor(scanner.next());

System.out.print("請輸入頁數:");
book.setPageCount(scanner.nextInt());

returnbook;
}

publicstaticvoidmain(String[]args)throwsIOException{

while(true){

System.out.print("是否添加圖書?(true/false)");

if(!scanner.nextBoolean()){
break;
}

Bookbook=input();

books.add(book);
}

FileWriterwriter=newFileWriter("books.txt");

for(Bookbook:books){
writer.write(book.getBookInfo()+System.lineSeparator());
}

writer.close();
}
}

H. java編程基礎

這三段的區別就是if else語句的邏輯區別。

第一段相當於

if(x>0){
max=mid-1;
}
if(x<0){
min=mid+1;
}else//這個else是和上面的if(x<0)匹配的,相當於if(x>=0),但應該是等於0才算找到,所以就錯了
{
returnmid;
}

第二段三個if語句獨立,且條件都不相交,所以不會干擾。

第三段

if(x>0)
max = mid - 1;
else if(x<0)
min = mid + 1;
else // 這里三個條件語句是一個整體,這個else是前兩個條件都不滿足的情況,不大於又不小於,就是等於了
return mid;

熱點內容
安卓雙擊鎖屏如何設置 發布:2025-01-20 23:56:52 瀏覽:201
拉鏈壓縮袋 發布:2025-01-20 23:55:16 瀏覽:685
少兒編程多少錢一節課 發布:2025-01-20 23:53:46 瀏覽:939
政府網站登錄密碼是什麼意思 發布:2025-01-20 23:53:09 瀏覽:188
pythonwin26 發布:2025-01-20 23:37:00 瀏覽:502
國外哪些同款同配置車比國內貴 發布:2025-01-20 23:32:55 瀏覽:994
匯編為什麼少編譯了一條語句 發布:2025-01-20 23:30:57 瀏覽:146
伺服器內存不夠電腦會怎麼樣 發布:2025-01-20 23:10:35 瀏覽:209
discuz圖片上傳失敗 發布:2025-01-20 22:59:55 瀏覽:95
c語言函數分為 發布:2025-01-20 22:59:42 瀏覽:535