Java 重复整个程序并指定sentinel值

Java 重复整个程序并指定sentinel值,java,repeat,sentinel,Java,Repeat,Sentinel,我真的很感激在这个问题上能得到一些帮助;我对Java也很陌生,所以请原谅任何误用术语/任何其他可能明显的混淆迹象。这是我要上的课。我已经广泛地查阅了这篇文章,但没有成功。我的程序都设计好了(花了我这么长时间),但我必须找到一种方法,让整个过程自己重复,基本上允许用户重复程序,而不必无限次退出。由于我没有整数,我不知道如何使用while循环或执行while循环 import java.util.Scanner; public class Final { public static voi

我真的很感激在这个问题上能得到一些帮助;我对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);
            }
        }
    }
}

我试过很多东西,但都没用。我希望在不退出的情况下重复整个程序,并使用sentinel值“Done”来停止它。多谢各位

你想要这种形式的东西。一般的想法是将代码封装在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"));
        }
    }
}

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

如您所见,while循环将一直运行,直到您按下零为止,因此该条件会将新值设置为sentinel值,并使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