-1

对于这个问题,我非常感谢一些帮助;我对 Java 也很陌生,所以请原谅任何滥用术语/任何其他可能明显的混淆迹象。这是我正在上的一门课。我对此进行了广泛的研究,但没有成功。我已经设计好了我的程序(我花了这么长时间),但我必须找到一种方法让整个事情重复自己,并且基本上允许用户重复程序而不退出无限次。由于我没有整数,我不确定如何使用 while 循环或执行 while 循环。

import java.util.Scanner;

public class Final
{
    public static void main(String[] args)
    {
        System.out.println("Welcome to Class Reunions! This is a program specifically designed to help plan your future reunion party. Simply enter the following information, and we will  calculate the cost of your party!");
        {
            Scanner user_input = new Scanner(System.in);
            {

                String numberofhours;
                System.out.print("Please enter the number of hours for your party: ");
                numberofhours = user_input.next();

                String numberofguests;
                System.out.print("Please enter the number of guests that plan to attend your party: ");
                numberofguests = user_input.next();

                System.out.println("Please confirm the purchase of our house band: (yes/no): ");
                String bandpurchase = user_input.next();
                if (bandpurchase.equals("yes"))
                    bandpurchase = String.valueOf(350);
                System.out.println("Thank you!");
                if (bandpurchase.equals("no"))
                    bandpurchase = String.valueOf(0);

                int h = (Integer.valueOf(numberofhours) * 200);
                int g = (Integer.valueOf(numberofguests) * 40);
                int b = (Integer.valueOf(bandpurchase));
                int answer = h + g + b;

                String totalcost = String.valueOf(answer);
                System.out.println("Your expected total cost for the party is: $ " + totalcost);

                int p = Integer.valueOf(totalcost) / Integer.valueOf(numberofguests);

                String costperson = String.valueOf(p);
                System.out.println("The cost per person for the party will be: $ " + costperson);
            }
        }
    }
}

我已经尝试了很多东西,但没有任何效果。我想让整个程序在不退出的情况下重复,并让哨兵值“完成”来停止它。非常感谢!

4

2 回答 2

0

如您所见,while 循环将一直持续到您按零为止,因此条件会将新值设置为标记值并使 while 循环中断并退出循环

代码:

public static void main(String[] args) {
        int sentinel = 0; <---------
        while (sentinel == 0) { <----------
            System.out.println("Enter your Name:(for exit just press 0)");
            Scanner input = new Scanner(System.in);
            String name = input.next();
            if (name.equals("0")) { <----- sentinel variable will assinged new value      
                sentinel = 1;              which is one so while loop can be broken 
            } else {
                System.out.println("the name is " + name);
            }
        }
    }

输出:

Enter your Name:(for exit just press 0)
Marco
the name is Marco
Enter your Name:(for exit just press 0)
0
于 2014-07-30T15:24:21.933 回答
0

你想要这种形式的东西。一般的想法是将您的代码包含在一个提示用户确认重复的while循环中。

public class Final
{
    public static void main(String[] args)
    {
        boolean runLoopAgain=true;
        while (runLoopAgain){
            //your
            //existing
            //code
            System.out.println("Again? (yes/no)");
            runLoopAgain=(user_input.nextLine().equals("yes"));
        }
    }
}

此外,您可能需要检查您的格式。例如,您的第一个打印语句下的大块/缩进是不必要的。

于 2014-07-30T15:14:25.380 回答