如何在一个包含三种选择的if else程序中初始化变量?

huangapple go评论72阅读模式
英文:

How do I initialize a variable in an if else program that combines three choices?

问题

我正在为课程中的一个问题编写一个派对策划程序

我无法初始化我的三个选择娱乐装饰和食物

Eclipse 告诉我应该将所有这些值都设为 0我必须坚持使用这些 if else 语句因为这是我们目前学到的内容

以下是我的代码

```java
import java.util.Scanner;

public class PartyPlanner {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int entertainment;
        int decorations;
        int food;

        int budget = entertainment + decorations + food;

        Scanner keyboard = new Scanner(System.in);
        System.out.println("对于您的选择,请在括号中键入相应的内容。我们将为您进行计算。");
        System.out.println("选择娱乐:[乐队] 价格为 $400 或者 [DJ] 价格为 $150");
        String choice1 = keyboard.nextLine();

        if (choice1.equals("DJ")) 
        {
            entertainment = 400;
        }
        else if (choice1.equals("乐队"))
        {
            entertainment = 150;
        }

        System.out.println("您想在哪里购买装饰?[学校] 价格为 $100 或者 [自己购买] 价格为 $250 ?");
        String choice2 = keyboard.nextLine();

        if (choice2.equals("学校"))
        {
            decorations = 100;
        }
        else if (choice2.equals("自己购买"))
        {
            decorations = 250;
        }

        System.out.println("您想购买 [比萨] 价格为 $200,还是 [三明治] 价格为 $250,还是 [开胃菜] 价格为 $150?");
        String choice3 = keyboard.nextLine();

        if (choice3.equals("比萨")) 
        {
            food = 200;
        }
        else if (choice3.equals("三明治"))
        {
            food = 250;
        }
        else if (choice3.equals("开胃菜")) 
        {
            food = 150;
        }

        System.out.println("您已选择:" + choice1 +
            " 和 " + choice2 + " 和 " + choice3);
        System.out.println("这个派对的总费用为:" + budget);

    }
}

问题在于:

局部变量 entertainment、decorations 和 food 可能未被初始化。


<details>
<summary>英文:</summary>
I&#39;m writing a party planner program for a question for class.
I can&#39;t initialize my three choices of entertainment, decorations, and food. 
Eclipse tells me that I should set all these values to 0. I have to stick to these if else statements because that&#39;s what we have learned so far. 
Here is my code: 

import java.util.Scanner;

public class PartyPlanner {

public static void main(String[] args) {
// TODO Auto-generated method stub
int entertainment;
int decorations;
int food;
int budget = entertainment + decorations + food;
Scanner keyboard = new Scanner(System.in);
System.out.println(&quot;For your choices, please type&quot;
+ &quot; in what is contained in the brackets.&quot;
+ &quot; We will do the calculations for you.&quot;);
System.out.println(&quot;Choose entertainment:&quot;
+ &quot; [band] for $400 or &quot; + &quot;[DJ] for $150&quot;);
String choice1 = keyboard.nextLine();
if (choice1 == &quot;DJ&quot;) 
{
entertainment = 400;
}
else if (choice1 == &quot;band&quot;)
{
entertainment = 150;
}
System.out.println(&quot;Where would you like to buy &quot;
+ &quot;decorations? [school] for $100 or [your own] for $250 ?&quot;);
String choice2 = keyboard.nextLine();
if (choice2 == &quot;school&quot;)
{
decorations = 100;
}
else if (choice2 == &quot;your own&quot;)
{
decorations = 250;
}
System.out.println(&quot;Would you like to purchase&quot;
+ &quot; [pizza] for $200 or [sub sandwiches]&quot;
+ &quot; for $250 or [appetizers] for $150?&quot;);
String choice3 = keyboard.nextLine();
if (choice3 == &quot;pizza&quot;) 
{
food = 200;
}
else if (choice3 == &quot;sub sandwiches&quot;)
{
food = 250;
}
else if (choice3 == &quot;appetizers&quot;) 
{
food = 150;
}
System.out.println(&quot;You have chosen: &quot; + choice1 +
&quot; and &quot; + choice2 + &quot; and &quot; + choice3);
System.out.println(&quot;The total cost of this party&quot;
+ &quot; comes out to:&quot; + budget);
}

}


The problem is 
**The local variable entertainment, decorations, and food may have not been initalized.**
</details>
# 答案1
**得分**: 3
首先,在代码末尾进行求和,因为一开始变量尚未初始化,另外如果没有进行任何选择,可以将值赋为零,像这样:
```java
public class PartyPlanner {
public static void main(String[] args) {
// TODO Auto-generated method stub
int entertainment;
int decorations;
int food;
Scanner keyboard = new Scanner(System.in);
System.out.println("For your choices, please type"
+ " in what is contained in the brackets."
+ " We will do the calculations for you.");
System.out.println("Choose entertainment:"
+ " [band] for $400 or " + "[DJ] for $150");
String choice1 = keyboard.nextLine();
if (choice1.equals("DJ")) 
{
entertainment = 400;
}
else if (choice1.equals("band"))
{
entertainment = 150;
}
else { entertainment = 0; }
System.out.println("Where would you like to buy "
+ "decorations? [school] for $100 or [your own] for $250 ?");
String choice2 = keyboard.nextLine();
if (choice2.equals("school"))
{
decorations = 100;
}
else if (choice2.equals("your own"))
{
decorations = 250;
}
else { decorations = 0; }
System.out.println("Would you like to purchase"
+ " [pizza] for $200 or [sub sandwiches]"
+ " for $250 or [appetizers] for $150?");
String choice3 = keyboard.nextLine();
if (choice3.equals("pizza")) 
{
food = 200;
}
else if (choice3.equals("sub sandwiches"))
{
food = 250;
}
else if (choice3.equals("appetizers")) 
{
food = 150;
}
else { food = 0 ; }
int budget = entertainment + decorations + food;
System.out.println("You have chosen: " + choice1 +
" and " + choice2 + " and " + choice3);
System.out.println("The total cost of this party"
+ " comes out to:" + budget);
}
}
英文:

First , you have to make the sum at the end of the code because at first the variables are not initialized yet , and secondly you can assign a value of zero if no choice have been made , like this :

public class PartyPlanner {
public static void main(String[] args) {
// TODO Auto-generated method stub
int entertainment;
int decorations;
int food;
Scanner keyboard = new Scanner(System.in);
System.out.println(&quot;For your choices, please type&quot;
+ &quot; in what is contained in the brackets.&quot;
+ &quot; We will do the calculations for you.&quot;);
System.out.println(&quot;Choose entertainment:&quot;
+ &quot; [band] for $400 or &quot; + &quot;[DJ] for $150&quot;);
String choice1 = keyboard.nextLine();
if (choice1 == &quot;DJ&quot;) 
{
entertainment = 400;
}
else if (choice1 == &quot;band&quot;)
{
entertainment = 150;
}
else { entertainment = 0; }
System.out.println(&quot;Where would you like to buy &quot;
+ &quot;decorations? [school] for $100 or [your own] for $250 ?&quot;);
String choice2 = keyboard.nextLine();
if (choice2 == &quot;school&quot;)
{
decorations = 100;
}
else if (choice2 == &quot;your own&quot;)
{
decorations = 250;
}
else { decorations = 0; }
System.out.println(&quot;Would you like to purchase&quot;
+ &quot; [pizza] for $200 or [sub sandwiches]&quot;
+ &quot; for $250 or [appetizers] for $150?&quot;);
String choice3 = keyboard.nextLine();
if (choice3 == &quot;pizza&quot;) 
{
food = 200;
}
else if (choice3 == &quot;sub sandwiches&quot;)
{
food = 250;
}
else if (choice3 == &quot;appetizers&quot;) 
{
food = 150;
}
else { food = 0 ; }
int budget = entertainment + decorations + food;
System.out.println(&quot;You have chosen: &quot; + choice1 +
&quot; and &quot; + choice2 + &quot; and &quot; + choice3);
System.out.println(&quot;The total cost of this party&quot;
+ &quot; comes out to:&quot; + budget);
}
}

huangapple
  • 本文由 发表于 2020年6月29日 07:08:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/62629158.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定