2015-10-31 71 views
-5

下面是代碼。我真的需要幫助,因爲我不知道它有什麼問題。正如我所說,它編譯乾淨,但每當我運行它,它都無法執行。謝謝!這個java代碼編譯,但是是錯誤的。你能告訴我它有什麼問題嗎?

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

    Scanner scan = new Scanner(System.in); 

    int i; 
    String s; 
    char c; 

    System.out.print("Please enter a number: "); 
    i = scan.nextInt(); 
    System.out.println(i); 
    System.out.println(); 

    System.out.print("Please input three words separated by spaces: "); 
    s = scan.next(); 
    System.out.println(s); 
    System.out.println(); 

    System.out.print("Please input something: "); 
    s = scan.nextLine(); 
    System.out.println(s); 
    System.out.println(); 

    System.out.print("Please enter another number: "); 
    i = scan.nextInt(); 
    System.out.println(i); 
    System.out.println(); 

    System.out.print("Please input a word: "); 
    s = scan.nextLine(); 
    c = s.charAt(0); 
    System.out.println(c); 
    System.out.println(); 

    System.out.println("Good bye"); 

    } 
} 
+4

*「但是隻要我運行它,它就無法執行」* - 怎麼樣?它會產生錯誤嗎?你如何運行它? – MadProgrammer

+0

醜陋,根本不幹。你可以做得更好。 – duffymo

+0

你幹什麼意思?我是新來的Java。 – arsb48

回答

0
package javaapplication9; 
import java.util.Scanner; 
import java.io.BufferedInputStream; 
public class dumb 
{ 
    public static void main(String[ ] args){ 

     Scanner scan = new Scanner(System.in); 

     int i; 
     String s; 
     char result;   


     System.out.print("Please enter a number: "); 
     i = scan.nextInt(); 
     System.out.println(i); 
     System.out.println(); 

     System.out.print("Please input three words separated by spaces: "); 
     s = scan.next(); 
     System.out.println(s); 
     scan.next(); 


     System.out.print("Please input something: "); 
     scan.nextLine(); 
     s = scan.nextLine(); 
     System.out.println(s); 
     System.out.println(); 

     System.out.print("Please enter another number: "); 
     i = scan.nextInt(); 
     System.out.println(i); 
     System.out.println(); 

     System.out.print("Please input a word: "); 
     scan.nextLine(); 
     s = scan.nextLine(); 
     result = s.charAt(0); 
     System.out.println(result); 

     System.out.println(); 

     System.out.println("Good bye"); 

     } 
    } 
0

試試這個:

Scanner scan = new Scanner(System.in); 
int i; 
String s; 
char c; 
System.out.print("Please enter a number: "); 
i = scan.nextInt(); 
scan.nextLine(); 
System.out.println(i); 
System.out.println(); 

System.out.print("Please input three words separated by spaces: "); 
s = scan.nextLine(); 

System.out.println(s); 
System.out.println(); 

System.out.print("Please input something: "); 
s = scan.nextLine(); 
System.out.println(s); 
System.out.println(); 

System.out.print("Please enter another number: "); 
i = scan.nextInt(); 
scan.nextLine(); 
System.out.println(i); 
System.out.println(); 

System.out.print("Please input a word: "); 
s = scan.nextLine(); 
c = s.charAt(0); 
System.out.println(c); 
System.out.println(); 

System.out.println("Good bye"); 

因此,每個scan.nextInt()後,做scan.nextLine(),所以你可以繼續進行。

+0

非常感謝!這很好。我現在看到我做錯了什麼,我非常感激。 – arsb48

相關問題