2017-09-14 60 views
-3

如何切換結果的順序?我希望在菜單後被要求輸入輸出。如何切換輸出順序?

Welcome to the Library! Please make a selection from the menu: 
1. View. 
2. Show. 
Enter a choice: 1 

不過,我提出先進入輸入,我看到: 輸入選擇:1 歡迎來到圖書館!請從菜單中選擇: 1.查看。 2.顯示。

這是我的代碼:

import java.util.*; 
public class Store { 
public static void main(String[] args) { 
    new Store().use(); 
} 


public void use() { 
    char choice; 
    while ((choice = readChoice()) != 'X') { 
     switch (choice) { 
     case 1: view(); break; 
     case 2: show(); break; 
     default: help(); break; 
     } 
    } 

} 
private char readChoice() { 
    return In.nextChar(); 
} 
private String view() { 
    return ""; 
} 
private String show() { 
    return ""; 
} 

} 
private void help() { 
    System.out.println("Welcome! Please make a selection from the menu:"); 
    System.out.println("1. View."); 
    System.out.println("2. Show."); } 
+0

也許情況下「1」? –

+0

@Jonasw Nope,那不會編譯。那'1'呢? –

+0

@JonasW感謝您的意見。儘管如此,這並沒有改變。這是我想要改變的順序。 – Alison

回答

1

剛上use()方法的頂部添加help()

public void use() { 
    help(); 
    char choice; 
    while ((choice = readChoice()) != 'X') { 
     switch (choice) { 
      case 1: view(); break; 
      case 2: show(); break; 
      default: help(); break; 
     } 
    } 
} 

您還需要分別改變12'1''2',因爲你是切換到char。編譯的事實是因爲編譯器應用narrowing primitive conversionint轉換爲char

+0

java 1.5只接受int,short和enum –

+0

是的,但誰又在使用Java 5? –

0

我認爲有三個原因,代碼沒有達到你的期望。

  1. 您首先輸入輸入,因爲在打印控制檯上的任何內容之前,會調用readChoice()函數。它等待從控制檯讀取一個字符然後返回。所以你必須在while循環之前調用一次Help()函數。

  2. 我想開關櫃不會做你期望的。我的意思是當您輸入1或2時,不會調用view()和show()函數。原因是您將1和2讀作字符而不是整數。所以開關的情況下應該改成這樣:

    switch (choice) { 
        case '1': view(); break; //1 changed to '1' 
        case '2': show(); break; //2 changed to '2' 
        default: help(); break; 
    } 
    
  3. 我想你可能忘記打印「輸入一個選擇:」讀取字符之前。 (我用的是System.out.print(),而不是的System.out.println(),因爲它似乎「輸入選擇」,進入選擇應該是在同一行)

    private char readChoice() { 
        System.out.print("Enter a choice:"); 
        return In.nextChar(); 
    } 
    

這是整個代碼,希望它能正常工作(我把註釋,讓你看看我做了什麼變化):

import java.util.*; 
public class Store { 
public static void main(String[] args) { 
    new Store().use(); 
} 
public void use() { 
    char choice; 
    help();//called help() once before the loop 
    while ((choice = readChoice()) != 'X') { 
     switch (choice) { //cases changed 
     case '1': view(); break; 
     case '2': show(); break; 
     default: help(); break; 
     } 
    } 

} 
private char readChoice() { 
    //printing "Enter a choice:" 
    System.out.print("Enter a choice: "); 
    //I used Scanner class to read the next char, because I don't have 'In' class to use. 
    //you might write "return In.nextChar();" insead of the following lines 
    Scanner reader = new Scanner(System.in); 
    char c = reader.next().charAt(0); 
    return c; 
} 
private String view() { 
    System.out.println("you selected view"); //to see this function is called 
    return ""; 
} 
private String show() { 
    System.out.println("you selected show"); //to see this function is called 
    return ""; 
} 

private void help() { 
    System.out.println("Welcome! Please make a selection from the menu:"); 
    System.out.println("1. View."); 
    System.out.println("2. Show.");  
} 
};