2017-05-18 74 views
-6

我tryng寫在Java中稱爲ShopAssit簡單的程序在菜單中應改爲:如何在Java中獲取所需的輸出?

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;  
     } 
    }  
} 
+1

你不使用'打破任何特別的原因;'不同情況之間? – AntonH

+0

...它給出了什麼輸出? –

+2

您是否嘗試過使用調試器計算出您的程序在做什麼?學習調試你自己的代碼是你作業的重要部分。無論哪種方式,如果你需要幫助,你需要清楚地解釋你所看到的不正確的行爲。 –

回答

3

您需要添加break;每一個案件後,所以它應該是這樣的

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

根據所需的輸出我想我應該包括選項4之後的休息。 –

+0

@丹尼爾喬爾你知道情況2不會包括寫在上面的情況下輸出?要麼創建一個方法,包括你重複和調用它的部分,或者在每種情況下機械地重複一切。如果你有案例顯示相同的輸出,你可以做案例(1 || 2):這意味着案例1或2 – murksiuke

+0

@mursiuke,謝謝你的提示,我試着去做。 –