Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何返回到程序开头- java

如何返回到程序开头- java
EN

Stack Overflow用户
提问于 2018-06-08 09:24:10
回答 2查看 5.3K关注 0票数 0

我很难让我的代码返回到代码的开头,并在确认不等于1时重新启动。我尝试了返回函数,但它只是不断地重复确认,我不确定该怎么办。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import java.util.Scanner;

public class zoo {
    public static void main(String[] args) {

        int optionNumber = 0;
        int numberOfTickets = 0;
        int confirm = 0;
        int totalAmount = 0;

        int[] priceArray;
        priceArray = new int[3];
        priceArray[0] = 18;
        priceArray[1] = 36;
        priceArray[2] = (int) 32.50; 

        Scanner input = new Scanner (System.in);

        System.out.println("\t" + "@@@@@ Welcome to Zoos Victoria @@@@@");
        System.out.println("\t" + "\t" + "MAIN MENU" + "\n");
        System.out.println("\t" + "Zoo has the following ticketing options" + "\n");
        System.out.println("\t" + "1 = Child (4-6 yrs");
        System.out.println("\t" + "2 = Adult (16+ yrs");
        System.out.println("\t" + "3 = Senior (60+ yrs" + "\n");

        System.out.println("Enter your option:" );
        optionNumber=input.nextInt();

         if(optionNumber == 1) {
            System.out.println("Enter total No of tickets for Child");
            numberOfTickets=input.nextInt();
        } else if (optionNumber == 2){
            System.out.println("Enter total No of tickets for Adult");
            numberOfTickets=input.nextInt();
        } else {
             System.out.println("Enter total No of tickets for Senior");
             numberOfTickets=input.nextInt();
        }

        System.out.println("Press 1 to confirm");
        confirm=input.nextInt();

        if (confirm ==1) {
            System.out.println("confirmed");
        } else {

        }



        if(optionNumber == 1) {
            totalAmount=priceArray[0]*numberOfTickets;
            System.out.println("Total amount for child tickets: $" + totalAmount);

        } else if (optionNumber == 2) {
            totalAmount=priceArray[1]*numberOfTickets;
            System.out.println("Total amount for adult tickets $" + totalAmount);

        } else {
            totalAmount=(int) ((double) priceArray[2]*numberOfTickets);
            System.out.println("Total amount for senior tickets $" + totalAmount);
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2018-06-08 09:33:26

你可以围绕它构建一个循环。类似这样的东西(删除了一些代码,但它应该是什么样子的想法应该是清晰的):

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
boolean chooseOption = true;
    while (chooseOption) {
        Scanner input = new Scanner(System.in);
        //print menu
        System.out.println("\t" + "@@@@@ Welcome to Zoos Victoria @@@@@");
        //TODO Your code 

        if (confirm == 1) {
            System.out.println("confirmed");
            // finish option selection
            chooseOption = false;
        } else {
            //choseOption stays true, hence the menu will be reprinted
            System.out.println("Cancelled ticketing." + " Selected Option number: " + optionNumber);
        }
    }

总是试着把你的问题转移到一系列的任务中。在这种情况下,只要没有成功选择,您就希望显示一个菜单。

票数 3
EN

Stack Overflow用户

发布于 2018-06-08 09:42:18

在检查条件时考虑do...while循环, with the

例如:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Scanner input = new Scanner(System.in);
int confirm;
do {
    // everything in here will repeat
    // until the input is not equal to 1
    // so put your desired code here
    System.out.println("Press 1 to confirm");
    confirm = input.nextInt();
} while (confirm != 1);

因此,只要用户不输入do{do之后到while之前的}中的所有内容都会重复。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50757417

复制
相关文章

相似问题

添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文