英文:
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'm writing a party planner program for a question for class.
I can'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'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("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 == "DJ")
{
entertainment = 400;
}
else if (choice1 == "band")
{
entertainment = 150;
}
System.out.println("Where would you like to buy "
+ "decorations? [school] for $100 or [your own] for $250 ?");
String choice2 = keyboard.nextLine();
if (choice2 == "school")
{
decorations = 100;
}
else if (choice2 == "your own")
{
decorations = 250;
}
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 == "pizza")
{
food = 200;
}
else if (choice3 == "sub sandwiches")
{
food = 250;
}
else if (choice3 == "appetizers")
{
food = 150;
}
System.out.println("You have chosen: " + choice1 +
" and " + choice2 + " and " + choice3);
System.out.println("The total cost of this party"
+ " comes out to:" + 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("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 == "DJ")
{
entertainment = 400;
}
else if (choice1 == "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 == "school")
{
decorations = 100;
}
else if (choice2 == "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 == "pizza")
{
food = 200;
}
else if (choice3 == "sub sandwiches")
{
food = 250;
}
else if (choice3 == "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);
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论