2016-02-26 64 views
1

我的教授已經給出了一個由多位構造函數創建Monthnum類來完成所有參數的任務,因爲我們正在學習面向對象編程。我需要創建一個新的構造函數,它接受用戶輸入作爲一個int值,另一個構造函數接受它作爲一年中的字符串值。例如:1 = 1月和1月= 1。我知道我可以在我的主要方法中創建掃描儀,但我不確定如何讓此號碼被接受並打印出來。在正確的直接步驟將是非常有用的!與對象和構造函數一起使用的Java

import java.util.Scanner; 

public class learningObjectsAndClasses { 
    public static void main(String args[]){ 

    Scanner input = new Scanner(System.in); 
    int monthNumber = input.nextInt(); 
    String monthName = input.nextLine(); 
    Monthnum inputMonthNumber = new Monthnum(monthNumber); 
    Monthnum inputMonthName = new Monthnum(monthName); 
    System.out.println("Please enter the month name or number: " 
     + inputMonthNumber);  

    } 
} 
class Monthnum{ 

    int Monthnum; 
    String monthName; 


    Monthnum(){ 
     Monthnum = 1; 
    } 
    Monthnum(int whichMonth){ 
     Monthnum = whichMonth; 
     if (whichMonth == 1){ 
      System.out.println("January"); 
     } 
     else if (whichMonth == 2){ 
      System.out.println("February"); 
     } 
     else if (whichMonth == 3){ 
      System.out.println("March"); 
     } 
     else if (whichMonth == 4){ 
      System.out.println("April"); 
     } 
     else if (whichMonth == 5){ 
      System.out.println("May"); 
     } 
     else if (whichMonth == 6){ 
      System.out.println("June"); 
     } 
     else if (whichMonth == 7){ 
      System.out.println("July"); 
     } 
     else if (whichMonth == 8){ 
      System.out.println("August"); 
     } 
     else if (whichMonth == 9){ 
      System.out.println("September"); 
     } 
     else if (whichMonth == 10){ 
      System.out.println("October"); 
     } 
     else if (whichMonth == 11){ 
      System.out.println("November"); 
     } 
     else if (whichMonth == 12){ 
      System.out.println("December"); 
     } 
     else 
      System.out.println("Invalid input"); 

     } 
    Monthnum(String whichMonth){ 
     if (whichMonth == "January"){ 
      Monthnum = 1; 
     } 
     else if (whichMonth == "February"){ 
      Monthnum = 2; 
     } 
     else if (whichMonth == "March"){ 
      Monthnum = 3; 
     } 
     else if (whichMonth == "April"){ 
      Monthnum = 3; 
     } 
     else if (whichMonth == "May"){ 
      Monthnum = 4; 
     } 
     else if (whichMonth == "June"){ 
      Monthnum = 5; 
     } 
     else if (whichMonth == "July"){ 
      Monthnum = 6; 
     } 
     else if (whichMonth == "August"){ 
      Monthnum = 7; 
     } 
     else if (whichMonth == "September"){ 
      Monthnum = 8; 
     } 
     else if (whichMonth == "October"){ 
      Monthnum = 9; 
     } 
     else if (whichMonth == "November"){ 
      Monthnum = 10; 
     } 
     else if (whichMonth == "December"){ 
      Monthnum = 11; 
     } 
     else if (whichMonth == "March"){ 
      Monthnum = 12; 
     } 
     else 
      System.out.println("Invalid input"); 
    } 
} 
+0

的練習的要點是瞭解的構造,是嗎?如果是這樣,你確定你應該在這些構造函數中包含掃描器的實現嗎?請記住,構造函數的目的是幫助設置一個對象以供使用。除了接收參數並將這些參數分配給對象的局部字段之外,您通常不會在構造函數中包含功能。你應該和你的老師覈實你對運動的理解。 – MarsAtomic

+0

你有陣列嗎?毫無疑問,比現在擁有的if-else長鏈更好。 – markspace

+0

我可能會將它改爲未來的數組,但現在我只想讓程序工作 – simplest

回答

1

因此,您安裝掃描儀和一切後。您想要讀取用戶輸入的值。你會用Scanner's .nextInt()方法讀取它。

Scanner input = new Scanner(System.in); 
int num; 
num = input.nextInt(); 

然後閱讀它並將它保存在一個變量num後,可以實例Monthnum的對象爲您從用戶得到了一個給定的參數。

Monthnum m = new Monthnum(num); 

如果你想讀取用戶的字符串,可以用戶Scanner's nextLine()方法是這樣的:

Scanner input = new Scanner(System.in); //Same scanner from above 
             //no need to initialize it again 
String s; 
s = input.nextLine(); 

現在,你將宣佈與String參數初始化Monthnum的對象(你的避風港還沒有發表)

Monthnum p = new Monthnum(s); 

現在這裏是一個提示,使該構造函數。如果你已經擁有這些東西,你可以使用它們。只要改變他們是這樣的:

if (whichMonth.equals("January"){ 
     System.out.println("a"); 
} 
.... 

如果你不明白的東西問我問題!感謝

編輯:所以現在你的代碼應該是這樣的:

package tst; 

import java.util.Scanner; 

public class learningObjectsAndClasses { 
public static void main(String args[]){ 

    Scanner input = new Scanner(System.in); 

    System.out.println("Enter Month Name "); 
    String monthName = input.next(); 
    Monthnum inputMonthNumber = new Monthnum(monthName); 

    System.out.println("Enter Number "); 
    int monthNumber = input.nextInt(); 
    Monthnum inputMonthName = new Monthnum(monthNumber); 

} 
public static class Monthnum{ 

    public int Monthnum; 
    public String monthName; 


    public Monthnum(){ 
     Monthnum = 1; 
     monthName = "January"; 
    } 
    public Monthnum(int whichMonth){ 
     Monthnum = whichMonth; 
     if (whichMonth == 1){ 
      monthName = "January"; 
      System.out.println("January"); 
     } 
     else if (whichMonth == 2){ 
      monthName = "February"; 
      System.out.println("February"); 
     } 
     else if (whichMonth == 3){ 
      monthName = "March"; 
      System.out.println("March"); 
     } 
     else if (whichMonth == 4){ 
      monthName = "April"; 
      System.out.println("April"); 
     } 
     else if (whichMonth == 5){ 
      monthName = "May"; 
      System.out.println("May"); 
     } 
     else if (whichMonth == 6){ 
      monthName = "June"; 
      System.out.println("June"); 
     } 
     else if (whichMonth == 7){ 
      monthName = "July"; 
      System.out.println("July"); 
     } 
     else if (whichMonth == 8){ 
      monthName = "August"; 
      System.out.println("August"); 
     } 
     else if (whichMonth == 9){ 
      monthName = "September"; 
      System.out.println("September"); 
     } 
     else if (whichMonth == 10){ 
      monthName = "October"; 
      System.out.println("October"); 
     } 
     else if (whichMonth == 11){ 
      monthName = "November"; 
      System.out.println("November"); 
     } 
     else if (whichMonth == 12){ 
      monthName = "December"; 
      System.out.println("December"); 
     } 
     else { 
      System.out.println("Invalid input from int cons"); 
     } 

    } //end of cons 

    public Monthnum(String whichMonth){ 
     if (whichMonth.equals("January")){ 
      Monthnum = 1; 
      System.out.println(Monthnum); 
     } 
     else if (whichMonth.equals("February")){ 
      Monthnum = 2; 
      System.out.println(Monthnum); 
     } 
     else if (whichMonth.equals("March")){ 
      Monthnum = 3; 
      System.out.println(Monthnum); 
     } 
     else if (whichMonth.equals("April")){ 
      Monthnum = 4; 
      System.out.println(Monthnum); 
     } 
     else if (whichMonth.equals("May")){ 
      Monthnum = 5; 
      System.out.println(Monthnum); 
     } 
     else if (whichMonth.equals("June")){ 
      Monthnum = 6; 
      System.out.println(Monthnum); 
     } 
     else if (whichMonth.equals("July")){ 
      Monthnum = 7; 
      System.out.println(Monthnum); 
     } 
     else if (whichMonth.equals("August")){ 
      Monthnum = 8; 
      System.out.println(Monthnum); 
     } 
     else if (whichMonth.equals("September")){ 
      Monthnum = 9; 
      System.out.println(Monthnum); 
     } 
     else if (whichMonth.equals("October")){ 
      Monthnum = 10; 
      System.out.println(Monthnum); 
     } 
     else if (whichMonth.equals("November")){ 
      Monthnum = 11; 
      System.out.println(Monthnum); 
     } 
     else if (whichMonth.equals("December")){ 
      Monthnum = 12; 
      System.out.println(Monthnum); 
     } 
     else 
      System.out.println("Invalid input"); 
    } 
    } 
} 
+0

謝謝!這是一個巨大的幫助,我知道我就在那裏,但想不到它。 – simplest

+0

最簡單的你有Steam嗎? –

+0

@simplest:輸入以下內容時:Monthnum inputMonthNumber = new Monthnum(monthNumber);應該自動打印月份的名稱。如果這不能解決你的問題,也許我不明白你的問題。你可以在主要問題上發佈更新後的代碼嗎?謝謝 –