Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以将流程图(Java)中的if代码连接到else代码?_Java_If Statement - Fatal编程技术网

是否可以将流程图(Java)中的if代码连接到else代码?

是否可以将流程图(Java)中的if代码连接到else代码?,java,if-statement,Java,If Statement,我已经研究这个问题3个小时了,我被卡住了 我有两个条件,我不一定要用if-else语句将它们分开,我所指的是这个流程图: 我需要if-code仅在其条件为true时执行,然后通过else-code,因此本质上if-code的传出箭头将在else-code的顶部结束,我知道这不是java中最好的,因为我的教科书警告过它 也许我可以用一些代码来说明我的问题 int digit = -1; do { System.out.pr

我已经研究这个问题3个小时了,我被卡住了

我有两个条件,我不一定要用
if-else
语句将它们分开,我所指的是这个流程图:

我需要
if-code
仅在其条件为
true
时执行,然后通过
else-code
,因此本质上
if-code
的传出箭头将在
else-code
的顶部结束,我知道这不是java中最好的,因为我的教科书警告过它

也许我可以用一些代码来说明我的问题

        int digit = -1;
        do
        {
            System.out.print("Please enter your choise (1-6): ");     
            if (in.hasNextInt()) 
            {
                digit = in.nextInt();
            }
            else 
            {
                in.next();
                System.out.println(" ");
                System.out.println("This is a wrong input. Please try again!");
            }
        }while (digit > 6 || digit < 1);
// the program is asking the user to read off of a menu with number 1-6 
// representing different menu items, this ensures that the user inputs 
// is an integer 1-6 and asks again if not an integer 1-6       

        
        String digitName;
        switch (digit)
        {
            case 1: digitName = "Hamburger"; break;
            case 2: digitName = "Pizza"; break;
            case 3: digitName = "Noodle"; break;
            case 4: digitName = "Salad"; break;
            case 5: digitName = "Sandwhich"; break;
            case 6: digitName = "Finish the order!"; break;
            default: digitName = ""; break;
        }
        System.out.println(" ");
        System.out.println("Your choice is: " + digitName + ".");
// the switch statement allows the right menu item to be printed in the above print statements
        
        if (digit == 6) 
        {
            System.out.println("No need to pay for the delivery fee.");
            System.out.println("The total price is $0.0.");
            System.out.println("Thank you for using Online Order Program!");
// if digit == 6 then then user would like to exit
        }
        else 
        {
// given the choice of the user from the menu, other than exiting, the program
// asks the user if they would like meat with their order and this ensures/only 
// accepts an input from the user "yes" or "no", if not then prompts the user 
// again until "yes" or "no" not lower or upper case sensitive  
            System.out.print("Would you like to have some meat on your " + digitName + "? (Enter yes or no, don't worry about capitals) "); 
            String likeSomeMeatNul = in.next();
            String likeSomeMeat = likeSomeMeatNul.toLowerCase();
                        
            if (!likeSomeMeat.equals("yes") && !likeSomeMeat.equals("no")) 
            {
            
                do 
                {
                    System.out.println("\nThis is a wrong input. Please try again!");
                    System.out.print("Would you like to have some meat on your " + digitName + "? (Enter yes or no, don't worry about capitals) ");
                    String someMeat = in.next();
                    likeSomeMeat = someMeat.toLowerCase();
            
                }while (!likeSomeMeat.equals("yes") && !likeSomeMeat.equals("no"));
            }
        
// if the user would like meat with their order then the program needs to know 
// which kind, in this case "beef" or "pork" and the program only accepts inputs
// "beef" and "pork" not lower or upper case sensitive      
            if (likeSomeMeat.equals("yes")) 
            {
                System.out.print("Beef or Pork? (Enter Beef or Pork, don't worry about capitals) ");    
                String beefOrPorkNul = in.next();
                String beefOrPork = beefOrPorkNul.toLowerCase();
            
                if (!beefOrPork.equals("beef") && !beefOrPork.equals("pork")) 
                {
                    do 
                    {
                        System.out.println("\nThis is a wrong input. Please try again!");
                        System.out.print("Beef or Pork? (Enter Beef or Pork, don't worry about capitals) ");
                        String enterBeefOrPork = in.next();
                        beefOrPork = enterBeefOrPork.toLowerCase();
            
                    }while (!beefOrPork.equals("pork") && !beefOrPork.equals("beef"));
        }

// but if the user does not want any meat with their order
// say they ordered #3 Noodles, they just want noodles without meat
// I need to prompt the user if still like to order more food
// it works if the user wants their order with meat, and the program
// terminates if the user inputs "no" for "Would you like to have some meat?"       
        
        System.out.print("Would you like to have more food? (Enter yes or no, don't worry about capitals) ");
        String moreFoodNul = in.next();
        String moreFood = moreFoodNul.toLowerCase();
        
        if (!moreFood.equals("yes") && !moreFood.equals("no")) 
        {
            do 
            {
                System.out.println("\nThis is a wrong input. Please try again!");
                System.out.print("Would you like to have more food? (Enter yes or no, don't worry about capitals) ");
                String enter_more_food = in.next();
                moreFood = enter_more_food.toLowerCase();
            
            }while (!moreFood.equals("yes") && !moreFood.equals("no"));
        }
//only accepts "yes" or "no" not case sensitive
int位=-1;
做
{
系统输出打印(“请输入您的选择(1-6):”;
if(在.hasNextInt()中)
{
数字=in.nextInt();
}
其他的
{
in.next();
System.out.println(“”);
System.out.println(“这是错误的输入,请重试!”);
}
}而(位数>6 | |位数<1);
//程序要求用户读取数字为1-6的菜单
//表示不同的菜单项,这确保用户输入
//是整数1-6,并再次询问是否不是整数1-6
字符串digitName;
开关(数字)
{
案例1:digitName=“汉堡包”中断;
案例2:digitName=“Pizza”中断;
案例3:digitName=“面条”中断;
案例4:digitName=“沙拉”中断;
案例5:digitName=“Sandwhich”中断;
案例6:digitName=“完成订单!”中断;
默认值:digitName=“”;中断;
}
System.out.println(“”);
System.out.println(“您的选择是:“+digitName+”);
//switch语句允许在上述打印语句中打印右菜单项
如果(数字==6)
{
System.out.println(“无需支付交付费”);
System.out.println(“总价为$0.0”);
System.out.println(“感谢您使用在线订购程序!”);
//若数字==6,则用户希望退出
}
其他的
{
//如果用户可以从菜单中选择(而不是退出),则程序
//询问用户是否希望在订单中添加肉类,这确保/仅限于
//接受用户的输入“是”或“否”,如果不是,则提示用户
//直到“是”或“否”不区分大小写为止
System.out.print(“您想在“+digitName+”?(输入是或否,不必担心大写字母)上放点肉吗”);
字符串likesMemeatNul=in.next();
字符串likesMemeat=likesMemeatnul.toLowerCase();
如果(!likeSomeMeat.equals(“是”)和&!likeSomeMeat.equals(“否”))
{
做
{
System.out.println(“\n输入错误,请重试!”);
System.out.print(“您想在“+digitName+”?(输入是或否,不必担心大写字母)上放点肉吗”);
字符串somememeat=in.next();
likeSomeMeat=someMeat.toLowerCase();
}而(!likeSomeMeat.equals(“是”)和&!likeSomeMeat.equals(“否”);
}
//如果用户想要肉和他们的订单,那么程序需要知道
//在这种情况下,是“牛肉”还是“猪肉”,程序只接受输入
//“牛肉”和“猪肉”不区分大小写
if(likeSomeMeat.equals(“yes”))
{
System.out.print(“牛肉或猪肉?(输入牛肉或猪肉,不必担心大写)”;
字符串beefOrPorkNul=in.next();
字符串beefOrPork=beefOrPorkNul.toLowerCase();
如果(!beefOrPork.equals(“牛肉”)和(!beefOrPork.equals(“猪肉”))
{
做
{
System.out.println(“\n输入错误,请重试!”);
System.out.print(“牛肉或猪肉?(输入牛肉或猪肉,不必担心大写)”;
字符串enterBeefOrPork=in.next();
beefOrPork=输入beefOrPork.toLowerCase();
}而(!beefOrPork.equals(“猪肉”)和&!beefOrPork.equals(“牛肉”);
}
//但是如果用户不想要任何肉类与他们的订单
//比如说他们点了3个面条,他们只想要没有肉的面条
//如果还想点更多的食物,我需要提示用户
//如果用户想要他们点的肉和程序,它就会起作用
//如果用户为“您想吃肉吗?”输入“否”,则终止
System.out.print(“您想再吃点东西吗?(输入是或否,不用担心大写字母)”;
字符串moreFoodNul=in.next();
字符串moreFood=moreFoodNul.toLowerCase();
如果(!moreFood.equals(“是”)和&!moreFood.equals(“否”))
{
做
{
System.out.println(“\n输入错误,请重试!”);
System.out.print(“您想再吃点东西吗?(输入是或否,不用担心大写字母)”;
字符串输入_more_food=in.next();
moreFood=输入_more_food.toLowerCase();
}而(!moreFood.equals(“是”)和&!moreFood.equals(“否”);
}
//仅接受“是”或“否”,不区分大小写
我想它会检查
if(比如某些肉等于(“yes”)
中的条件是真是假,如果是真的,则执行并移到下一行,如果是假,则移到下一行,但这显然不是真的
if (condition) {
   // if code
} else {
   // else code
}
if (condition) {
   // if code
}
// else code
if (insideUS) {
    if (continentalUS) {
        shippingCost = 5;
    } else {
        shippingCost = 10;
    }
} else {
    shippingCost = 10;
}
if (insideUS && continentalUS) {
        shippingCost = 5;
} else {
    shippingCost = 10;
}
String likeSomeMeat = likeSomeMeatNul.toLowerCase();