无效的用户输入在main()

时间:2017-11-06 18:46:46

标签: java recursion

  

这是基本的编码练习。经验水平是初学者。

所需的输出

输入1选项A输入2选项B输入3选项C

* user i / p

* i / p具体实施

如果收到任何其他i / p,则

无效选项

输入1选项A,输入2选项B,输入3选项C

* user i / p

----------希望结束o / p ------------

不使用Switch case,循环,If / else

我的代码

import java.util.*;

public class Selection {

    public static void main(String[] args) {
        System.out.println("Welcome to ABC Shopping Experience");
        System.out.println("Please make selection of items to purchase");
        System.out.println("Clothing-1");
        System.out.println("Electronics-2");
        System.out.println("Sport goods-3");

        new Scan();
        Scan.scan1();
        new Thankyou();
        Thankyou.display(); 
    }
}

import java.util.Scanner;

public class Scan {
    static Scanner scn = new Scanner(System.in);
    static int first = scn.nextInt();
    static int count = 0;

    static Object scan1() {
        if(first == 1) 
            return new Clothing();
        else if (first == 2)
               return new Electronics();
        else if (first == 3)
            return new Sports();
        else {
               System.out.println("Invalid option ");   
            count++;

            if(count == 3) {
                Thankyou t1 = new Thankyou();
                Thankyou.display();
            } else {
                scan1();
            }
        }
        count=0;
        return new Thankyou();
    }
}

import java.util.Scanner;

class Thankyou {
    static void display() {
        System.out.println("thank you come again");
     }
}

------- -----输出

Welcome to ABC Shopping Experience
Please make selection of items to purchase
Clothing-1
Electronics-2
Sport goods-3
6
Invalid option 
Invalid option 
Invalid option 
thank you come again
thank you come again

----- OPUTPUT结束-----

同一个包中的所有类。

我理解切换/循环更容易,但这是我想要学习的过程。

感谢您的时间。

0 个答案:

没有答案