gpt4 book ai didi

java - for循环打印无效值?

转载 作者:行者123 更新时间:2023-12-01 22:05:42 25 4
gpt4 key购买 nike

我是 Java 编程新手。我开发了一个 Pizza 类,它接受参数并输出描述和成本。我开发了一个 PizzaOrderArray 类,将披萨订单存储在数组中。我有一个类也包含 main 方法。

我正在尝试打印订单 4 的成本,但内置错误消息声称 Pizza_size 无效并打印出该值为 0.0.

我不明白订单4的pizza_size的值如何打印在其上方,但pizza_size却变得无效。

我错过了什么?

Pizza.java

import java.text.NumberFormat; 
import java.util.Locale;

public class Pizza {

public Pizza(String size, int numCheeseTop, int numPepTop, int numHamTop){
if(!setPizzaSize(size)){
System.out.println(size + " is invalid size." + "Use small, medium or large.");
}
setNumCheese(numCheeseTop);
setNumPep(numPepTop);
setNumHam(numHamTop);
}

public Pizza(String size, int numPepTop, int numHamTop){
if(!setPizzaSize(size)){
System.out.println(size + " is invalid size." + "Use small, medium or large.");
}
pizza_cheese = 0;
setNumPep(numPepTop);
setNumHam(numHamTop);
}

public Pizza(String size, int numHamTop){
if(!setPizzaSize(size)){
System.out.println(size + " is invalid size." + "Use small, medium or large.");
}
pizza_pep = 0;
setNumHam(numHamTop);
pizza_cheese = 0;
}

public Pizza(String size){
if(!setPizzaSize(size)){
System.out.println(size + " is invalid size." + "Use small, medium or large.");
}
pizza_cheese = 0;
pizza_pep = 0;
pizza_ham = 0;
}

public Pizza(){
pizza_size = "small";
pizza_cheese = 0;
pizza_pep = 0;
pizza_ham = 0;
}

public Pizza(Pizza copyPizza)
{
pizza_size = copyPizza.getPizzaSize();
pizza_cheese = copyPizza.getNumCheese();
pizza_pep = copyPizza.getNumPep();
pizza_ham = copyPizza.getNumHam();
}


//Setters

public boolean setPizzaSize(String size){
if(size.equalsIgnoreCase("small") || (size.equalsIgnoreCase("medium") || (size.equalsIgnoreCase("large")))){
pizza_size = size.toLowerCase();
return true;
}
return false;
}

public void setNumCheese(int numCheeseTop){
pizza_cheese = numCheeseTop;
}

public void setNumPep(int numPepTop){
pizza_pep = numPepTop;
}

public void setNumHam(int numHamTop){
pizza_ham = numHamTop;
}
//End of setters

//Getters

public String getPizzaSize(){
return pizza_size;
}

public int getNumCheese(){
return pizza_cheese;
}

public int getNumPep(){
return pizza_pep;
}

public int getNumHam(){
return pizza_ham;
}

//End of getters

public double calcCost(){
if(pizza_size.toLowerCase() == "small"){
return 10 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
}
if(pizza_size.toLowerCase() == "medium"){
return 12 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
}
if(pizza_size.toLowerCase() == "large"){
return 14 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
}
if(pizza_size.toLowerCase() != "small" && pizza_size.toLowerCase() != "medium" && pizza_size.toLowerCase() != "large"){
System.out.println("Inside of calcCost if");
System.out.println("Invalid pizza size");
return 0;
}
return 0;
}


public String toString(){
return pizza_size + " pizza with " + pizza_cheese + " cheese toppings " + pizza_pep + " pepperoni toppings and "
+ pizza_ham + " ham toppings "; }

public String getDescription(){
return pizza_size + " pizza with " + pizza_cheese + " cheese toppings " + pizza_pep + " pepperoni toppings and "
+ pizza_ham + " ham toppings ";
}

public String pizza_size;
public int pizza_cheese, pizza_pep, pizza_ham;

} //End of Pizza class

PizzaOrderArray.java

import static java.lang.System.out;

public class PizzaOrderArray{

public String pizza_size;
public int pizza_cheese, pizza_pep, pizza_ham;

private Pizza[] pizza;
private int index = 0;


public PizzaOrderArray(){
index = 1;
pizza = new Pizza[index];
}

public PizzaOrderArray(int i){
index = 1;
pizza = new Pizza[index];
}

public PizzaOrderArray(PizzaOrderArray poa){
pizza = new Pizza[poa.index];
index = poa.index;
for (int i = 0; i < poa.index; i++){
pizza[i] = new Pizza(poa.pizza[i]);
}
}

public void setPizza(int index1, Pizza newpizza){
pizza[index1] = new Pizza(newpizza);
}

public String getPizzaSize(){
return pizza_size;
}

public int getNumCheese(){
return pizza_cheese;
}

public int getNumPep(){
return pizza_pep;
}

public int getNumHam(){
return pizza_ham;
}

public String toString() {
String s = "";
for(int i = 0; i < index; i++) {
s= (s + pizza[i].toString());
}
return s;
}

public double calcTotal(){
double r = 0.0;
for(int i = 0; i < index; i++){
System.out.println("PizzaOrderArray-calcTotal()");
r = r + pizza[i].calcCost();
}
return r;
}

public boolean equals(PizzaOrderArray orderarray) {
boolean r = false;
if(orderarray.pizza.length != pizza.length) {
return r;
}

for(int i = 0; i < orderarray.pizza.length; i++) {
if(pizza[i].equals(orderarray.pizza[i])) {
r = true;
}
else {
return false;
}
}
return r;
}


} //End of PizzaOrderArray class



import java.text.DecimalFormat;
import java.util.Scanner;
import java.util.Arrays;

public class V4_Project_15_page_418 {

public static void main (String args[]){

//Order1
PizzaOrderArray order1 = new PizzaOrderArray();
Pizza pizzaone = new Pizza("Medium", 1, 1, 2);
Pizza pizzatwo = new Pizza("Small", 1, 2, 1);
order1.setPizza(0, pizzaone);
System.out.println("Order 1: ");
System.out.println(order1);
System.out.println();

//Order2
Pizza pizzathree = new Pizza(pizzatwo);
PizzaOrderArray order2 = new PizzaOrderArray(2);
order2.setPizza(0, pizzaone);
order2.setPizza(0, pizzatwo);
System.out.println("Order 2: ");
System.out.println(order2);
System.out.println();

//Order3
PizzaOrderArray order3 = new PizzaOrderArray(1);
order3.setPizza(0, pizzaone);
order3.setPizza(0, pizzatwo);
System.out.println("Order 3: ");
System.out.println(order3);
System.out.println();

//Order4
PizzaOrderArray order4 = new PizzaOrderArray(order1);
System.out.println("Order4: ");
System.out.println(order4);
System.out.println();

//TEST THE PROGRAM
System.out.println("TEST: The total for order 4 is: " + order4.calcTotal());
System.out.println();

System.out.println("TEST: Is order1 equal to order2?");
System.out.println(order1.equals(order2));
System.out.println();

System.out.println("TEST: Is order2 equal to order4?");
System.out.println(order2.equals(order4));
System.out.println();



}//End of main class

}//End of V4_Project_15_page_418 class

最佳答案

您正在使用 == 比较字符串,它比较实例身份,而不是相等。您应该使用 String.equals 来代替。

或者,您可以使用开关:

public double calcCost(){
switch (pizza_size.toLowerCase()) {
case "small":
return 10 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
case "medium":
return 12 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
case "large":
return 14 + ((pizza_cheese + pizza_pep + pizza_ham)*2);
default:
System.out.println("Inside of calcCost if");
System.out.println("Invalid pizza size");
return 0;
}
}

您还应该考虑使用枚举来表示披萨大小:

enum Size { SMALL, MEDIUM, LARGE }

字符串不是一个好的选择,因为您不仅可以通过不同的大小写来表示大小(在整个代码中进行不区分大小写的比较和小写),而且还可以传递其他任何内容。如果您将输入限制为这几种大小,您的代码就会变得简单得多。并且,您可以使用 == 比较枚举值。

关于java - for循环打印无效值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32853237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com