2012-10-02 115 views
0
import java.util.Scanner; 
public class Question3 { 
public static void main(String[] args) { 
    String input; 
    int i1,i2,i3; 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("Input a 3 digit number "); 
    input = keyboard.next(); 
    String[] numbers = input.split("\\s+"); 
    i1= Integer.parseInt(numbers[0]); 
    i2= Integer.parseInt(numbers[1]); 
    i3= Integer.parseInt(numbers[2]); 
    System.out.println(i1); 
    System.out.println(i2); 
    System.out.println(i3); 

    } 
} 

下面是它应该做的:某人类型和程序应输出分割字符串转换成3份和转换为int

3然而,它会在行i2 = ...处引发一个arrayindexoutofbounds execpetion。...

我需要它们为INT顺便说一句,因为我需要与他们之后做的东西...我怎样才能解决这个问题? (我的课的问题是...)

Write a program that uses a Scanner to read three integers (positive) displays the biggest number of three. (Please complete without using either of the operators && or ||. These operators will be covered in class shortly. Similarly loops are not required.)

Some sample run:

Please input 3 integers: 5 8 3

The max of three is: 8

回答

2
import java.util.Scanner; 
public class Test { 
public static void main(String[] args) { 
    int i1,i2,i3; 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("Input a 3 digit number "); 

    i1= Integer.parseInt(keyboard.next()); 
    i2= Integer.parseInt(keyboard.next()); 
    i3= Integer.parseInt(keyboard.next()); 
    System.out.println(i1); 
    System.out.println(i2); 
    System.out.println(i3); 

    } 
} 
+0

只要输入的数字是这样输入的就完美了1 2 3不是123 谢谢 – Killerpixler

+0

您可以为自己定义分隔模式。 –

-1

使用keyboard.nextInt()读取int值。如果您需要读取3个值,请使用3次。

文档链接:

Scans the next token of the input as an int. This method will throw InputMismatchException if the next token cannot be translated into a valid int value as described below. If the translation is successful, the scanner advances past the input that matched.

如果你不知道下一个数据可能是一个整数,你可以使用Scanner#hasNextInt方法。

2

而不是input = keyboard.next();尝试input = keyboard.nextLine();

根据Scanner文档默认的分隔符是空白 -

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.

而且方法next() -

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.

所以在我们的例子中keyboard.next()只回报第一个数字作为字符串。

0
import java.util.Scanner; 

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

     String input; 
     int i1,i2,i3; 
     Scanner keyboard = new Scanner(System.in); 
     System.out.println("Input a 3 digit number "); 
     input = keyboard.next(); 
     int i=0; 
     for (String result: input.split("\\s+")){ 
      System.out.println(result.charAt(i++)); 
      System.out.println(result.charAt(i++)); 
      System.out.println(result.charAt(i++)); 
     } 

    } 
} 

你的原代码包含以下错误:numbers[0]包含字符串的所有元素没有空格,因此抛出试图访问numbers[1]时出错。在此代码中,for循环只执行一次,并且不带空格的字符串存储在字符串参考结果中,因此使用result.charAt()显示数字会给出正确的结果。

+0

嗨,欢迎来到StackOverflow!您可能想详细说明一下您的代码以及它的工作原理;只有代码的答案可能会解决某人眼前的问题,但随着代码的解释将使他们更好地理解未来的答案。 – computerfreaker

+0

由killerpixler提问的代码包含以下错误 数字[0]包含字符串中没有空格的所有元素,因此,在尝试执行数字时抛出错误[1] 在我的代码中,for循环是execute只有一次,没有空格的字符串存储在字符串参考结果中,并使用result.charAt()显示数字是正确的结果。 – user128328