欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Java水果箱代碼 java創(chuàng)建水果類

java程序怎么寫?

package?demo;

從網(wǎng)站建設(shè)到定制行業(yè)解決方案,為提供成都網(wǎng)站制作、成都做網(wǎng)站服務(wù)體系,各種行業(yè)企業(yè)客戶提供網(wǎng)站建設(shè)解決方案,助力業(yè)務(wù)快速發(fā)展。創(chuàng)新互聯(lián)公司將不斷加快創(chuàng)新步伐,提供優(yōu)質(zhì)的建站服務(wù)。

public?class?Fruit?{

//水果類屬性

String?kind,color;

double?weight;

//有參構(gòu)造必須賦值

public?Fruit(String?kind,String?color,double?weight){

this.setKind(kind);

this.setColor(color);

this.setWeight(weight);

}

//屬性封裝

public?String?getKind()?{

return?kind;

}

public?void?setKind(String?kind)?{

this.kind?=?kind;

}

public?String?getColor()?{

return?color;

}

public?void?setColor(String?color)?{

this.color?=?color;

}

public?double?getWeight()?{

return?weight;

}

public?void?setWeight(double?weight)?{

this.weight?=?weight;

}

}

package?demo;

import?java.util.ArrayList;

import?java.util.Scanner;

public?class?Box?{

//箱子,有一個(gè)水果類集合

ArrayListFruit?f=new?ArrayListFruit();

public?ArrayListFruit?getF()?{

return?f;

}

public?void?setF(ArrayListFruit?f)?{

this.f?=?f;

}

//將水果添加到箱子(集合中)

public?void?addFruit(Fruit?temp)?{

this.f.add(temp);

}

void?manage(){

Box?box=new?Box();

int?choose;

try?{

do{

System.out.println("請(qǐng)選擇添加水果:1、蘋果,2、橘子,3、梨,4、查看,5、退出");

Scanner?input=new?Scanner(System.in);

choose=input.nextInt();

switch?(choose)?{

case?1:

System.out.println("請(qǐng)輸入類型");

String?type1=input.next();

System.out.println("請(qǐng)輸入顏色");

String?color1=input.next();

System.out.println("請(qǐng)輸入重量");

double?weight1=input.nextDouble();

Fruit?temp1=new?Fruit(type1,color1,weight1);

box.addFruit(temp1);

break;

case?2:

System.out.println("請(qǐng)輸入類型");

String?type2=input.next();

System.out.println("請(qǐng)輸入顏色");

String?color2=input.next();

System.out.println("請(qǐng)輸入重量");

double?weight2=input.nextDouble();

Fruit?temp2=new?Fruit(type2,color2,weight2);

box.addFruit(temp2);

break;

case?3:

System.out.println("請(qǐng)輸入類型");

String?type3=input.next();

System.out.println("請(qǐng)輸入顏色");

String?color3=input.next();

System.out.println("請(qǐng)輸入重量");

double?weight3=input.nextDouble();

Fruit?temp3=new?Fruit(type3,color3,weight3);

box.addFruit(temp3);

break;

case?4:

double?allWeight=0;

for?(int?i=0;ibox.getF().size();i++)?{

allWeight+=box.getF().get(i).weight;

}

System.out.println("總重量:"+allWeight);

System.out.println("顏色:");

for(Fruit?fruit:box.getF()){

System.out.println(fruit.getColor());

}

break;

case?5:

break;

default:

System.out.println("選擇錯(cuò)誤");

break;

}

}while(choose!=5);

}?catch?(Exception?e)?{

System.out.println("輸入錯(cuò)誤");

}

}

public?static?void?main(String[]?args)?{

Box?manage=new?Box();

manage.manage();

}

}

java程序題!!!求大神指教!謝了! 有一個(gè)水果箱(Box),箱子里裝

時(shí)間不夠 我得回宿舍 所有的驗(yàn)證部分都沒做 代碼可以運(yùn)行 但前提是你得正確輸入 否則不認(rèn)

package com.Test1;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.Scanner;

//java程序題!!!求大神指教!謝了!

//有一個(gè)水果箱(Box),箱子里裝有水果(Fruit),每一種水果都有不同的重量和顏色,

//水果有:蘋果,梨,橘子。每個(gè)蘋果(Apple)都有不同的重量和顏色,

//每個(gè)橘子(Orange)有不同的重量和顏色,每個(gè)梨(Pear)都有不同的重量和顏色,

//可以像水果箱(Box)里添加水果(addFruit),也可以取出水果(getFruit),

//還可以顯示水果的重量和顏色,寫出實(shí)現(xiàn)這些方法的代碼,要求實(shí)現(xiàn)上述功能!

public class HelpTest {

public static void main(String[] args) throws NumberFormatException, IOException {

// TODO Auto-generated method stub

InputStreamReader isr =new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(isr);

Box b=new Box();

while(true)

{

System.out.println(" 請(qǐng)選擇功能 ");

System.out.println("1 添加水果");

System.out.println("2 刪除水果");

System.out.println("3 顯示水果信息");

System.out.println("4 退出系統(tǒng)");

int a =Integer.parseInt(br.readLine());

System.out.println(a);

switch (a) {

case 1:

System.out.println("請(qǐng)輸入水果名稱");

String name=br.readLine();

System.out.println("請(qǐng)輸入水果顏色");

String color=br.readLine();

System.out.println("請(qǐng)輸入水果重量");

float weight=Float.parseFloat(br.readLine());

Furit f=new Furit(name, color, weight);

b.AddFurit(f);

break;

case 2:

System.out.println("請(qǐng)輸入水果名稱");

String name1=br.readLine();

b.DelFurit(name1);

break;

case 3:

System.out.println("請(qǐng)輸入水果名稱");

String name2=br.readLine();

b.ShowInfo(name2);

break;

case 4:

System.exit(0);

break;

default:

break;

}

}

}

}

class Furit{

private String name;

private String color;

private float weight;

public Furit(String name,String color,float weight){

this.name=name;

this.color=color;

this.weight=weight;

}

public Furit(String name)

{

this.name=name;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

public float getWeight() {

return weight;

}

public void setWeight(float weight) {

this.weight = weight;

}

}

class Box{

ArrayListFurit al=new ArrayListFurit();

//添加水果的方法

public void AddFurit(Furit f){

al.add(f);

}

//刪除水果的方法

public void DelFurit(String name)

{

for(int i=0;ial.size();i++)

{

if(al.get(i).getName().equals(name))

{

al.remove(i);

}

}

}

//顯示水果的方法

public void ShowInfo(String name ){

for(int i=0;ial.size();i++)

{

Furit f=(Furit)al.get(i);

if(name.equals(f.getName()))

{

System.out.println(al.get(i).getName()+"的顏色是"+al.get(i).getColor()+" 重量是"+al.get(i).getWeight());

}

}

}

}

我給你寫了這么多你采納他的答案? 呵呵

java程序,求解

可以定義一個(gè)FruitBox父類,然后定義水果蘋果Apple,橘子Orange,梨Pear的子類,顏色color和重量weight封裝成屬性,添加子類的get,set方法,定義一個(gè)測(cè)試類測(cè)試一下.

本文標(biāo)題:Java水果箱代碼 java創(chuàng)建水果類
文章地址:http://chinadenli.net/article48/ddocchp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)用戶體驗(yàn)網(wǎng)站維護(hù)Google網(wǎng)頁(yè)設(shè)計(jì)公司網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)