这个java代码编译,但是错了。你能告诉我它有什么问题吗?

时间:2015-10-31 02:13:26

标签: 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");

    }
}

2 个答案:

答案 0 :(得分: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");

        }
    }

答案 1 :(得分: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()以便继续。