首页 > 解决方案 > Java,根据用户输入返回程序中的上一步

问题描述

我有一个程序,其中向用户询问多项选择问题,每个问题的选择之一是退出。

如果选择退出,我必须询问用户是否要重新开始,如果是,我想循环回到某个检查点。如果不是,则程序终止。

下面是一个只有一个问题的简化示例。我正在尝试构建的程序有多个。

有没有一种方法不涉及嵌套循环上的嵌套循环?

import java.util.Scanner;

class RandomStuff {
    
    /* this is not the actual program, just a simplified version to show what I'm trying to figure out
    let's pretend perfect input
    */
    
    public static void main (String[]args){
       
     Scanner keyboard = new Scanner(System.in);
     
     // checkpoint I wish to loop back to. Not the beginning of the program
     System.out.println("Press 1 to continue, press 2 to cancel ");
     int number = keyboard.nextInt();
     String again;
     
     switch(number){
     
    case 1:
   System.out.println("user chose to continue");
   
    break;
    
   case 2:
   System.out.println("user chose to cancel");
   System.out.println("start again? (yes/no)");
   again = keyboard.next();
   
   if("yes".equals(again)){
   /* loop to checkpoint*/ }
   
   else 
       System.exit(0);
    break;
         
     }
     
     /* afterwards, a few other rounds of asking an input an looping to the same checkpoint 
     when appropriate. Essentially I'm looking for a method to loop back to checkpoint
     from any decision to be made
     */
     
   
    keyboard.close();
}

    }

标签: java

解决方案


使用递归函数是循环的最佳选择。您可以使用另一种方法,而不是在 main 方法中编写您的选择:

import java.util.Scanner;
class Random{
    
    public static void Choice(Scanner keyboard){
        
        System.out.println("Press 1 to continue, press 2 to cancel ");
        int number = keyboard.nextInt();
        String again;
     
        switch(number){
     
        case 1:
        System.out.println("user chose to continue");
        break;
    
        case 2:
        System.out.println("user chose to cancel");
        System.out.println("start again? (yes/no)");
        again = keyboard.next();
   
        if("yes".equals(again)){
            
                Choice(keyboard);
                //Function will be called again
            
            }
   
        else 
            System.exit(0);
            break;
         
        }
        
    }
    
    public static void main(String args[]){
        
        Scanner sc = new Scanner(System.in);
        
        Choice(sc);
        
    }
    
}

但这也可以通过使用单个 do-while 循环轻松完成

看到这个:

import java.util.Scanner;
class Random{
    
    public static void main(String args[]){
        
        Scanner keyboard = new Scanner(System.in);
        
        boolean run = true;
        
        do{
            
            System.out.println("Press 1 to continue, press 2 to cancel ");
            int number = keyboard.nextInt();
            String again;
     
            switch(number){
     
            case 1:
                System.out.println("user chose to continue");
                break;
    
            case 2:
                System.out.println("user chose to cancel");
                System.out.println("start again? (yes/no)");
                again = keyboard.next();
    
                if("yes".equals(again)){
                    
                    continue;
                
                }
    
                else 
                    run = false;
                    //terimanting the loop
                    break;
         
           }
        }
        while(run);{
            System.out.println("User chose to exit"); 
            System.exit(0);  
        }
        
    }
    
}

我测试了这两个程序,它们运行良好,如果您有任何问题或无法理解某些内容,请告诉我。


推荐阅读