介于進(jìn)了好多個(gè)問題,都看到樓下,我就過來湊個(gè)回答數(shù)。

在北京等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站設(shè)計(jì)制作、網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作按需網(wǎng)站建設(shè),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計(jì),成都全網(wǎng)營銷,成都外貿(mào)網(wǎng)站建設(shè)公司,北京網(wǎng)站建設(shè)費(fèi)用合理。
public?class?Drink{
private?String?name;
public?Drink()?{}
public?Drink(String?name)?{
this.name?=?name;
}
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?void?print()?{
System.out.println("?I?am?"?+?name);;
}
public?static?void?main(String[]?args)?{
Drink?drink?=?new?Drink("可口可樂");
System.out.println(Drink.print());
}
}
正確的結(jié)果:對兌了17次,共換了93個(gè)瓶子,第18次對兌時(shí)余下1個(gè)空瓶,2個(gè)瓶蓋。
遞歸模型樓上“l(fā)zs丶灬 ”同志是正確的,但存在邏輯錯(cuò)誤。我?guī)退拚缦拢?/p>
public?static?int?test(int?sum,?int?pingzi,?int?gaizi){
if?(pingzi??2??gaizi??3)
{
return?sum;
}
int?newPingzi?=?pingzi?/?2;
int?newGaizi?=?gaizi?/?3;
sum?+=?newPingzi;
sum?+=?newGaizi;
pingzi?=?newPingzi?+?newGaizi?+?pingzi?%?2;
gaizi?=?newGaizi?+?newPingzi?+?gaizi?%?3;
return?test(sum,?pingzi,?gaizi);
}
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class Question {
class Drink{
protected String type;//蘇打,果汁?
protected String name;//名字?
protected double price;//價(jià)格
public String getType() {
return type;
}
public double getPrice() {
return price;
}
public String getName() {
return name;
}
}
class Cole extends Drink{
public Cole(){
name = "可口可樂";
type = "蘇打";
price = 6.0d;
}
}
class Putaoduo extends Drink{
public Putaoduo(){
name = "葡萄多";
type = "果汁";
price = 3.0d;
}
}
class Person{
private double money;
private ListDrink drinks = new ArrayListDrink();
public void buyDrink(Drink drink) throws QianBuGouException, Exception{
if(drink != null){
double price = drink.getPrice();
if(money = price){
drinks.add(drink);
if(price == 0){
throw new Exception("竟然有免費(fèi)的飲料,好爽呀!");
}else if(price 0){
throw new Exception("竟然有倒貼的飲料,好爽呀!");
}
money -= price;
}else{
throw new QianBuGouException();
}
}
}
public void drink(Drink drink) throws DrinkNotFoundException{
if(drinks.contains(drink)){
drinks.remove(drink);
}else{
throw new DrinkNotFoundException("這瓶飲料不是我的!");
}
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public ListDrink getDrinks() {
return drinks;
}
}
class QianBuGouException extends Exception{
public QianBuGouException(){
super("我的錢不夠買飲料的啦,再買就破產(chǎn)了!");
}
}
class DrinkNotFoundException extends Exception{
public DrinkNotFoundException(String message){
super(message);
}
}
public Question(){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Person you = new Person();
you.setMoney(10d);
Cole cole = new Cole();
try{
you.drink(cole);
JOptionPane.showMessageDialog(null, "我喝了一瓶飲料", "提示信息", JOptionPane.ERROR_MESSAGE);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);
}
try{
you.buyDrink(new Cole());
JOptionPane.showMessageDialog(null, "我買了一瓶飲料", "提示信息", JOptionPane.ERROR_MESSAGE);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);
}
try{
you.drink(new Cole());
JOptionPane.showMessageDialog(null, "我喝了一瓶飲料", "提示信息", JOptionPane.ERROR_MESSAGE);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);
}
try{
you.buyDrink(new Putaoduo());
JOptionPane.showMessageDialog(null, "我買了一瓶飲料", "提示信息", JOptionPane.ERROR_MESSAGE);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);
}
try{
you.buyDrink(new Putaoduo());
JOptionPane.showMessageDialog(null, "我買了一瓶飲料", "提示信息", JOptionPane.ERROR_MESSAGE);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);
}
try{
you.buyDrink(new Putaoduo());
JOptionPane.showMessageDialog(null, "我買了一瓶飲料", "提示信息", JOptionPane.ERROR_MESSAGE);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);
}
try{
you.buyDrink(new Putaoduo());
JOptionPane.showMessageDialog(null, "我買了一瓶飲料", "提示信息", JOptionPane.ERROR_MESSAGE);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);
}
ListDrink drinks = you.getDrinks();
int all = drinks.size();
for(int i = 0; i all; i ++){
Drink drink = drinks.get(0);
try{
you.drink(drink);
JOptionPane.showMessageDialog(null, "我喝了一瓶飲料", "提示信息", JOptionPane.ERROR_MESSAGE);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);
}
}
JOptionPane.showMessageDialog(null, "我還有" + drinks.size() + "瓶飲料。", "提示信息", JOptionPane.ERROR_MESSAGE);
}
});
}
public static void main(String args[]){
new Question();
}
}
下附代碼,可以在鍵盤輸入加入判斷,比如是否輸入的是數(shù)字而不是字母等等,是否輸入的是正數(shù)而不是其他符號(hào)... 另外可以將下面代碼優(yōu)化,封裝,在main方法只做調(diào)用即可
package test;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
boolean flag = true;
while(flag){
System.out.println("歡迎使用自動(dòng)飲料售賣機(jī)");
System.out.println("1.雪碧 3.00");
System.out.println("2.可樂 3.00");
System.out.println("3.特侖蘇 5.00");
System.out.println("4.農(nóng)夫山泉 2.00");
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.print("請輸入你的選擇是:");
int choise = input.nextInt();
System.out.print("請投幣:");
int money = input.nextInt();
money(money, choise);
System.out.print("是否繼續(xù)'1.繼續(xù) 0.退出' :");
int goon = input.nextInt();
if(goon==1){
flag = true;
}else{
flag = false;
}
}
}
public static void money(int money,int choise){
switch(choise){
case 1:
if(money-30){
System.out.println("找零"+(money-3)+"元");
}else if(money-30){
System.out.println("請?jiān)偻度?+(3-money)+"元");
}else if(money-3==0){
System.out.println("生活愉快");
}else{
System.out.println("輸入有誤!");
}
break ;
case 2:
if(money-30){
System.out.println("找零"+(money-3)+"元");
}else if(money-30){
System.out.println("請?jiān)偻度?+(3-money)+"元");
}else if(money-3==0){
System.out.println("生活愉快");
}else{
System.out.println("輸入有誤!");
}
break ;
case 3:
if(money-50){
System.out.println("找零"+(money-5)+"元");
}else if(money-50){
System.out.println("請?jiān)偻度?+(5-money)+"元");
}else if(money-5==0){
System.out.println("生活愉快");
}else{
System.out.println("輸入有誤!");
}
break ;
case 4:
if(money-20){
System.out.println("找零"+(money-2)+"元");
}else if(money-20){
System.out.println("請?jiān)偻度?+(2-money)+"元");
}else if(money-2==0){
System.out.println("生活愉快");
}else{
System.out.println("輸入有誤!");
}
break ;
}
}
}
網(wǎng)站欄目:品嘗飲料java代碼,基于java的飲品售賣系統(tǒng)
URL分享:http://chinadenli.net/article13/dseiigs.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、外貿(mào)建站、做網(wǎng)站、網(wǎng)頁設(shè)計(jì)公司、自適應(yīng)網(wǎng)站、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)