2017-04-04 93 views
-1

我已經編寫了此代碼以將用戶鍵入的任何整數轉換爲星期幾。我一直無法解決我遇到的一些錯誤,我錯過了什麼?如何將用戶(int)的輸入轉換爲字符串

import java.util.Scanner; 
    class dayofweek2 { 
     public static void main(String[] args) { 
     Scanner daynumber = new Scanner(System.in); 

     String input = Integer.parseInt(daynumber); 
     System.out.println("Enter a number from 1 to 7"); 
     if (input == 1) { 
      System.out.println("monday"); 
     } 
     if (input == 2) { 
      System.out.println("tuesday"); 
     } 
     if (input == 3) { 
      System.out.println("wednesday"); 
     } 
     if (input == 4) { 
      System.out.println("thursday"); 
     } 
     if (input == 5) { 
      System.out.println("friday"); 
     } 
     if (input == 6) { 
      System.out.println("saturday"); 
     } 
     if (input == 7) { 
      System.out.println("sunday"); 
     } 
     } 
    } 
+1

'如果(輸入== 1);''if'是小寫和不應該有一個分號 –

+0

daynumber沒有任一數目。你無法解析。 –

+2

'Integer.parseInt'不帶掃描儀對象 –

回答

0

A Scanner需要採取一些輸入。您可以通過調用Scanner.nextLine()或其他next方法之一來實現。

在您的例子,你可以這樣做:

Scanner scanner = new Scanner(System.in); 
String input = scanner.nextLine(); // read a line 
int daynumber = Integer.parseInt(input); // parse the line that was entered as an integer 

switch(daynumber) { 
    // ... 
} 
+0

很有幫助,謝謝@matt – juniorbuba

+0

非常感謝各位傢伙,經過很多努力,我已經能夠完成程序設計,讓它運行我將在稍後發佈完整的程序以供更詳細的審查.....再次感謝各位 – juniorbuba

2

有你的代碼中的多個流浪/陳舊分號。我有固定的代碼爲你:)

提示:你可以使用你switch語句來簡化你的邏輯,使其更具可讀性

import java.util.Scanner; 
    class dayofweek2 { 
     public static void main(String[] args) { 

     Scanner daynumber = new Scanner(System.in); // Print to screen, querying user 
     System.out.println("Enter a number from 1 to 7"); 
     String j = daynumber.nextLine(); // Per requirements, obtain String 
     Integer input = Integer.parseInt(j); // Convert String to Integer 

     if (input == 1) { // Begin processing of input 
      System.out.println("monday"); 
     } 
     if (input == 2) { 
      System.out.println("tuesday"); 
     } 
     if (input == 3) { 
      System.out.println("wednesday"); 
     } 
     if (input == 4) { 
      System.out.println("thursday"); 
     } 
     if (input == 5) { 
      System.out.println("friday"); 
     } 
     if (input == 6) { 
      System.out.println("saturday"); 
     } 
     if (input == 7) { 
      System.out.println("sunday"); 
     } 
     } 
    } 
+0

我寫了一篇用switch語句但是在代碼中也有一些錯誤 – juniorbuba

+0

感謝通過@xyzt – juniorbuba

+0

修復的方式,沒有注意到你使用的是Scanner不正確 – unjankify

0

System.out.println("Enter a number from 1 to 7");

的邏輯是grep用戶輸入的數字並將其存儲在一個變量中。

int tmp = daynumber.nextInt();

,然後用是否繼續或switch語句

1

你有很多不幸的分號。他們終止您的if機構。此外,我會使用Scanner#nextInt()Scanner的另一個變量名稱。接下來,在嘗試讀取input之前顯示提示。而且,我會通過使用一個數組來消除所有這些if(s)。像,

Scanner scan = new Scanner(System.in); 
System.out.println("Enter a number from 1 to 7"); 
int input = scan.nextInt(); 
String[] days = { "monday", "tuesday", "wednesday", "thursday", 
     "friday", "saturday", "sunday" }; 
if (input >= 1 && input <= 7) { 
    System.out.println(days[input - 1]); 
} 
相關問題