gpt4 book ai didi

Java 跳过打印语句扫描器,具体

转载 作者:行者123 更新时间:2023-12-02 10:21:29 27 4
gpt4 key购买 nike

所以我的问题是我想要求输入使用扫描仪的信息,但它根本不打印出来。当它显示跳过的扫描仪的值时,Scanner CheeseType = new Scanner(System.in);,我得到 null。

package classesProject;

import java.util.Scanner;

class Pizza {

String size;
String cheese;
int numToppings;
double price = 0;

}
public class pizzaTime {

public static void main(String[] args) {

Pizza order1 = new Pizza();

double priceOfSize = 0;
double priceOfCheese = 0;
double priceOfToppings = 0;

System.out.println("Pizza size small, medium or large: ");
Scanner sizeAsker = new Scanner(System.in);
order1.size = sizeAsker.nextLine();

if(order1.size == "small") {
priceOfSize = 3.0;

} else if(order1.size == "medium") {
priceOfSize = 5.0;

} else if(order1.size == "large") {
priceOfSize = 7.0;

System.out.println("Cheese type: normal, xtra, or thic: ");
Scanner cheeseType = new Scanner(System.in);
order1.cheese = cheeseType.nextLine();

}if(order1.cheese == "normal") {
priceOfCheese = 0.0;

} else if(order1.cheese == "xtra") {
priceOfCheese = 0.5;

} else if(order1.cheese == "thic") {
priceOfCheese = 1.0;

}

System.out.println("Number of toppings: ");
Scanner toppingAsker = new Scanner(System.in);
order1.numToppings = toppingAsker.nextInt();

priceOfToppings = order1.numToppings * 0.25;

double orderTotalPrice = priceOfSize + priceOfCheese;

System.out.println("Pizza size: " + order1.size + "\n"
+ "Cheese type: " + order1.cheese + "\n"
+ "Number of toppings: " + order1.numToppings + "\n"
+ "Order total: " + orderTotalPrice
);

}

}

被跳过的是:

System.out.println("Cheese type: normal, xtra, or thic: ");
Scanner cheeseType = new Scanner(System.in);
order1.cheese = cheeseType.nextLine();

运行后,控制台显示:

Pizza size small, medium or large: 
small
Number of toppings:
2
Pizza size: small
Cheese type: null
Number of toppings: 2
Order total: 0.0

正如您所看到的,它直接从披萨尺寸扫描仪跳到配料数量扫描仪,而不是按顺序进行。我不知道为什么,或者我应该做什么来解决这个问题。

最佳答案

由于您在下一个问题后关闭括号,因此仅针对大披萨询问此问题

else if(order1.size == "large") {
priceOfSize = 7.0;

System.out.println("Cheese type: normal, xtra, or thic: ");
Scanner cheeseType = new Scanner(System.in);
order1.cheese = cheeseType.nextLine();

}

如果您在priceOfSize = 7 之后添加右括号,则可以继续。不过,您仍然需要在其他地方修复丢失的括号。

关于Java 跳过打印语句扫描器,具体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54322574/

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