我的代码通过初始if语句
<?php
include ('../db.php');
$sql = 'INSERT INTO `soal` (`id`, `no`, `soal`, `options`, `a`, `b`, `c`, `d`, `e`, `benar`) VALUES (NULL, "'.$_GET['no'].'", "'.$_GET['soal'].'", "4", "'.$_GET['a'].'", "'.$_GET['b'].'", "'.$_GET['c'].'", "'.$_GET['d'].'", "e", "'.$_GET['benar'].'");';
$conn->query($sql);
然后在完成while循环后从库存中减去一个
if(choice.equalsIgnoreCase("Coke")){
itemid = 1;
sp.setItemnum(1);
}
else if(choice.equalsIgnoreCase("Water")){
itemid = 2;
sp.setItemnum(2);
}
else if(choice.equalsIgnoreCase("Gatorade")){
itemid = 3;
sp.setItemnum(3);
}
else if(choice.equalsIgnoreCase("Sprite")){
itemid = 0;
sp.setItemnum(0);
}
由于某种原因,这并不是减去我的数组的索引。
if(sp.isLeft(itemid)){
double f = sp.getItemPrice(itemid);
Customer ct = new Customer(f);
Scanner sv = new Scanner(System.in);
System.out.println("Your total is: $" + f +"\nPlease insert money (type amount of money to pay.)");
double t = sv.nextDouble();
ct.markPayment(t);
boolean paid = ct.checkIfPaid();
while(paid != true){
System.out.println("Please pay " + ct.getPayment() + " more");
double k = sv.nextDouble();
ct.markPayment(k);
paid = ct.checkIfPaid();
}
sp.reduceStock(itemid);
这是我从
中减去的数组public void reduceStock(int itemn){
stock[itemn] -= 1;
}
这是我的&#34; isLeft&#34;方法
private int[] stock = {2, 4, 0, 2};
以下是所有代码
控制器
public boolean isLeft(int itemnum){
boolean i;
if(stock[this.itemnum] > 0){
i = true;
}
else{
i = false;
System.out.println("We're restocking this item. Please come another time");
setStock(itemnum);
}
return i;
}
供应商
import java.util.Scanner;
public class Controller {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Supplier sp = new Supplier();
Vending vd = new Vending();
int itemid = 4;
System.out.println("I am a vending machine. What would you like? \nHere are your choices:");
sp.getItems();
String choice = sc.nextLine();
if(choice.equalsIgnoreCase("Coke")){
itemid = 1;
sp.setItemnum(1);
}
else if(choice.equalsIgnoreCase("Water")){
itemid = 2;
sp.setItemnum(2);
}
else if(choice.equalsIgnoreCase("Gatorade")){
itemid = 3;
sp.setItemnum(3);
}
else if(choice.equalsIgnoreCase("Sprite")){
itemid = 0;
sp.setItemnum(0);
}
else{
System.out.println("I didn't get that. Please try again");
main(args);
}
if(sp.isLeft(itemid)){
double f = sp.getItemPrice(itemid);
Customer ct = new Customer(f);
Scanner sv = new Scanner(System.in);
System.out.println("Your total is: $" + f +"\nPlease insert money (type amount of money to pay.)");
double t = sv.nextDouble();
ct.markPayment(t);
boolean paid = ct.checkIfPaid();
while(paid != true){
System.out.println("Please pay " + ct.getPayment() + " more");
double k = sv.nextDouble();
ct.markPayment(k);
paid = ct.checkIfPaid();
}
sp.reduceStock(itemid);
}
else{
System.out.println("Sorry! that item is out of stock. Please chose another item.\n-------------------------------------------\n");
vd.addStock(itemid);
main(args);
}
main(args);
}
}
}
客户
import java.util.ArrayList;
public class Supplier {
private int[] stock = {2, 4, 0, 2};
private double[] price = {2.00, 2.00, 1.00, 2.5};
private String[] items = {"Sprite", "Coke", "Water", "Gatorade"};
int itemnum;
public void setItemnum(int num){
itemnum = num;
}
public Supplier(){
}
public Supplier(String[] itemStock, String[] itemPrices){
}
public int getItemStock(int c){
return stock[c];
}
public double getItemPrice(int i){
return price[i];
}
public void getItems(){
for(int i = 0; i < items.length; i++){
System.out.println(items[i] + " ");
}
}
public int getStock(int d){
return stock[d];
}
public void setStock(int f){
stock[f] = 5;
}
public void reduceStock(int itemn){
stock[itemn] -= 1;
}
public int getNum(){
return itemnum;
}
public boolean isLeft(int itemnum){
boolean i;
if(stock[itemnum] > 0){
i = true;
}
else{
i = false;
System.out.println("We're restocking this item. Please come another time");
setStock(itemnum);
}
return i;
}
}
自动贩卖
public class Customer {
private double payment;
private boolean paid;
public Customer(){
}
public Customer(double f){
payment = f;
// Setting the payment equal to the amount needed to pay
}
public void markPayment(double i){
payment = payment - i;
}
boolean temp1;
public boolean checkIfPaid(){
if(payment <= 0.0){
System.out.println("Here you go. Enjoy");
temp1 = true;
}
else{
temp1 = false;
}
return temp1;
}
// make another check if paid to use!
public double getPayment(){
return payment;
}
}
答案 0 :(得分:1)
// int itemnum is a local variable:
public boolean isLeft(int itemnum){
boolean i;
// but you use this.itemnum instead:
if(stock[this.itemnum] > 0){
只需删除“这个。”
除了这三行需要static
:
public class Supplier {
static private int[] stock = {2, 4, 0, 2};
static private double[] price = {2.00, 2.00, 1.00, 2.5};
static private String[] items = {"Sprite", "Coke", "Water", "Gatorade"};