如何在Java中获得如下所述的所需输出?

时间:2017-05-18 14:38:09

标签: java

我试着在Java中编写一个名为ShopAssit的简单程序,菜单应该是:

Welcome to ShopAssit

1. Show Shop
2. Show Price
3. Minimum Price from different shops
4. minimum price from one particular shop
5. Discount available 
0. Exit

Enter an option:

以下是必需的输出:

1. when user enters 1:
output should read;
Welcome to ShopAssit
1.Show Shop
2.Show Price
3.Minimum Price from different shops
4.minimum price from one particular shop
5.Discount available 
0.Exit
Enter an option:1
Show Shop
Enter an Option:

When user enters 2, output should read:
Welcome to ShopAssit
1.Show Shop
2.Show Price
3.Minimum Price from different shops
4.minimum price from one particular shop
5.Discount available 
0.Exit
Enter an option:1
Show shop
Enter an option:2
Show price
Enter an Option:

when 3 is entered, it should read;
Welcome to ShopAssit
1.Show Shop
2.Show Price
3.Minimum Price from different shops
4.minimum price from one particular shop
5.Discount available 
0.Exit
Enter an option:1
show shop
Enter an option:2
Show Price
Enter an Option:3
Minimum Price from different shops
Enter an option:

when 4 is entered, it should read:
Welcome to ShopAssit
1.Show Shop
2.Show Price
3.Minimum Price from different shops
4.minimum price from one particular shop
5.Discount available 
0.Exit
Enter an option:1
show shop
Enter an option:2
Show Price
Enter an option:3
Minimum Price from different shops
Enter an option
minimum Price from one particular shop
Enter an Option:

and when user enters 5, the following output is expected;
Welcome to ShopAssit
1.Show Shop
2.Show Price
3.Minimum Price from different shops
4.minimum price from one particular shop
5.Discount available 
0.Exit
Enter an option:
Discount available
Enter an Option:

when 0 is entered the following output is expected:
Welcome to ShopAssit
1.Show Shop
2.Show Price
3.Minimum Price from different shops
4.minimum price from one particular shop
5.Discount available 
0.Exit
Enter an option
Discount available
Enter an option:
Exit

如果不满足上述任何一项,最后会出现错误消息。

我已经启动了如下所示的程序,但它没有给我上面提到的所需输出。

import java.util.*;
public class MyShopAssistant {

public static void main(String[] args) {
    // TODO Auto-generated method stub

     System.out.println("Welcome to Shop Assistant Application.");
     System.out.println("1.Show Shop.");
     System.out.println("2.Show Price.");
     System.out.println("3.Minimum price from different shops.");
     System.out.println("4.Minimum price from one particular shop.");
     System.out.println("5.Discount Available.");
     System.out.println("0.Exit.");
     System.out.println();

    Scanner input =new Scanner(System.in);
    System.out.print("Enter an Option:");
    int menuNumber = input.nextInt();

switch (menuNumber)

     {   
     case 1:
          System.out.println("Show Shop!");
          System.out.print("Enter an option:");
          System.out.print(input.nextInt());

     case 2:
          System.out.println("Show Price.");
          System.out.print("Enter an option:");
          System.out.print(input.nextInt());
     case 3:
          System.out.println("Minimum Price from different shops");
          System.out.print("Enter an Option:");
          System.out.print(input.nextInt());
     case 4:
          System.out.println("4.Minimum price from one particular shop.");
          System.out.print("Enter an Option:");
          System.out.print(input.nextInt());      
     case 5:
          System.out.println("Discount!");
          System.out.print("Enter an Option:");
          System.out.print(input.nextInt());
     case 0:
         System.out.print("Exit!");
         System.out.print(input.nextInt());
     default: 
          System.out.println("Enter value between 0 and 5.");
          System.out.print("Enter an Option:");
          System.out.print(input.next());
          break;       
       }
     }       
}

2 个答案:

答案 0 :(得分:3)

你需要添加休息;在每个案例之后,它应该看起来像这样

case(number): 
//do stuff
break;
//other cases

答案 1 :(得分:1)

  

当输入0时,预计会出现以下输出:欢迎光临商店

正如您现在所做的那样,当输入0时,您将退出。但就像其他人说的那样,必须添加那些break语句。

这里有一个很好的简单示例:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html