如果条件为假,如何重复方法?

时间:2019-03-25 20:43:57

标签: java

我正在制作一个“ Tinder4Food”程序/项目,在该程序/项目中,它为您提供两种或更多种食物,然后用户选择一种。现在,由于我想完成基本操作,因此只能选择3种食物。选择食物后,它会询问您是否已完成,并想转到查看页面,在该页面上您会看到所有喜欢的食物。就像我之前说的,只有3种食物,因此只会列出1种或2种食物。因此,您可以选择是,我想进入我的评论页面,或者不,我不想。那就是问题所在。如果用户拒绝,我希望它重复此方法,在该方法中向您显示食物的选择,然后选择其中一种。之后,它将引导您返回到评论问题,依此类推...

我看了网上,发现循环是最好的。我已经尝试过了,但是还是不行。也许我做错了。


import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.lang.String;
import java.util.Scanner;



public class Main {

    public int food() {
        return food();

    }

    public static void main(String[] args) {


        List<String> foodList = new ArrayList<String>();

        //random foods getting listed
        Random rand = new Random();

        int x = rand.nextInt(3) + 1;
        int l = rand.nextInt(3) + 1;

        System.out.print("Which food do you want?");
        foodList.add("pizza");
        foodList.add("burger");
        foodList.add("fries");

        System.out.println("In our menu today we have " + foodList.get(0) + " " + foodList.get(1) + " " + foodList.get(2));


            Scanner food = new Scanner(System.in);
            int n = food.nextInt();
            String input = food.nextLine();
            String array[] = input.split(" ");
            // ask discord for help about using an if else statement for this.

            int f = 1;
            int z = 2;
            int y = 3;

            System.out.println(f + " is " + foodList.get(0) + " " + z + " is " + foodList.get(1) + " " + y + " is " + foodList.get(2));
            // f is equal to pizza
            // int z is equal to burger
            // int y is equal to fries


            if (n == f) {
                System.out.println(foodList.get(0) + " will be added to your good list");

            }

            if (n == z) {

                System.out.println(foodList.get(1) + " will be added to your good list!");
            }

            if (n == y) {

                System.out.println(foodList.get(2) + " will be added to your good list!");


            }



            System.out.println("Is this your final choice?");

            Scanner end = new Scanner(System.in);
            int k = end.nextInt();
            String choice = food.nextLine();
            String endchoice[] = input.split(" ");


            int b = 1;
            int v = 2;
            // b is equal to yes. v is equal to no.

            if (k == b) {
                System.out.println("Okay, this is your good list " + n);


            }

            if (k == v) {

                System.out.println("Okay. We will keep going ");


            }
        }
    }



一切正常,直到最后一部分。我之前提到过它,如果您想知道的话,我会再做一次。我想重复询问您想要哪种食物的方法。之后,将您带回到询问您是否已完成并要转到查看页面或继续进行的问题。最终问题方法是底部的两个if语句。我特别是在说if (k == v)的if语句。还有其他一些错误,例如当您回答最后一个问题时,您必须输入int设置为的数字,然后输入int字母。

如果您能提供帮助,那就太好了。我不是Java的新手,但是我还不太了解。我只有13个tryna学习如何编码。谢谢!!!

0 个答案:

没有答案