如何初始化结果变量?

时间:2019-05-31 05:27:44

标签: java

我需要使结果变量可变,以便将其与我的其他字符串连接起来以打印结果,并且尝试了多种不起作用的方法。

我已经包含了我的字符串集和第一个if语句,以便对我的项目有所了解。

我的项目规则指出:“使用串联将'结果'初始化为单个字符串。此字符串将包含事件类型,聚会大小,用餐和准备建议。将结果变量打印到控制台。它还说“'result'是将被打印到控制台的字符串。

import java.util.Scanner;

public class whatToEat {   

    public static void main (String[] args) {
        int partySize;
        String eventType;

        System.out.println("Is your event Casual, Semi-Formal, or Formal?");
        Scanner sc = new Scanner(System.in);
        eventType = sc.next();

        System.out.println("How many people will your event have?");
        partySize = sc.nextInt();

        if (eventType.equals("Casual") && partySize == 1) {

            System.out.println("Since you're hosting a casual event for "
                    + partySize + 
                    " person, you should serve sandwiches prepared in the mircowave. ");

1 个答案:

答案 0 :(得分:0)

因为它说“结果”是您在控制台中打印的字符串,所以建议您先将信息保存在字符串中:

String result = "";

然后:

result = "Since you're hosting a casual event for " + partySize + " person, you should serve sandwiches prepared in the mircowave."

最后:

System.out.println(result);