2012-10-02 163 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