gpt4 book ai didi

java - 我的代码有什么问题;构造函数和循环?

转载 作者:行者123 更新时间:2023-12-02 05:09:13 26 4
gpt4 key购买 nike

这是我的驱动程序类

import java.io.File;
import java.io.IOException;
import java.util.Scanner;


public class PizzaMatch {

@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {

String answer = "";
String yes = "Y";
String no = "N";
String topping = "";
int size = 0;
double cost = 0;
int iteration = 0;
int inFileSize = 0;
double inFileCost = 0;
String inFileTopping = "";

Scanner reader = new Scanner(System.in);
Scanner inFileReader = new Scanner(new File("C:/Users/Frosty Snow/Downloads/pizza.txt"));

System.out.println("Would you like to enter a Pizza? (Y or N)");
answer = reader.next();

if(answer.equalsIgnoreCase(no)) {
System.exit(1);
}

System.out.println("Input a Pizza topping, size, and cost:");
topping = reader.next();
size = reader.nextInt();
cost = reader.nextDouble();

Pizza Order = new Pizza (size, topping, cost);

while(inFileReader.hasNextLine())
{
iteration ++;
inFileSize = inFileReader.nextInt();
inFileTopping = inFileReader.next();
inFileCost = inFileReader.nextDouble();

Pizza inFileOrder = new Pizza(inFileSize, inFileTopping, inFileCost);

if(Order.equals(inFileOrder))
{
System.out.println("That pizza is # " + iteration + " in the file.");
break;
}
}

}

}

这是我的资源类

public class Pizza {

private int size;
private String topping;
private double cost;


public Pizza()
{
size = 10;
topping = "cheese";
cost = 9.00;
}

public Pizza(int s, String t, double c)
{
size = s;
topping = t;
cost = c; }

public int getSize() {
return size;
}

public void setSize(int s) {
s = size;
}

public String getTopping(){
return topping;
}

public void setTopping(String t){
topping = t;
}

public void setCost(double c) {
cost = c;
}

public double getCost(double c){
return cost;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Pizza))
throw new IllegalArgumentException();

Pizza temp = (Pizza) obj;

if (this.size == temp.size && this.topping.equals(temp.topping) && this.cost == temp.cost)
return true;
else
return false;
}

public String toString()
{
return String.format("%d inch %s pizza will cost $%,.2f\n", size, topping, cost);

}
}

这些是我的错误

Would you like to enter a Pizza? (Y or N)
sloppyJoe 15 15.30
Input a Pizza topping, size, and cost:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at PizzaMatch.main(PizzaMatch.java:34)

Would you like to enter a Pizza? (Y or N)
Y
Input a Pizza topping, size, and cost:
sloppyJoe 15 15.30
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at PizzaMatch.main(PizzaMatch.java:42)

我认为问题与 while 语句有关,阅读下一行。我可以用其他东西来代替它吗?如果还有其他事情可能让我陷入困境,请告诉我。

披萨.txt

cheese 12 12.50
sausage 15 21.89
peppers 10 15.05
pineapple 40 33.00
pepperoni 11 10.22
olive 9 8.99
xcheese 13 9.66
supreme 13 15.22
mushroom 17 16.34
bbqchicken 14 20.13
pepperoni 10 11.70
sausage 12 11.89
peppers 12 15.05
pineapple 14 13.50
squirrel 12 12.24
pickle 12 7.99
hawaiian 10 13.00
pepperoni 11 10.22
meat 13 9.66
sloppyJoe 15 15.30
dessert 14 17.60
bigBubba 16 25.00
steakLovers 23 30.77
bison 10 11.70
secretMeat 12 11.99
peppers 12 14.00
pineNeedle 13 13.50
sweetTart 12 12.24
tofu 7 8.99

最佳答案

必须更改顺序

        inFileSize = inFileReader.nextInt();
inFileTopping = inFileReader.next();
inFileCost = inFileReader.nextDouble();

        inFileTopping = inFileReader.next();
inFileSize = inFileReader.nextInt();
inFileCost = inFileReader.nextDouble();

这是因为您的文件首先包含配料的名称。所以你必须先阅读inFileTopping

更改后的输出将是

Would you like to enter a Pizza? (Y or N)
Y
Input a Pizza topping, size, and cost:
cheese 12 12.50
That pizza is # 1 in the file.

了解更多详情

文件的第一行

cheese 12 12.50

这里首先是浇头。您试图将其读取为尺寸,这给您带来了错误

关于java - 我的代码有什么问题;构造函数和循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27472750/

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