我怎样才能提高效率呢? - Java中的IF语句 [英] How can I make this more efficient? - IF statements in Java
问题描述
我只是想知道,我怎样才能使这段代码更有效率?如果我坚持下去,那将是非常多余的......如果你能保持简单那就太好了......
I'm just wondering, how can I make this code more efficient? It would just be incredibly redundant if I kept going... Please, if you could keep it simple that would be nice...
我开始使用这段代码,我正在寻找一种方法来接受输入(比方说1)并将其存储到变量中......稍后会发生这样的事情:
I started out with this code, and I'm looking for a way to take the input (lets say 1) and store it into a variable later on... Something like this:
while(pizzaCounter < 5)
{
Scanner pizzaPick = new Scanner(System.in);
int Pizzas = pizzaPick.nextInt();
if ((Pizzas >= 0) && (Pizzas <= 1)) {
String saveName = pizza[Pizzas];
pizzaCounter++;
if(pizzaCounter < 5){
System.out.println("and");
}
}
}
<我有点想将这两个代码合并在一起,我只是不知道如何。
(这两个位于此文本之上,位于下方)
I kind of want to merge these two codes together, I just have no idea how. (These two being the bit above this text, and the bit below)
while(pizzaCounter < 5)
{
Scanner pizzaPick = new Scanner(System.in);
int Pizzas = pizzaPick.nextInt();
if (Pizzas == 1)
{
System.out.println("1- standard size pizza? - $" + standardPrice + "\nOr \n2- Large pizza? - $" + largePrice);
Scanner pizzaSize = new Scanner(System.in);
int Size = pizzaSize.nextInt();
if(Size == 1)
{
moneyCounter = moneyCounter + standardPrice;
}
else if(Size == 2)
{
moneyCounter = moneyCounter + largePrice;
}
pizzaCounter++;
if(pizzaCounter < 5){
System.out.println("and");
}
这是冗余的来源......
This is where the redundancy comes in...
if (Pizzas == 2)
{
System.out.println("1- standard size pizza? - $" + standardPrice + "\nOr \n2- Large pizza? - $" + largePrice);
Scanner pizzaSize = new Scanner(System.in);
int Size = pizzaSize.nextInt();
if(Size == 1)
{
moneyCounter = moneyCounter + standardPrice;
}
else if(Size == 2)
{
moneyCounter = moneyCounter + largePrice;
}
pizzaCounter++;
if(pizzaCounter < 5){
System.out.println("and");
}
}
}
System.out.println("The Subtotal is: $" + moneyCounter + "\n");
}
推荐答案
我也会考虑制作if块中的代码是一个新方法,这样它就可以根据方法的参数灵活地处理不同的东西。
您将得到以下内容:
I would also consider making the code inside the if block a new method, that way it gives you flexibility to something different based on arguments to to the method. You will have something like this:
while(pizzaCounter < 5)
{
Scanner pizzaPick = new Scanner(System.in);
int Pizzas = pizzaPick.nextInt();
if ((Pizzas >= 0) && (Pizzas <= 1)) {
String saveName = pizza[Pizzas];
pizzaCounter++;
if(pizzaCounter < 5){
System.out.println("and");
}
}
if (Pizzas == 1 )
{
handlePizza();
pizzaCounter++;
if(pizzaCounter < 5){
System.out.println("and");
}
}
else if( Pizzas == 2)
{
handlePizza();
pizzaCounter++;
if(pizzaCounter < 5){
System.out.println("and");
}
}
}
public void handlePizza(){
System.out.println("1- standard size pizza? - $" + standardPrice + "\nOr \n2- Large pizza? - $" + largePrice);
Scanner pizzaSize = new Scanner(System.in);
int Size = pizzaSize.nextInt();
if(Size == 1)
{
moneyCounter = moneyCounter + standardPrice;
}
else if(Size == 2)
{
moneyCounter = moneyCounter + largePrice;
}
}
然后你可以玩弄参数并返回值方法。
You could then play around with arguments and return values for the method.
这篇关于我怎样才能提高效率呢? - Java中的IF语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!